Php – Get Width of Auto Size column in PHPExcel

PHPphpexcel

$objPHPExcel->getActiveSheet()->getColumnDimension("A")->setAutoSize(true);

$Acolumnsize = $objPHPExcel->getActiveSheet()->getColumnDimension("A")->getWidth();

echo "<h1>" . $Acolumnsize . "</h1>";

I want to set a column to auto size itself. In this case it's column A.

Once it has been auto sized I want to be able to use the value when determining the width of a different column so I need to know how much space column A takes up.

Any time I used the above code the width of column A is -1 meaning the column width hasn't been set. So it seems the auto sizing isn't setting the width of the column for some reason.

I'm wondering how do I get the width of the auto sized column?

Best Answer

The Worksheet classes have a calculateColumnWidths method. This can be use to force the calculation of auto-sized columns, which is usually to delayed until it is necessary. So try:

$objPHPExcel->getActiveSheet()->calculateColumnWidths();

before reading the column widths.