Home » Php » php – phpExcel column size issues

php – phpExcel column size issues

Posted by: admin July 12, 2020 Leave a comment


I want to make sure that no value is cut off in a workbook produced by phpExcel. To do this I call


every time I write a cell.
This doesn’t seem to work: Many columns end up really tiny.

Does anybody know how to make sure every column in the outputted excel workbook is sized according to its content?

How to&Answers:

Don’t do this every time you write a cell… it’s an unnecessary overhead. Do it just once for each column.

Be warned, it’s still expensive when saving, because PHPExcel then walks through each cell in that column working out the width necessary based on the content of that cell, taking into account formula calculation and formatting, and using the maximum size of any cell in that column. Even then it isn’t a perfect solution: the default behaviour is to estimate, counting the number of characters to display, and using a generic multiplier based on font (Arial, Verdana of Calibri) and a little bit of padding. For a more accurate calculation, you can change the method used for the calculation, and the code then uses GD to build a ttf bounding box and base the cell size on the dimensions of that box. This is a more accurate calculation, but a lot slower.

You can change the calculation method using:


before saving, where $method is one of the following:


This still isn’t a perfect solution, but it’s the best calculation that PHPExcel can do.

An alternative is to call the worksheet’s calculateColumnWidths() method yourself directly before saving… and then walk through each column’s dimension increasing the value by perhaps 5% to try and ensure that the entire column fits, before saving.