我正在通过PHP Excel生成Excel文档。用户要求将文件内容直接输出到浏览器。我写了以下代码(通过谷歌研究后(:
$writer = PHPExcel_IOFactory::createWriter($xcel, 'Excel5');
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
header("Content-type: applicaton/vnd.ms-excel");
header("Content-type: application/force-download");
header("Content-type: application/octet-stream");
header("Content-type: application/download");;
header("Content-Disposition: attachment;filename=File.xlsx");
header("Expires: 0");
header("Pragma: public");
$writer->save('php://output');
其结果如下:
ࡱ ; ����������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������
�������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������� B = %r8X"1 口径 ��� � ��� � ��� � ��� � ��� � ��� � ��� � ��� � ��� � ��� � ��� � ��� � � � 8 3F ff f ̙ ̙3f 3 fff 3f3 f333 3 3f33 333 iD07_12_ACT_N g W&File 创建于 06 Sep 2012 - 14:19:28 � *+ &ffffff ?'ffffff ?(�?)? "dXX333333 ?333333 ?U}$ > @gg根条目 F ß: ß: 工作簿 F����������
我不是将要求的内容输出为 excel 表,而是获取 excel 表的编码内容。
有人可以解决这个问题吗?
感谢
如果你想生成一个xls (Excel5(文件,试试这个:
$writer = PHPExcel_IOFactory::createWriter($xcel, 'Excel5');
header('Content-Type: application/vnd.ms-excel');
header("Content-Disposition: attachment;filename=File.xls");
header('Cache-Control: max-age=0');
$writer->save('php://output');
如果您想要一个XLSX(适用于Excel 2007或更高版本(文件,请尝试以下操作:
$writer = PHPExcel_IOFactory::createWriter($xcel, 'Excel2007');
header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
header("Content-Disposition: attachment;filename=File.xlsx");
header('Cache-Control: max-age=0');
$writer->save('php://output');
header('Content-Type: application/vnd.ms-excel');
header('Content-Disposition: attachment;filename="01simple.xls"');
header('Cache-Control: max-age=0');
对于大多数浏览器来说应该足够了(IE over SSL是一个例外(:/Tests/01simple-download-xls.php工作吗?
在标题之前,您是否有任何输出到浏览器?