Set spreadsheet cell width using PHPExcel

I am trying to set the cell width in an Excel document generated by PHPExcel using:

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

but it does not work.

What is the method I need to call here?

+57
php phpexcel phpspreadsheet
Jul 22 2018-11-11T00:
source share
7 answers

This is a subtle difference, but it works fine for me:

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

Note that the difference between getColumnDimensionByColumn and getColumnDimension

Also, I don't even install AutoSize, and it works great.

+143
Aug 13 '11 at 11:45
source share

The setAutoSize method should appear before setWidth:

 $objPHPExcel->getActiveSheet()->getColumnDimensionByColumn('C')->setAutoSize(false); $objPHPExcel->getActiveSheet()->getColumnDimensionByColumn('C')->setWidth('10'); 
+9
Oct. 16 '15 at 12:52
source share

Hi, I have the same problem. add 0.71 to the excel cell width value and set this value to

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

for example: Column width = 3.71 (excel value)

specify column width = 4.42

will provide an output file with the same cell width.

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

hope this help

+8
Feb 08 '15 at 21:05
source share

Tha is because getColumnDimensionByColumn gets the index of the column (an integer starting at 0), not the row.

The same goes for setCellValueByColumnAndRow

+5
Nov 05 '15 at 19:35
source share

autoSize for the column width set below. It works for me.

 $spreadsheet->getActiveSheet()->getColumnDimension('A')->setAutoSize(true); 
+2
Mar 15 '18 at 7:31
source share

The correct way to set the column width is to use a string published by Jahmic, however it is important to note that you need to apply the style after adding the data, and not earlier, otherwise the column width is not applied on some configurations

+1
Aug 14 '14 at 8:32
source share

This worked for me:

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

be sure to add setAuzoSize(false) , before setWidth(); that Rolland mentioned

+1
Dec 22 '17 at
source share



All Articles