Chand Jogani
Chand Jogani

Reputation: 146

Datatables.net - Export to Excel is showing error while customizing

I am using SPFx, typescript and datatables.net for my webpart and I want to export to excel and do some customization on data like align to left. Exporting is working fine but while I add code for customization its showing error. here is my code:

 window["JSZip"] = JSZip;
      $('#trackerDataTable').DataTable({
        dom: 'Bfrtip',
        buttons: [
          {
            extend: 'excel',
            text: '<i></i>&nbsp;Export to Excel &nbsp;',
            filename: 'Timecards',
            extension: '.xlsx',
            exportOptions: {
              columns: ':visible'
            },
            customize: function (xlsx) {
              debugger;
              var sheet = xlsx.xl.worksheets['sheet1.xml'];
              $('row c[r^="C"]', sheet).attr('s', '50');
            }
          },
        ]
});

Here in the above code xlsx.xl is showing below error :

enter image description here

It would be great if you can help me here to make it work. Thank you in advance.

Upvotes: 0

Views: 662

Answers (1)

Chand Jogani
Chand Jogani

Reputation: 146

I found the solution now. I created a separate function for the function. I am using typescript here.

window["JSZip"] = JSZip;
      $('#trackerDataTable').DataTable({
        dom: 'Bfrtip',
        buttons: [
          {
            extend: 'excel',
            className: 'btn-primary',
            text: '&nbsp; Export to Excel &nbsp;',
            filename: 'Timecards',
            extension: '.xlsx',
            exportOptions: {
              columns: ':visible'
            },
            customize: function (xlsx) {
              debugger;
              ExportTimecardsWebPart.exportExcel(xlsx); // create separate function
            }
            // customize: function(xlsx) {
            //   debugger;
            //   var sheet = xlsx.xl.worksheets['sheet1.xml'];
            //   $('row c[r^="C"]', sheet).attr('s', '50');
            // }
          },
        ],
});
 
 public static exportExcel(xlsx: any): void {
    debugger;
    var sheet = xlsx.xl.worksheets['sheet1.xml'];
    $('row c[r^="C"]', sheet).attr('s', '50');
  }

Upvotes: 0

Related Questions