php://output - 只输出页面的一部分而不是整个页面

我正在使用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 文件所需的代码。


如何使代码仅导出页面的特定部分?


绝地无双
浏览 326回答 1
1回答

PIPIONE

您可以清除输出缓冲区以停止任何已经echod. 然后exit是您交付内容后的脚本。//protect against infinite looping, in case of an error with ob_end_clean()$successfulClean = TRUE;//empty the output bufferwhile (ob_get_level()!==0       &&$successfulClean===TRUE){    $successfulClean = ob_end_clean();}// your code$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");//------//end the request, to stop your CMS from mucking things up. Content Mucking System lolexit;
打开App,查看更多内容
随时随地看视频慕课网APP