Create Xlsx Files With Hidden Rows

| March 13, 2022 | 669 views | PhpSpreadsheet

Hide rows of xlsx files in PhpSpreadsheet, a useful procedure to keep out of sight the not-very-important data.

Requirements:

  • 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.

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);
}

// Hide 'Row 3'.
$spreadsheet->getActiveSheet()->getRowDimension('3')->setVisible(false);

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

// Save the new .xlsx file
$writer->save('create-xlsx-files-with-hidden-rows.xlsx');

Test.

Run the following codes.

$ php create-xlsx-files-with-hidden-rows.php

Result.

Open the generated file create-xlsx-files-with-hidden-rows.xlsx.

Check row height.

  • 1. Select the hidden row.
  • 2. Click ‘Format’.
  • 3. Click ‘Hide & Unhide’.
  • 4. Select ‘Unhide Rows’.
  • 5. ‘Row 3’ is now visible.

References:

0 Comments

Leave a Reply

Your email address will not be published.