I need to generate an excel file (xls) and trigger the download after it is generated. I found this example in the documentation.
<?php require 'vendor/autoload.php'; use PhpOfficePhpSpreadsheetSpreadsheet; use PhpOfficePhpSpreadsheetWriterXlsx; $spreadsheet = new Spreadsheet(); $sheet = $spreadsheet->getActiveSheet(); $sheet->setCellValue('A1', 'Hello World !'); $writer = new Xlsx($spreadsheet); $writer->save('hello world.xlsx');
It shows how to create a excel file and save it on the server.
How can I serve the result to the client instead and “force” him to download it?
I need to get the data of the $writer
somehow.
I am currently solving it without PhpSpreadsheet:
// Excel Export $filename = 'export_'.date('d-m-y').'.xls'; $filename = $validator->removeWhitespace($filename); header('Content-type: application/ms-excel'); header('Content-Disposition: attachment; filename='.$filename); exit($response["output"]); // <-- contains excel file content
But it is not working with my delimiter (semicolon). The semicolon is not getting interpreted and everything is getting written into one column.
If I export it as .csv, then it works. But I need it as .xls or .xlsx
Advertisement
Answer
I solved it with a workaround. I temporarily save the file on the server, then I load the content into a variable and serve it as a download file. Then I delete the file from the server.
Workaround:
$date = date('d-m-y-'.substr((string)microtime(), 1, 8)); $date = str_replace(".", "", $date); $filename = "export_".$date.".xlsx"; try { $writer = new Xlsx($response["spreadsheet"]); $writer->save($filename); $content = file_get_contents($filename); } catch(Exception $e) { exit($e->getMessage()); } header("Content-Disposition: attachment; filename=".$filename); unlink($filename); exit($content);