Home  >  Article  >  Backend Development  >  phpExcel输出xls文档显示乱码的解决方法

phpExcel输出xls文档显示乱码的解决方法

WBOY
WBOYOriginal
2016-06-13 13:13:22882browse

phpExcel输出xls文档显示乱码的解决办法

最近在做一些报表的系统,主要是对excel的读取和处理操作。

之前在自己的机器上做开发的时候,没有出现乱码的情况,

即使是我布置到服务器上,导入,导出excel都没有出现乱码的情况

后来在一位同事的机器上装了下,却出现乱码的情况,很郁闷,

后来在网上看了一篇博文,就解决了。

?

$filename = urlencode("个税表.xls");
header('Content-Type: application/vnd.ms-excel');
header('Content-Disposition: attachment;filename='.$filename);
header('Cache-Control: max-age=0');
$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5'); 
$objWriter->save('php://output'); 

?

我原来是这么写的,但在有些机器上却出现乱码

?

后来我改成下面的就好了

?

$filename = urlencode("个税表.xls");
ob_end_clean();
header('Content-Type: application/vnd.ms-excel');
header('Content-Disposition: attachment;filename='.$filename);
header('Cache-Control: max-age=0');
$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5'); 
$objWriter->save('php://output'); 

?

加了个ob_end_clean();在输出xls文档之前清理下缓存。就没问题了。

1 楼 telankes2000 2012-05-31  
是有用 能解释下原因吗?

2 楼 yeyuan 2012-05-31  
telankes2000 写道
是有用 能解释下原因吗?
应该是在表的数据输出之前,有一些其它的数据输出!先清理下,再输出就可以了
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn