1

I'm generating Excel files w/ php using the library PHP_Excel, I don't have any issues except with the autosize properties, here's my code that doesn't work :

$wb = new PHPExcel();
$sheet = $wb->getActiveSheet();

PHPExcel_Shared_Font::setAutoSizeMethod(PHPExcel_Shared_Font::AUTOSIZE_METHOD_EXACT);

foreach(range("A", "U") as $col){
    $sheet->getColumnDimension($col)->setAutoSize(true);
}

(I rearranged it from the answer of this topic)

Although, I'm getting a php error inside the generated file : Error

Is there a better way to properly autosize columns ?

Natty
  • 497
  • 1
  • 11
  • 23

1 Answers1

0

If the default "estimated" method isn't good enough for you, then the exact method needs access to the truetype fonts (including bold, italic, etc) that you're using so that it can do the calculation... of course, it's also a great deal slower because it has to do such a complex calculation for every cell in the columns that you're autosizing, basically building an image containing the text styled according to the font details, and then measuring that image. It's far easier and quicker if you use the estimated method rather than exact

Mark Baker
  • 209,507
  • 32
  • 346
  • 385