我正在使用Phpspreadsheet生成Excel文件。
我想直接将文件下载输出。所以我使用了以下代码:
$writer = IOFactory::createWriter($spreadsheet, "Xlsx"); //Xls is also possible
header('Content-Type: application/vnd.ms-excel');
header('Content-Disposition: attachment; filename="VIEW-tilbud.xls"');
header('Cache-Control: max-age=0');
$writer->save("php://output");
但这包括页面的所有内容,因此我的Excel文件被奇怪的字符损坏。 我无法摆脱CMS系统生成的页面上的多余内容,因此我将无法仅拥有需要代码的功能来导出Excel文件。
如何使代码仅导出页面的特定部分?
答案 0 :(得分:2)
您可以清除输出缓冲区以停止所有echo
d的操作。交付内容后,然后exit
脚本。
//protect against infinite looping, in case of an error with ob_end_clean()
$successfulClean = TRUE;
//empty the output buffer
while (ob_get_level()!==0
&&$successfulClean===TRUE){
$successfulClean = ob_end_clean();
}
// your code
$writer = IOFactory::createWriter($spreadsheet, "Xlsx"); //Xls is also possible
header('Content-Type: application/vnd.ms-excel');
header('Content-Disposition: attachment; filename="VIEW-tilbud.xls"');
header('Cache-Control: max-age=0');
$writer->save("php://output");
//------
//end the request, to stop your CMS from mucking things up. Content Mucking System lol
exit;
相关文档: