2013-04-09 82 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

相关问题