How to autosize row height ?

Topics: Developer Forum, User Forum
Aug 18, 2009 at 12:52 PM

Hi

We looked at the documentation, but could not find a way to auto-size a row.

Any help is welcome..

Kind regards

 

Developer
Aug 18, 2009 at 5:14 PM

The short answer is that you can explicitly set auto-size on a row like this:

// auto-size on row 1
$objWorksheet->getRowDimension('1')->setRowHeight(-1);

Normally you don't have to do this because that is the default value.

The long answer is that row heights are a mess in Excel and many things are happening behind the scenes when you alter the text wrap properties etc. It will be hard to have PHPExcel to reflect this. Moreover, PHPExcel is not yet able to tell you what the actual row height (in points/pixels) is for those auto-sized rows.

Aug 19, 2009 at 7:13 AM

Thank you for your answer.

We tried your code on our document, but it doesn't work.

 

Developer
Aug 19, 2009 at 10:38 PM

Can you say whether the problem occurs with PHPExcel_Writer_Excel5 or PHPExcel_Writer_Excel2007 ? Do you have some sample code so we can reproduce this?

Aug 21, 2009 at 7:29 AM

Here is a generated file : http://www.2shared.com/file/7292998/d45602ac/2003_autosize.html

The autosize feature works when we export the file in 2007 format, but not 2003 (using PHPExcel_Writer_Excel5).

One of the cell in the autosized row is merged with two cells below. removing the merge code didn't help.

 

code :

 

const XL_CC_LABEL_ROW = 3;
const XL_2003_FORMAT = 2003;
const XL_2007_FORMAT = 2007;
[...]
$sheet->getRowDimension(self::XL_CC_LABEL_ROW)->setRowHeight(-1);
[...]
$filename = '';
$objWriter = null;


switch ($fileFormat){
  case self::XL_2003_FORMAT :
    $filename = $name.'.xls';
    $objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5');
    header('Content-Type: application/vnd.ms-excel');
    header('Content-Disposition: attachment;filename="'.$filename.'"');
    break;
  case self::XL_2007_FORMAT :
    $filename = $name.'.xlsx';
    $objWriter = new PHPExcel_Writer_Excel2007($objPHPExcel);
    //$objWriter->setPreCalculateFormulas(false);
    //$objWriter->setOffice2003Compatibility(true);
    header('Content-Type: application/vnd.openxmlformats-officedocument.'.
      'spreadsheetml.sheet');
    header('Content-Disposition: attachment;filename="'.$filename.'"');

    break;
  default:dieParamError("unknown file format");
}

header('Cache-Control: max-age=0');

$objWriter->save('php://output');

 

 

Developer
Aug 21, 2009 at 7:53 AM

Ok, thanks for the info. This is what I feared.

I think it is an inherited problem from PEAR::Spreadsheet_Excel_Writer (which PHPExcel_Writer_Excel5 is based on) not dealing correctly with some row option flags. I will look at it more deeply tomorrow and create the necesasry work item. Will post back here.

Developer
Aug 21, 2009 at 8:51 PM

Fixed per work item 10503:

http://phpexcel.codeplex.com/WorkItem/View.aspx?WorkItemId=10503

Download next source code release tomorrow:

http://phpexcel.codeplex.com/SourceControl/ListDownloadableCommits.aspx