In case somebody was looking for this.
The resolution below also works on PHPSpreadsheet
, their new version of PHPExcel.
// assuming $spreadsheet is instance of PhpOffice\PhpSpreadsheet\Spreadsheet
// assuming $worksheet = $spreadsheet->getActiveSheet();
foreach(range('A',$worksheet->getHighestColumn()) as $column) {
$spreadsheet->getColumnDimension($column)->setAutoSize(true);
}
Note:
getHighestColumn()
can be replaced withgetHighestDataColumn()
or the last actual column.
What these methods do:
getHighestColumn($row = null)
- Get highest worksheet column.
getHighestDataColumn($row = null)
- Get highest worksheet column that contains data.
getHighestRow($column = null)
- Get highest worksheet row
getHighestDataRow($column = null)
- Get highest worksheet row that contains data.