PHPExcel getHighestColumn() not working

11.3k views Asked by At

Rather than defining cell color etc. in a range in the form of "A1:G1" or range('A','G') - since the table is dynamic and will have different count of columns each time - I wanted to use the range in the form of:

->getStyle('A1:' . $highestColumn . '1')

where

 $highestColumn = $objPHPExcel->getActiveSheet()->getHighestColumn();

but this is not working. If I print the result, $highestColumn is shown to be "A", whereas in fact it should be "G".

Why is getHighestColumn() not working?

2

There are 2 answers

1
Mark Baker On

getHighestColumn() (and getHighestRow()) use a value populated from a file load, and those values then remain unchanged - even if you add further rows or columns yourself - until you save the PHPExcel object.

Perhaps you should use getHighestDataColumn() which is calculated dynamically at the point when it is called, and always based on the actual collection of cell data at that point in time.

0
Al-Iskander Al-Albani On

FIXED IT.

Just took all the code which I was using for formatting etc. and MOVED it below the statement $objPHPExcel->setActiveSheetIndex(0) right before the file is saved.

And I used this as the variable:

$highestColumn = $objPHPExcel->getActiveSheet()->getHighestDataColumn();