getHighestColumn in xlsx not working

徘徊边缘 提交于 2019-12-10 04:26:51

问题


Have this code, using PHPExcel

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

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

I have the same excel file saved in .xls and .xlsx It has 10 columns (From B to K), and 10 rows

When I use getHighestColumn, in .xls I'm getting 'K' (correct), but in .xlsx I'm getting AMK (the last column in all excel worksheet) About the row, using .xls I'm getting 10, but in .xlsx I'm getting 1024. I'm pretty sure that, except the table, the rest of the worksheet is in blank.

Any ideas why I'm getting different result?

Here are the readers I'm using

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();
     }

I've checked with var_dump and in each case I'm using the right reader.

php 5.3.5, PHPExcel 1.7.8, Symfony 1.4


回答1:


Rows and columns are counted by getHighestColumn() and getHighestRow() if they contain anything (including style information) rather than simply having content. These values are also calculated when the spreadsheet is loaded, so may not be accurate if you subsequently add new rows or columns to the worksheet.

Instead, use the getHighestDataColumn() and getHighestDataRow() methods to return the highest row and column that actually contain data values in cells. While less efficient, they actually calculate the highest cell reference when called, which is slower, but always accurate




回答2:


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


来源:https://stackoverflow.com/questions/15903471/gethighestcolumn-in-xlsx-not-working

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!