I\'m trying to set the width of a cell in an Excel document generated with PHPExcel with:
$objPHPExcel->getActiveSheet()->getColumnDimensionByColumn(\'C\')
It's a subtle difference, but this works fine for me:
$objPHPExcel->getActiveSheet()->getColumnDimension('A')->setWidth(10);
Notice, the difference between getColumnDimensionByColumn
and getColumnDimension
Also, I'm not even setting AutoSize and it works fine.
setAutoSize method must come before setWidth:
$objPHPExcel->getActiveSheet()->getColumnDimensionByColumn('C')->setAutoSize(false);
$objPHPExcel->getActiveSheet()->getColumnDimensionByColumn('C')->setWidth('10');
autoSize for column width set as bellow. It works for me.
$spreadsheet->getActiveSheet()->getColumnDimension('A')->setAutoSize(true);
This worked for me:
$objPHPExcel->getActiveSheet()->getColumnDimension('C')->setAutoSize(false);
$objPHPExcel->getActiveSheet()->getColumnDimension('C')->setWidth(10);
be sure to add setAuzoSize(false)
, before the setWidth();
as Rolland mentioned
The correct way to set the column width is by using the line as posted by Jahmic, however it is important to note that additionally, you have to apply styling after adding the data, and not before, otherwise on some configurations, the column width is not applied
Tha is because getColumnDimensionByColumn receives the column index (an integer starting from 0), not a string.
The same goes for setCellValueByColumnAndRow