php 导出表格中文乱码,如何解决php excel reader导出excel中文乱码?

解决php excel reader导出excel中文乱码的方法:1、如果不使用dump函数,可以通过修改【_defaultEncoding】变量解决问题;2、如果使用dump函数导出excel,需要修改htmlentities函数解决。

e20df2857bfff24d9d619e79599676d8.png

解决php excel reader导出excel中文乱码的方法:

在下载完php excel reader2.21后,请解压至你的PHP环境配置的运行目录下,打开example.php,首先来看1$data=new Spreadsheet_Excel_Reader("example.xls");

此语句是用来创建一个php导出excel的实例,在excel_reader2.php文件中我们可以找到此php excel reader类的构造函数原型Spreadsheet_Excel_Reader($file='',$store_extended_info=true,$outputEncoding='')

顾名思义,php excel reader导出excel文件内容的编码类型是通过$outputEncoding参数来指定的,默认的php excel reader导出excel的编码类型是通过变量_defaultEncoding设定,默认为UTF-8,所以通常解决php excel reader导出excel中文乱码有两种方法。

php excel reader导出excel中文乱码解决方法一:1$data=new Spreadsheet_Excel_Reader("example.xls");

改为1$data=new Spreadsheet_Excel_Reader("example.xls",true,"GB2312");

php excel reader导出excel中文乱码解决方法二:

打开excel_reader2.php,找到1var$_defaultEncoding="UTF-8";

修改为1var$_defaultEncoding="GB2312";

即可解决php excel reader导出excel乱码的问题。

那为什么通过上述教程修改后,如果在example.xls中添加中文后example.php仍然输出乱码呢?

这是因为其调用了php excel reader类中的dump函数,此函数是用来将导出的excel文件内容以HTML的形式输出,而问题恰恰是由于这个函数中htmlentities函数作怪,htmlentities函数是用来把字符转换为HTML实体的,原型如下htmlentities(string,quotestyle,character-set)

其默认的字符集为ISO-8859-1,所以当使用php excel reader的dump函数导出excel出现中文乱码时,

解决方法一:1$val=htmlentities($val);

修改为1$val=htmlentities($val,ENT_COMPAT,"GB2312");

解决方法二:1$val=htmlentities($val);

修改为1$val=htmlspecialchars($val);

php excel reader导出excel中文乱码解决方法总结

如果不使用dump函数导出excel,可以通过修改_defaultEncoding变量或者通过new Spreadsheet_Excel_Reader(excel文件名,true,”GB2312″);解决导出excel乱码问题,如果使用dump函数以HTML的方式导出excel,需要修改htmlentities函数解决导出excel乱码问题。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值