2013-04-09 80 views
6

有這樣的代碼,使用PHPExcelgetHighestColumn在XLSX工作不

public function getHighestColumn() 
    { 
     return $this->objPHPExcel->setActiveSheetIndex(0)->getHighestColumn(); 
    } 

    public function getHighestRow() 
    { 
     return $this->objPHPExcel->setActiveSheetIndex(0)->getHighestRow(); 
    } 

我已經在同一個excel文件保存在.xls和原來的.xlsx 它有10列(從B到K)和10行

當我使用getHighestColumn,在.xls中,我得到'K'(正確),但在.xlsx中我得到AMK(所有excel工作表中的最後一列) 關於該行,使用.xls I' m得到10,但在.xlsx我得到1024. 我很確定,除了表格,其餘的工作表是空白的。

任何想法,爲什麼我得到不同的結果?

下面是我使用

public function openReader($filePath) 
    { 
     //aca determinamos cual tipo es para saber que reader es 
    $reader_5 = new PHPExcel_Reader_Excel5(); 
    $reader_07 = new PHPExcel_Reader_Excel2007(); 

    $inputFileType = $this->canRead(new PHPExcel_Reader_Excel5(), $filePath, self::EXCEL5); 

    if($inputFileType === '') 
     $inputFileType = $this->canRead(new PHPExcel_Reader_Excel2007(), $filePath, self::EXCEL2007); 
    else 
    { 
     throw new Exception("No se puede procesar el archivo; el formato no es correcto"); 
    } 
    return PHPExcel_IOFactory::createReader($inputFileType); 
    } 


private function canRead($reader, $path, $readerType) 
    { 
    return $reader->canRead($path) ? $readerType: ''; 
    } 

public function persist($fileName) 
    { 
     $filePath = sfConfig::get('sf_upload_dir').'\\'.$fileName; 

     $this->objReader = $this->openReader($filePath); 
     //Set only first Sheet 
     $this->setOnlyFirstSheet(); 

     $this->createObjPHPExcel($filePath); 

     echo $this->getHighestColumn(); 
     echo $this->getHighestRow(); 
    } 

我已經var_dump檢查,並在每種情況下我使用恰當的讀者以饗讀者。

PHP 5.3.5,1.7.8 PHPExcel,Symfony的1.4

+1

你不應該得到的XLS文件中的最高列「B」,如果你有在列數據'K'....但在MS EXcel中打開xlsx文件,然後按Ctrl-End來查看它認爲最後一個單元格是什麼 – 2013-04-09 13:40:01

+2

另外,因爲行和列由getHighestColumn()和getHighestRow()計算,如果它們包含_anything_ (包括樣式信息)而不是簡單地包含內容,請查看getHighestDataColumn()和getHighestDataRow()方法 – 2013-04-09 13:42:47

+0

我的意思是,獲得'K'是一個錯字。檢查方法 – 2013-04-09 13:45:11

回答

16

行和列由getHighestColumn()getHighestRow()計算,如果他們包含任何(包括樣式信息),而不是簡單地內容。這些值也是在電子表格加載時計算的,因此如果隨後向工作表中添加新的行或列,可能會不準確。

而是使用getHighestDataColumn()getHighestDataRow()方法返回實際包含單元格中數據值的最高行和列。雖然效率較低,但實際計算調用的時候,最高的單元格引用,這是比較慢的,但總是準確

1
$highestColumn = $sheet->getHighestColumn();  
$colNumber = PHPExcel_Cell::columnIndexFromString($highestColumn); 
+1

提供的代碼可能會有所幫助,但爲了使它成爲一個很好的答案,您還應該描述/解釋爲什麼/您的代碼如何解決問題。 – ZygD 2015-06-19 09:46:09

+0

我已經在這個項目中使用了這段代碼 – gopi 2015-07-02 11:25:47

相關問題