原声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 == "產(chǎn)品信息") $name = "product";else if ($name == "行業(yè)") $name = "industry";else if ($name == "集團(tuán)信息"){continue; $name = "bloc";var_dump($data);die;}else if ($name == "客戶信息"){var_dump($data);die;$name = "customer";}else if ($name == "業(yè)務(wù)員信息") $name = "employeer";else continue;$drive->actionUpload($name, $data);}
這是截圖:
讀取的內(nèi)容前一部分沒有問題,后面的也沒有問題,但是中間這部分會變成這個樣子。
回復(fù)討論(解決方案)
沒有看到亂碼
截圖中的是你 var_dump 的結(jié)果
Excel文件中的數(shù)據(jù)都是正常的,但是在讀取的時候中間會有一部分讀取出類似于“ (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 } ”這樣的數(shù)據(jù)。正常的數(shù)據(jù)輸出應(yīng)該是 [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) "廣州越秀區(qū)中山三路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 的結(jié)果
Excel文件中的數(shù)據(jù)都是正常的,但是在讀取的時候中間會有一部分讀取出類似于“(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 } ”這樣的數(shù)據(jù)。正常的數(shù)據(jù)輸出應(yīng)該是[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) "廣州越秀區(qū)中山三路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問題。
是的,讀取的前一部分內(nèi)容還是正常的,后面也是正常的,但是中間就會有一部分讀出像圖片里那樣。
問題已經(jīng)解決了。是因為讀取單個cell的內(nèi)容,讀取的結(jié)果可能是object類型,也可能是string類型,加上一個判斷,修改下類型就好了。
本文原創(chuàng)發(fā)布php中文網(wǎng),轉(zhuǎn)載請注明出處,感謝您的尊重!
總結(jié)
以上是生活随笔為你收集整理的原声php 读取excel乱码_使用PHPExcel读取Excel文件时会读出乱码的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 消防管件做的机器人图片_消防管件组装成机
- 下一篇: 动态规划算法php,php算法学习之动态