我正在使用Phpspreadsheet生成 Excel 文件。我想直接将文件输出为下载。所以我使用了以下代码:$writer = IOFactory::createWriter($spreadsheet, "Xlsx"); //Xls is also possibleheader('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 文件所需的代码。如何使代码仅导出页面的特定部分?
1 回答
PIPIONE
TA贡献1829条经验 获得超9个赞
您可以清除输出缓冲区以停止任何已经echod. 然后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;
- 1 回答
- 0 关注
- 239 浏览
添加回答
举报
0/150
提交
取消