Trouble with setAutoSize of Column

Topics: Developer Forum, User Forum
Dec 30, 2010 at 6:42 PM

The trouble appear when I setup the font type "Arial", in this case the column width is smaller than the length of string  in the cell. Instead, with font type curier for example, the autoSize  apparently work fine, in really the width is bigger than the length of string  in the cell and then the text is showed completly.
I am working wrong or the problem is on PHPExcel Class?


$objPHPExcel->getActiveSheet()->getDefaultStyle()->getFont()->setName('Arial'); // Curier  or Calibri work fine


for ($col=0; $col<$sql_num_fields; $col++){

          $objPHPExcel->getActiveSheet()->setCellValue(chr($col+$base_col).$row, utf8_encode(mysql_field_name($rowset,$col)));

          $objPHPExcel->getActiveSheet()->getColumnDimension(chr($col+$base_col))->setAutoSize(true); //Ancho de Columna Automático
























Jan 3, 2011 at 11:05 PM

It's certainly possible that there is a problem in PHPExcel. If the PHPExcel_Shared_Font autoSizeMethod property is set to PHPExcel_Shared_Font::AUTOSIZE_METHOD_EXACT then PHPExcel will use the GD library to calculate an accurate width for an autosize column; but this is slow, and generally not recommended for performance reasons. The default method is PHPExcel_Shared_Font::AUTOSIZE_METHOD_APPROX, which calculates estimates for Calibri, Arial or Verdana (defaulting to the Calibri calculation). For Arial, the method uses a generic estimated character width of 7 pixels for a 10pt font size multiplied by the number of characters, (adjusted up or down based on the actual point size).

$columnWidth = (int) (7 * PHPExcel_Shared_String::CountCharacters($columnText));
$columnWidth = $columnWidth * $fontSize / 10; // extrapolate from font size

Then adds a small margin (based on the width of a single additional character) before converting from pixels to Excel's column width units.


Naturally, this can only ever be an estimate for proportional-spaced fonts such as Arial.