原声php 读取excel乱码_使用PHPExcel读取Excel文件时会读出乱码

这是我读取文件的代码:

require_once dirname(__FILE__) . "/PHPExcel/Classes/PHPExcel.php";$PHPReader = new PHPExcel_Reader_Excel2007();if (!$PHPReader->canRead($filePath)) {$PHPReader = new PHPExcel_Reader_Excel5();if (!$PHPReader->canRead($filePath)) {echo "no file";die;}}$PHPExcel = $PHPReader->load($filePath);$sheetCount = $PHPExcel->getSheetCount();$sheetNames = $PHPExcel->getSheetNames();for ($SheetID = 0; $SheetID < $sheetCount; $SheetID++) {$name = $sheetNames[$SheetID];$currentSheet = $PHPExcel->getSheetByName($name);$allColumn = $currentSheet->getHighestColumn();$allRow = $currentSheet->getHighestRow();$drive = new DriveCommand();$data = array();for ($currentRow = 1; $currentRow <= $allRow; $currentRow++) {for ($currentColumn = 'A'; $currentColumn <= $allColumn; $currentColumn++) {$val = $currentSheet->getCellByColumnAndRow(ord($currentColumn) - 65, $currentRow)->getValue();//$objExcel[$name][$currentRow - 1][ord($currentColumn) - 65] = $val;$data[$currentRow][] = $val;}}if ($name == "产品信息") $name = "product";else if ($name == "行业") $name = "industry";else if ($name == "集团信息"){continue; $name = "bloc";var_dump($data);die;}else if ($name == "客户信息"){var_dump($data);die;$name = "customer";}else if ($name == "业务员信息") $name = "employeer";else continue;$drive->actionUpload($name, $data);}

这是截图:

读取的内容前一部分没有问题,后面的也没有问题,但是中间这部分会变成这个样子。

回复讨论(解决方案)

没有看到乱码

截图中的是你 var_dump 的结果

Excel文件中的数据都是正常的,但是在读取的时候中间会有一部分读取出类似于“ (12) { ["_name":protected]=> string(6) "宋体" ["_size":protected]=> int(12) ["_bold":protected]=> bool(false) ["_italic":protected]=> bool(false) ["_superScript":protected]=> bool(false) ["_subScript":protected]=> bool(false) ["_underline":protected]=> string(4) "none" ["_strikethrough":protected]=> bool(false) ["_color":protected]=> object(PHPExcel_Style_Color)#3006 (4) { ["_argb":protected]=> string(8) "FF000000" ["_parentPropertyName":protected]=> NULL ["_isSupervisor":protected]=> bool(false) ["_parent":protected]=> NULL } ”这样的数据。正常的数据输出应该是 [2]=> array(13) { [0]=> string(6) "B01001" [1]=> string(6) "超市" [2]=> string(12) "百佳超市" [3]=> string(6) "广东" [4]=> string(6) "广州" [5]=> string(21) "广州中华广场店" [6]=> string(51) "广州越秀区中山三路228号中华广场三层" [7]=> NULL [8]=> NULL [9]=> NULL [10]=> NULL [11]=> float(2735) [12]=> string(6) "caoshi" } [3]=> array(13) { [0]=> string(6) "B01002" [1]=> string(6) "超市" [2]=> string(12) "百佳超市" 这种格式。

我也有时候会碰到乱码,有时候又是好的,估计是微软excel问题。

没有看到乱码

截图中的是你 var_dump 的结果

Excel文件中的数据都是正常的,但是在读取的时候中间会有一部分读取出类似于“(12) { ["_name":protected]=> string(6) "宋体" ["_size":protected]=> int(12) ["_bold":protected]=> bool(false) ["_italic":protected]=> bool(false) ["_superScript":protected]=> bool(false) ["_subScript":protected]=> bool(false) ["_underline":protected]=> string(4) "none" ["_strikethrough":protected]=> bool(false) ["_color":protected]=> object(PHPExcel_Style_Color)#3006 (4) { ["_argb":protected]=> string(8) "FF000000" ["_parentPropertyName":protected]=> NULL ["_isSupervisor":protected]=> bool(false) ["_parent":protected]=> NULL } ”这样的数据。正常的数据输出应该是[2]=> array(13) { [0]=> string(6) "B01001" [1]=> string(6) "超市" [2]=> string(12) "百佳超市" [3]=> string(6) "广东" [4]=> string(6) "广州" [5]=> string(21) "广州中华广场店" [6]=> string(51) "广州越秀区中山三路228号中华广场三层" [7]=> NULL [8]=> NULL [9]=> NULL [10]=> NULL [11]=> float(2735) [12]=> string(6) "caoshi" } [3]=> array(13) { [0]=> string(6) "B01002" [1]=> string(6) "超市" [2]=> string(12) "百佳超市" 这种格式。

我也有时候会碰到乱码,有时候又是好的,估计是微软excel问题。

是的,读取的前一部分内容还是正常的,后面也是正常的,但是中间就会有一部分读出像图片里那样。

问题已经解决了。是因为读取单个cell的内容,读取的结果可能是object类型,也可能是string类型,加上一个判断,修改下类型就好了。

本文原创发布php中文网,转载请注明出处,感谢您的尊重!

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值