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"
}
}
composer.json
Step 2.
Install phpspreadsheet.
$ composer install
command line
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');
create-xlsx-files-with-hidden-rows.php
Test.
Run the following codes.
$ php create-xlsx-files-with-hidden-rows.php
command line
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.
Leave a Reply