If a column is set to AutoSize, PHPExcel attempts to calculate the column width based on the calculated value of the column (so on the result of any formulae), and any additional characters added by format masks such as thousand separators.
By default, this is an estimated
width: a more accurate calculation method is available, based on using GD, which can also handle font style features such as bold and italic; but this is a much bigger overhead, so it is turned off by default. You can enable the more accurate calculation using
PHPExcel_Shared_Font::setAutoSizeMethod(PHPExcel_Shared_Font::AUTOSIZE_METHOD_EXACT);
However, autosize doesn't apply to all Writer formats... for example CSV. You don't mention what writer you're using.
But you also need to identify the columns to set dimensions:
foreach(range('B','G') as $columnID) {
$objPHPExcel->getActiveSheet()->getColumnDimension($columnID)
->setAutoSize(true);
}
$objPHPExcel->getActiveSheet()->getColumnDimension()
expects a column ID.
$objPHPExcel->getActiveSheet()->getColumnDimensions()
will return an array of all the defined column dimension records; but unless a column dimension record has been explicitly created (perhaps by loading a template, or by manually calling getColumnDimension()
) then it won't exist (memory saving).
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…