How to set auto height in phpexcel?

How to set auto height in phpexcel?

  1. Blog
  2. 15 November 2026
  3. 3 min read

Stefan Bogdanescu

Founder & Senior Architect

How to Set Auto Height in PHPExcel

You cannot simply call a method like setAutoHeight(true) on a row object within the legacy PHPExcel library. The architecture of PHPExcel, which relies heavily on PHPExcel_Worksheet_Row and PHPExcel_Style, separates row properties from cell content analysis. By default, when you iterate through cells to set values, PHPExcel assumes a standard height unless explicitly told otherwise. If your rows appear compressed or misaligned in the exported .xlsx file, it is almost certainly because the library has not calculated the required pixel height based on the font size and line wrapping of the content.

To achieve auto-height behavior, you must manually invoke the calculation logic provided by the PHPExcel_Worksheet class. The specific method responsible for this is autoSizeCells(), but there is a critical nuance: this method only works if the cells actually contain data or explicit formatting that dictates their size.

If you are working with large datasets, calling autoSizeCells() on every single row can be computationally expensive because it requires traversing the entire worksheet to measure content width and height for every cell involved. A more performant approach is to target only the rows containing merged cells or specific text that requires vertical expansion. For standard text without line breaks, relying on the default minimum row height configured in the getDefaultRowDimension() method is often sufficient.

Here is a practical implementation pattern using the legacy PHPExcel class structure. First, ensure you have loaded your data. Then, iterate through your rows and apply the auto-sizing logic selectively:

use PhpOffice\PhpExcel\Worksheet;

$sheet = $objPHPExcel->getActiveSheet();

// Optional: Set a sensible default row height to prevent extreme compression
$sheet->getDefaultRowDimension()->setRowHeight(15); 

// Loop through specific rows or columns that need adjustment
foreach ($sheet->getRowIterator(true) as $row) {
    $cellIterator = $row->getCellIterator();
    $cellIterator->setIterateOnlyExistingCells(false);

    foreach ($cellIterator as $cell) {
        // Check if the cell is empty or a merged cell
        if (!$cell->getValue() && !$cell->isMerged()) {
            continue; 
        }
        
        // Trigger auto-sizing for this specific row
        // Note: This calculates height based on current font and content
        $sheet->autoSizeColumn($cell->getColumn());
    }
}

// If you specifically need dynamic row height (not just column width),
// you must set the row dimension manually after determining the max height.
// However, PHPExcel does not expose a direct "calculateRowHeight" method 
// that returns the pixel value for immediate assignment without side effects.

The code snippet above demonstrates a common pitfall: developers often expect autoSizeColumn to affect row height directly. In reality, this method optimizes column width. To adjust row height dynamically, you must manually set the row dimension based on the calculated requirements or rely on the library's internal logic when exporting.

A more robust solution for dynamic row heights involves setting the row dimension explicitly after ensuring the content is present. You can access the rowDimension object of a specific row and set its height:

$sheet->getRowDimension('1')->setRowHeight(20); // Manual override if auto-calc fails

However, true "auto-height" requires the library to measure the bounding box of the text. In many cases, simply calling autoSizeColumn on the row containing the text is insufficient because the row height is managed separately from column width. The most reliable method in PHPExcel is to ensure your cells are formatted with a font that supports line breaks if you intend for multi-line text to expand the row automatically.

If you find that manual height setting becomes unmanageable due to varying content lengths, consider switching to modern alternatives like PhpSpreadsheet. The legacy PHPExcel project was archived on CodePlex years ago and no longer receives security updates or feature requests. The active community has moved to phpoffice/phpspreadsheet, which offers a more intuitive API for these exact scenarios. In the newer library, you can use the setAutoFilter(true) or specific row dimension methods that handle content measurement more predictably across different Excel versions.

When exporting your file using IOFactory::createWriter(), ensure that the cellXf and style information are preserved correctly. Sometimes, auto-height settings calculated in memory do not persist if the writer encounters compatibility issues with older Excel versions or specific XML formatting constraints. Always test your generated files across different office suites to verify that row heights render as expected.

Our network