Create Xlsx Files With Row Height Settings




Dynamically set a row height of xlsx files in PhpSpreadsheet, a handy way to compress your data vertically.


  • Composer
  • PHP 7.2 or newer

Step 1.

Setup dependencies.

    "require": {
        "phpoffice/phpspreadsheet": "^1.3"

Step 2.

Install phpspreadsheet.

$ composer install

Step 3.

Create a new PHP file, and start coding.


// Autoload dependencies
require 'vendor/autoload.php';

// Import the core class of PhpSpreadsheet
use PhpOffice\PhpSpreadsheet\Spreadsheet;

// Import the Xlsx writer class
use PhpOffice\PhpSpreadsheet\Writer\Xlsx;

// Create a new Spreadsheet object
$spreadsheet = new Spreadsheet();

// Retrieve the current active worksheet
$sheet = $spreadsheet->getActiveSheet();

 * Loop through 'Column A - D'.
foreach (range('A', 'D') as $letter) {
    // Set some data to 'Column A - D' of 'Row 5'
    $sheet->setCellValue($letter . '5', 'Column ' . $letter);

// Set the height of 'Row 5' to 75 pt.

// Write a new .xlsx file
$writer = new Xlsx($spreadsheet);

// Save the new .xlsx file


Run the following codes.

$ php create-xlsx-files-with-row-height-settings.php


Open the generated file create-xlsx-files-with-row-height-settings.xlsx.

“If you want to set a row height using a different unit of measure, then you can do so by telling PhpSpreadsheet what UoM the height value that you are setting is measured in. Valid units are pt (points), px (pixels), pc (pica), in (inches), cm (centimeters) and mm (millimeters).”

// Set the height of 'Row 5' to 75 px.
$spreadsheet->getActiveSheet()->getRowDimension('5')->setRowHeight(75, 'px');

Check row height.

  • 1. Click row 5.
  • 2. Click ‘Home’.
  • 3. Click ‘Format’.
  • 4. Select ‘Row Height’.
  • 5. The ‘Row Height’ value is now visible.

Another way of checking the row height.

  • 1. Right click on row 5.
  • 2. Select ‘Row Height’.
  • 3. The ‘Row Height’ value is now visible.






Leave a Reply

Your email address will not be published. Required fields are marked *

Latest Tutorials

Web Dev Tutorials