PHPExcel讀取excel文件


之前的博文已經對PHPExcel導出excel文件做了簡單的總結,現對他讀取excel做以下總結。(對數據量不會很大的建可以采用web直接用此方法讀取,如果數據量會很大的話,還是建議web只做上傳功能,讀取、處理還是放后台吧。PHPExcel還是比較耗費時間、內存的。)

 

實例代碼:

 1 //首先導入PHPExcel
 2 require_once 'PHPExcel.php';
 3 
 4 $filePath = "test.xlsx";
 5 
 6 //建立reader對象
 7 $PHPReader = new PHPExcel_Reader_Excel2007();
 8 if(!$PHPReader->canRead($filePath)){
 9     $PHPReader = new PHPExcel_Reader_Excel5();
10     if(!$PHPReader->canRead($filePath)){
11         echo 'no Excel';
12         return ;
13     }
14 }
15 
16 //建立excel對象,此時你即可以通過excel對象讀取文件,也可以通過它寫入文件
17 $PHPExcel = $PHPReader->load($filePath);
18 
19 /**讀取excel文件中的第一個工作表*/
20 $currentSheet = $PHPExcel->getSheet(0);
21 /**取得最大的列號*/
22 $allColumn = $currentSheet->getHighestColumn();
23 /**取得一共有多少行*/
24 $allRow = $currentSheet->getHighestRow();
25 
26 //循環讀取每個單元格的內容。注意行從1開始,列從A開始
27 for($rowIndex=1;$rowIndex<=$allRow;$rowIndex++){
28     for($colIndex='A';$colIndex<=$allColumn;$colIndex++){
29         $addr = $colIndex.$rowIndex;
30         $cell = $currentSheet->getCell($addr)->getValue();
31         if($cell instanceof PHPExcel_RichText)     //富文本轉換字符串
32             $cell = $cell->__toString();
33             
34         echo $cell;
35     
36     }
37 
38 }

這里需要說明的是上面注釋中的“富文本轉換字符串”。

   PHPExcel讀取EXCEl文件中,如果單元格中的內容有兩種字體時,讀到的是富文本的對象:

例如:單元格中有內容:“測試1”,其中前半部分的“測試”字體為宋體,后半部分的“1”字體為Calibri,這時候通過

    $cell = $sheet->getCell($addr)->getValue();

獲取單元格的值。並打印:

 1 PHPExcel_RichText Object(
 2     [_richTextElements:private] => Array
 3      (
 4                [0] => PHPExcel_RichText_TextElement Object ([_text:private] => 測試)
 5                      [1] => PHPExcel_RichText_Run Object
 6                      (
 7                          [_font:private] => PHPExcel_Style_Font Object
 8                                (
 9                                     [_name:private] => Calibri
10                                         [_size:private] => 11
11                                         [_bold:private] => 
12                                         [_italic:private] => 
13                                         [_superScript:private] => 
14                                         [_subScript:private] => 
15                                         [_underline:private] => none
16                                         [_strikethrough:private] => 
17                                         [_color:private] => PHPExcel_Style_Color Object
18                                         (
19                                             [_argb:private] => FF000000
20                                             [_isSupervisor:private] => 
21                                             [_parent:private] => 
22                                             [_parentPropertyName:private] => 
23                                         )
24                                         [_parentPropertyName:private] => 
25                                         [_isSupervisor:private] => 
26                                         [_parent:private] => 
27                                         [colorIndex] => 8
28                                     )
29                                     [_text:private] => 1
30                                 )
31                         )
32                 )

可以看到對這樣的單元格不能直接讀取單元格的文本內容。(注:這里的富文本是我自己的翻譯,不知對否)。

另外,讀取單元格的函數還有:

//列從0開始,行從1開始

$currentSheet ->getCellByColumnAndRow($colIndex,$rowIndex)->getValue();


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM