下载 PHPSpreedsheet excel 文件以损坏的文件结尾
Download PHPSpreedsheet excel file ends with corrupted file
我使用 PhpSpreadsheet 库创建了一个 xls 并下载了它,但文件已损坏。 xls 将写入输出流,不会创建文件。
我试图创建一个空的电子表格,但失败了。您可以在以下代码示例中看到它:
$spreadsheet = new Spreadsheet();
$writer = new Xls($spreadsheet);
header('Content-Type:application/vnd.ms-excel');
header('Content-Disposition: attachment;filename="Auswertung_Allgemeinkosten.xls"');
header("Pragma: no-cache");
header("Expires: 0");
header('Cache-Control: max-age=0');
$writer->save('php://output');
如果我打开下载的文件,会显示以下消息:
"File format and extension of 'Auswertung_Allgemeinkosten.xls' don't match. The file could be corrupted or unsafe. Unless you trust its source, don't open it. Do you want to open it anyway?"
文件是这样的:
为什么不使用空模板呢?
$path = "<PATH TO EXCEL>/empty.xlsx";
$targetPath= "<PATH TO TARGET>";
//generate Reader
$reader = \PhpOffice\PhpSpreadsheet\IOFactory::createReader("Xlsx");
//Gen workbook
$spreadsheet = $reader->load($path);
//Do Your Stuff
$writer = \PhpOffice\PhpSpreadsheet\IOFactory::createWriter($spreadsheet, 'Xlsx');
$writer->save($targetPath);
这对我有帮助:
$filename = 'Auswertung_Allgemeinkosten.xlsx';
$writer->save($filename);
header('Content-disposition: attachment; filename='.$filename);
header('Content-type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
header('Content-Length: ' . filesize($filename));
header('Content-Transfer-Encoding: binary');
header('Cache-Control: must-revalidate');
header('Pragma: public');
ob_clean();
flush();
readfile($filename);
unlink($filename);
我使用 PhpSpreadsheet 库创建了一个 xls 并下载了它,但文件已损坏。 xls 将写入输出流,不会创建文件。
我试图创建一个空的电子表格,但失败了。您可以在以下代码示例中看到它:
$spreadsheet = new Spreadsheet();
$writer = new Xls($spreadsheet);
header('Content-Type:application/vnd.ms-excel');
header('Content-Disposition: attachment;filename="Auswertung_Allgemeinkosten.xls"');
header("Pragma: no-cache");
header("Expires: 0");
header('Cache-Control: max-age=0');
$writer->save('php://output');
如果我打开下载的文件,会显示以下消息:
"File format and extension of 'Auswertung_Allgemeinkosten.xls' don't match. The file could be corrupted or unsafe. Unless you trust its source, don't open it. Do you want to open it anyway?"
文件是这样的:
为什么不使用空模板呢?
$path = "<PATH TO EXCEL>/empty.xlsx";
$targetPath= "<PATH TO TARGET>";
//generate Reader
$reader = \PhpOffice\PhpSpreadsheet\IOFactory::createReader("Xlsx");
//Gen workbook
$spreadsheet = $reader->load($path);
//Do Your Stuff
$writer = \PhpOffice\PhpSpreadsheet\IOFactory::createWriter($spreadsheet, 'Xlsx');
$writer->save($targetPath);
这对我有帮助:
$filename = 'Auswertung_Allgemeinkosten.xlsx';
$writer->save($filename);
header('Content-disposition: attachment; filename='.$filename);
header('Content-type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
header('Content-Length: ' . filesize($filename));
header('Content-Transfer-Encoding: binary');
header('Cache-Control: must-revalidate');
header('Pragma: public');
ob_clean();
flush();
readfile($filename);
unlink($filename);