原声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中文網,轉載請注明出處,感謝您的尊重!
總結
以上是生活随笔為你收集整理的原声php 读取excel乱码_使用PHPExcel读取Excel文件时会读出乱码的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 消防管件做的机器人图片_消防管件组装成机
- 下一篇: 英特尔AMD竞相为笔记本处理器添加图形功