Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

getHighestColumn in xlsx not working

Tags:

php

phpexcel

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

like image 269
Gonzalo.- Avatar asked Apr 09 '13 13:04

Gonzalo.-


1 Answers

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

like image 198
Mark Baker Avatar answered Oct 25 '22 09:10

Mark Baker