Mike
Mike

Reputation: 117

Export html table contents as xlsx format

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.

Upvotes: 3

Views: 5335

Answers (1)

mshomali
mshomali

Reputation: 654

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

Upvotes: 3

Related Questions