我正在使用以下标头输出通过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
用于.xls
使用Excel5
Writer 创建的文件
application/vnd.openxmlformats-officedocument.spreadsheetml.sheet
用于使用Writer .xlsx
创建的文件Excel2007
但是如果你gibberish
在浏览器中看到它(实际上是xlsx文件本身),那么你可能会回显或打印其他东西给浏览器,即使它只是像空格那样的空白字符,发送这些标头之前的标签页或换行符
清洁输出
ob_end_clean(); $objWriter->save('php://output'); exit();