使用PHPExcel讀取Excel檔案時會讀出亂碼

來源:互聯網
上載者:User
這是我讀取檔案的代碼:
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類型,加上一個判斷,修改下類型就好了。

  • 聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

    如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.