how to set row height to be computed automatically?

Topics: Developer Forum
Nov 26, 2009 at 12:08 AM

I have some troubles with setting automatic height to the row where is a long text. I am of course using text wrapping

If i dont specify row height, only one row is shown, but if specify fixed row height then not all the text in the cell is shown in case of the text is longer then row height

My point is to display all text inside the cell even if the text has i.e. 20 lines. The problem is that I dont know how many lines of text will be displayed...



$style = $sheet->getStyleByColumnAndRow($pColumn, $pRow);
            $sheet->mergeCells("B$pRow:".chr(64 + $table_width)."$pRow");
            $objRichText = new PHPExcel_RichText( $sheet->getCellByColumnAndRow($pColumn, $pRow));


$sheet->getColumnDimension(chr(65 + $pColumn))->setWidth(100); // fixed width
$style = $sheet->getStyleByColumnAndRow($pColumn, $pRow);
$style->getAlignment()->setWrapText(true); // text wrapping enabled
$sheet->mergeCells("A$pRow:".chr(64 + $table_width)."$pRow");
$objRichText = new PHPExcel_RichText( $sheet->getCellByColumnAndRow($pColumn, $pRow));
$objRichText->createText("really long text ....".$text); 



// $sheet->getRowDimension($pRow)->setRowHeight(100); // I dont know the height! I want display all the content 

The default height is -1 and writer of course ignores this value and only one row of text is shown, I wanna see the text wrapped

I tried 1.7.1, nightbuild, Excel5 and Excel2007 with the same result (In HTML it looks as I expect)

Nov 26, 2009 at 6:12 PM

Well I have discovered, that this is a bug in Excel. If you have richtext in merged cells, text wrapping does not work. Solution: you can not merge columns :(

Nov 27, 2009 at 2:50 AM

Yes, unfortunately this is a problem with Excel itself.

Merged cells + wrap text + autofit row height = not working

Dec 28, 2009 at 7:43 PM
Edited Dec 28, 2009 at 8:53 PM

I have experienced this too.  I had a couple of ideas for work-arounds, but not sure if they are possible.

1)  Is there a way to get the resulted height from an auto-wrap?  The idea would be to set height to -1, see what the resultant height and then explicitely set this before wrapping the text.

2)  It seems that in word if you do auto-height THEN do merge it works fine (but doesn't work in reverse order), so I don't know if the PHP excel writer can perform the same basic actions.

 Again, not sure if either of these would work, but wanted to put my 2 cents in.

 Also, here is a quick and dirty snippet I wrote to auto-size a row height (for merged rows).  It probably doesn't work for every situation, but it works for mine :)

//These 2 variables will depend on text size, font family, etc
$row_chars = 68; //Rough idea of how many characters it takes to wrap to a new line in the merged cell
$one_row = 13;  //Row height of one line of text

$cv_ar = explode("\n", $cell_value);
$cv_rows = 0;
if(count($cv_ar) > 1){
 foreach($cv_ar AS $cv_row){
   $cv_rows += ceil((st($cv_row)/$row_chars));
elseif(st($cell_value) > $row_chars){
 $cv_rows += ceil((st($cv_row)/$row_chars));
if($cv_rows > 0){
 $use_height = $cv_text_rows * $one_row;



Jan 17, 2010 at 10:41 AM


I follow what you are saying. It will be difficult to get the "resulted height from an auto-wrap". It depends on fonts, font sizes etc. which is what complicates matters.

We already have some experimental code to determine exact auto-size column width based on font, font size etc. So "resulted height from an auto-wrap" may come one day, but requires some work.

Jan 1, 2012 at 1:29 PM

My 2 cents: as wrapping seems not to be working with RichText what I did was to add text as: chr(13).chr(10)