Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Setting width of spreadsheet cell using PHPExcel

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?

like image 905
user198003 Avatar asked Jul 22 '11 10:07

user198003


People also ask

How do I add a new line in a cell in Excel using PHP?

Write a newline character "\n" in a cell (ALT+"Enter") In Microsoft Office Excel you get a line break in a cell by hitting ALT+"Enter".

How do I change font size in PHPExcel?

$objPHPExcel->getActiveSheet()->getStyle("F1:G1")->getFont()->setFontSize(16);

Is PHPExcel deprecated?

PHP Excel was officially deprecated in 2017 and permanently archived in 2019. PHP Excel has not be maintained for years and must not be used anymore.


8 Answers

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.

like image 193
Jahmic Avatar answered Oct 11 '22 13:10

Jahmic


setAutoSize method must come before setWidth:

$objPHPExcel->getActiveSheet()->getColumnDimensionByColumn('C')->setAutoSize(false);
$objPHPExcel->getActiveSheet()->getColumnDimensionByColumn('C')->setWidth('10');
like image 32
Rolland Avatar answered Oct 11 '22 11:10

Rolland


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

like image 31
King Alawaka Avatar answered Oct 11 '22 11:10

King Alawaka


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

The same goes for setCellValueByColumnAndRow

like image 40
shurbks Avatar answered Oct 11 '22 12:10

shurbks


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

$spreadsheet->getActiveSheet()->getColumnDimension('A')->setAutoSize(true);
like image 33
Du Luong Avatar answered Oct 11 '22 12:10

Du Luong


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

like image 21
Magor Menessy Avatar answered Oct 11 '22 13:10

Magor Menessy


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);
}
like image 27
Carlos Alberto Gonzalez Avatar answered Oct 11 '22 13:10

Carlos Alberto Gonzalez


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

like image 45
Go0se Avatar answered Oct 11 '22 11:10

Go0se