PHPExcel讀取excel文件

建議web只作上傳功能,讀取、處理仍是放後臺吧。PHPExcel仍是比較耗費時間、內存的。

實例代碼: php

error_reporting(0);
require_once 'PHPExcel_1.8.0/Classes/PHPExcel.php';     //修改成本身的目錄
echo '<p>TEST PHPExcel 1.8.0: read xlsx file</p>';
 
$filePath = "cdkey.xlsx";  
 
//創建reader對象  
$PHPReader = new PHPExcel_Reader_Excel2007();  
if(!$PHPReader->canRead($filePath)){  
    $PHPReader = new PHPExcel_Reader_Excel5();  
    if(!$PHPReader->canRead($filePath)){  
        echo 'file is not excel';  
        return ;  
    }  
}  
 
//創建excel對象,此時你便可以經過excel對象讀取文件,也能夠經過它寫入文件  
$PHPExcel = $PHPReader->load($filePath);  
 
/**讀取excel文件中的第一個工做表*/  
$currentSheet = $PHPExcel->getSheet(0);  
/**取得最大的列號*/  
$allColumn = $currentSheet->getHighestColumn();  
/**取得一共有多少行*/  
$allRow = $currentSheet->getHighestRow();  
 
//循環讀取每一個單元格的內容。注意行從1開始,列從A開始  
for($rowIndex=1;$rowIndex<=$allRow;$rowIndex++){  
    for($colIndex='A';$colIndex<=$allColumn;$colIndex++){  
        $addr = $colIndex.$rowIndex;  
        $cell = $currentSheet->getCell($addr)->getValue();  
        if($cell instanceof PHPExcel_RichText)     //富文本轉換字符串  
            $cell = $cell->__toString();  
        $arrExcel[$rowIndex][$colIndex]=$cell;       
      
    }  
 
}

//print_r($arrExcel);exit; web


PHPExcel_RichText Object(
    [_richTextElements:private] => Array
     (
               [0] => PHPExcel_RichText_TextElement Object ([_text:private] => 測試)
                     [1] => PHPExcel_RichText_Run Object
                     (
                         [_font:private] => PHPExcel_Style_Font Object
                               (
                                    [_name:private] => Calibri
                                        [_size:private] => 11
                                        [_bold:private] =>
                                        [_italic:private] =>
                                        [_superScript:private] =>
                                        [_subScript:private] =>
                                        [_underline:private] => none
                                        [_strikethrough:private] =>
                                        [_color:private] => PHPExcel_Style_Color Object
                                        (
                                            [_argb:private] => FF000000
                                            [_isSupervisor:private] =>
                                            [_parent:private] =>
                                            [_parentPropertyName:private] =>
                                        )
                                        [_parentPropertyName:private] =>
                                        [_isSupervisor:private] =>
                                        [_parent:private] =>
                                        [colorIndex] => 8
                                    )
                                    [_text:private] => 1
                                )
                        )
                ) 函數

能夠看到對這樣的單元格不能直接讀取單元格的文本內容。(注:這裏的富文本是我本身的翻譯,不知對否)。 測試

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

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

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

相關文章
相關標籤/搜索