[英]Force download excel file using PHPExcel
我正在使用以下标题输出通过 PHPExcel 生成的 Excel 表:
// Redirect output to a client’s web browser (Excel2007)
header('Content-Type: application/vnd.ms-excel');
header('Content-Disposition: attachment;filename="01simple.xlsx"');
header('Cache-Control: max-age=0');
$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel2007');
$objWriter->save('php://output');
相反,乱码文本被发送到浏览器:
PK������&DG�D�X��������[Content_Types].xml��MN�0���"�%nY ��vAa �(0����ؖg�w{&i�@�nbE�{��y��d۸l
m�����X�(���)���F��;@1_�����c)j�x/%��E��y�
任何人都可以帮助我使用正确的标题来强制下载 excel 文件吗? 非常感谢。
编辑1:
header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
header('Content-Disposition: attachment;filename="01simple.xlsx"');
header('Cache-Control: max-age=0');
$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel2007');
$objWriter->save('php://output');
还是不行
错误的内容类型和文件类型无济于事:
application/vnd.ms-excel
用于使用Excel5
Writer 创建的.xls
文件
application/vnd.openxmlformats-officedocument.spreadsheetml.sheet
用于使用Excel2007
Writer 创建的.xlsx
文件
但是,如果您在浏览器中看到该gibberish
(实际上是 xlsx 文件本身),那么您很有可能也在向浏览器回显或打印其他内容,即使它只是一个空格字符,如空格、制表符或换行符,然后再发送这些标题
清洁输出
ob_end_clean();
$objWriter->save('php://output');
exit();
这个问题偷走了我的一生! 像这样杀死它:
ob_get_clean();
$objWriter->save('php://output');
ob_end_flush();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.