82

I'm trying to set the width of a cell in an Excel document generated with PHPExcel with:

$objPHPExcel->getActiveSheet()->getColumnDimensionByColumn('C')->setWidth('10');
$objPHPExcel->getActiveSheet()->getColumnDimensionByColumn('C')->setAutoSize(false);

but that does not works.

What is the method that I need to call here?

James John McGuire 'Jahmic'
  • 11,728
  • 11
  • 67
  • 78
user198003
  • 11,029
  • 28
  • 94
  • 152

8 Answers8

200

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.

Muhammad Hassaan
  • 7,296
  • 6
  • 30
  • 50
James John McGuire 'Jahmic'
  • 11,728
  • 11
  • 67
  • 78
  • How can I set the wordwrap property to particular column. It would be useful for me if you know. Thanks! – Talk2Nit Dec 01 '14 at 07:02
  • 5
    It's been a while since I have worked with PHPExcel, but I believe you want 'setWrapText'. example: $objPHPExcel->getActiveSheet()->getStyle('A6')->getAlignment()->setWrapText(true); – James John McGuire 'Jahmic' Dec 02 '14 at 04:00
  • Jahmic is right. AutoSize is set to false on all columns by default, so it's not necessary to do it with code. – imelgrat Jun 06 '18 at 20:12
13

setAutoSize method must come before setWidth:

$objPHPExcel->getActiveSheet()->getColumnDimensionByColumn('C')->setAutoSize(false);
$objPHPExcel->getActiveSheet()->getColumnDimensionByColumn('C')->setWidth('10');
Rolland
  • 679
  • 5
  • 10
  • 2
    AutoSize:false is required in order to set a width – user3711851 Sep 13 '16 at 10:18
  • 2
    getColumnDimensionByColumn() expects a 0-based column index, not a column name. (from the Worksheet class: "@param string $pColumn Numeric column coordinate of the cell") – userfuser Aug 11 '17 at 04:43
9

hi i got the same problem.. add 0.71 to excel cell width value and give that value to the

$objPHPExcel->getActiveSheet()->getColumnDimension('A')->setWidth(10);

eg: A Column width = 3.71 (excel value)

give column width = 4.42

will give the output file with same cell width.

$objPHPExcel->getActiveSheet()->getColumnDimension('A')->setWidth(4.42);

hope this help

Rizier123
  • 58,877
  • 16
  • 101
  • 156
King Alawaka
  • 521
  • 4
  • 3
8

autoSize for column width set as bellow. It works for me.

$spreadsheet->getActiveSheet()->getColumnDimension('A')->setAutoSize(true);
Du Luong
  • 124
  • 1
  • 4
5

Tha is because getColumnDimensionByColumn receives the column index (an integer starting from 0), not a string.

The same goes for setCellValueByColumnAndRow

shurbks
  • 51
  • 1
  • 1
5

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

Magor Menessy
  • 381
  • 4
  • 13
2

This way is much easier to adjust the size of the columns automatically.

foreach (range('A', 'I') as $letra) {            
            $spreadsheet->getActiveSheet()->getColumnDimension($letra)->setAutoSize(true);
}
1

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

Go0se
  • 98
  • 1
  • 8