PHPExcel 导出 xls 乱码
本文介绍了PHPExcel 导出 xls 乱码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
问 题
使用 PHPExcel 的 save 方法时,
// 保存为 xls 文件,但是这个只是保存到服务器上了,还需要手动下载到本地
$writer->save('商品列表.xls');
直接下载:
if ($this->fileVersion == 'Excel5') {
header('Content-Type: application/vnd.ms-excel'); // 输出xls格式的文件
header('Content-Disposition: attachment;filename="' . $config['fileName'] . '.xls"'); //告诉浏览器输出文件的名称
} else {
header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet'); // 输出xlsx格式的文件
header('Content-Disposition: attachment;filename="' . $config['fileName'] . '.xlsx"'); //告诉浏览器输出文件的名称
}
// If you're serving to IE 9, then the following may be needed
header('Cache-Control: max-age=1');
// If you're serving to IE over SSL, then the following may be needed
header('Expires: Mon, 26 Jul 1997 05:00:00 GMT'); // Date in the past
header('Last-Modified: ' . gmdate('D, d M Y H:i:s') . ' GMT'); // always modified
header('Cache-Control: cache, must-revalidate'); // HTTP/1.1
header('Pragma: public'); // HTTP/1.0
$writer->save('php://output');
通过 php://output 可以直接下载导出的 xls 文件,并且不会在服务器上生成 xls 文件;
但是,在下载的时候,直接报错,这个错误是在浏览器开发者工具中的 network 中出现的,也没有触发文件下载操作
一直没查到这个乱码是怎样造成的,如果直接在 header 之前,print_r($writer); exit;
看到的对象 $writer
是没有乱码的
很困惑这个问题
解决方案
问题已解决,这种方式可以直接下载导出的文件,但是不能使用 Ajax
这篇关于PHPExcel 导出 xls 乱码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文