3

Iam using PHP+jQuery to export the html table contents. When I click on on export button, the excel file is generated successfully as xls format. I want to generate the file as xlsx format.

$FileName="my-file.xls";
header('Content-Type: application/force-download');
header('Content-disposition: attachment; filename='.$FileName.'');
header("Pragma: ");
header("Cache-Control: ");
echo $_REQUEST['tableData'];
exit();

Adding the jQuery

var clone_val = $(".tblExport").clone();
$("#tData").val($('<span><style> .textFormat {  mso-number-format:"\@"; } .numberFormat { mso-number-format:"0\.00" }</style>').append (clone_val).html());
$("#excel-export-form").submit();

The table contents is stored inside a input box having attribute id="tData" name="tableData" using jQuery. When I changed the filename as my-file.xlsx, a corrupted xlsx file is generated. Also tried Data Table excel export, but it didn't support multiple headers.

Mike
  • 117
  • 1
  • 3
  • 10

1 Answers1

3

You can do this with a php library. I prefer PhpSpreadsheet to use for working with excel.

mshomali
  • 664
  • 1
  • 8
  • 27
  • Ok. Is it possible to do with the current code. Now xls is export is working. – Mike Aug 19 '19 at 10:09
  • 1
    I think exporting xlsx without PhpSpreadSheet is very difficult and I suggest to you using this library. – mshomali Aug 19 '19 at 10:20