Html Grid

Invoke the exportdata method of the jqxGrid. Author: http://jqwidgets.com

HTML

<script src="https://jqwidgets.com/public/jqwidgets/jqx-all.js"></script>
<link rel="stylesheet" href="https://jqwidgets.com/public/jqwidgets/styles/jqx.base.css">
<link rel="stylesheet" href="https://jqwidgets.com/public/jqwidgets/styles/jqx.energyblue.css">
<script src="https://jqwidgets.com/jquery-widgets-demo/demos/jqxgrid/generatedata.js"></script>
<div id='jqxWidget' style="font-size: 13px; font-family: Verdana; float: left;">
    <div id="jqxgrid"></div>
    <div style='margin-top: 20px;'>
        <div style='float: left;'>
            <input type="button" value="Export to Excel" id='excelExport' />
        </div>
    </div>
</div>

JavaScript

var data = [{
    firstname: "Peter",
    lastname: "Parker",
    productname: 'Coffee'
  }, {
    firstname: "John",
    productname: 'Coffee'
  }, {
    firstname: "Peter",
    lastname: "Parker",
  }];

  var source = {
    localdata: data,
    datatype: "array",
    datafields: [{
      name: 'firstname',
      type: 'string'
    }, {
      name: 'lastname',
      type: 'string'
    }, {
      name: 'productname',
      type: 'string'
    }],
    updaterow: function(rowid, rowdata) {
      // synchronize with the server - send update command   
    }
  };
  var dataAdapter = new $.jqx.dataAdapter(source);
  // initialize jqxGrid
  $("#jqxgrid").jqxGrid({
    width: 500,
    source: dataAdapter,
    theme: 'energyblue',
    editable: true,
    selectionmode: 'singlecell',
    columns: [{
      text: 'First Name',
      datafield: 'firstname',
    }, {
      text: 'Last Name',
      datafield: 'lastname',
    }, {
      text: 'Product',
      datafield: 'productname',

    }]
  });
  $("#excelExport").jqxButton({
    theme: 'energyblue'
  });

  $("#excelExport").click(function() {

    const rows = $('#jqxgrid').jqxGrid('getrows');

    const newData = rows.map(row => ({
      firstname: row.firstname || '-',
      lastname: row.lastname || '-',
      productname: row.productname || '-',

    }));

    source.localdata = newData;
    $("#jqxgrid").jqxGrid('updatebounddata');

    $("#jqxgrid").on('bindingcomplete', function() {
      $("#jqxgrid").jqxGrid('exportdata', 'xls', 'jqxGrid');
    });

  });