How to generate an Excel document with multiple worksheets from PHP?

How to generate an Excel document with multiple worksheets from PHP?

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

Stefan Bogdanescu

Founder & Senior Architect

How to generate an Excel document with multiple worksheets from PHP?

Generating a single-sheet Excel file in PHP is straightforward if you stick to standard headers, but creating a workbook with multiple worksheets requires more than just setting Content-Type: application/vnd.ms-excel. The raw HTTP headers provided in common tutorials handle the MIME type and filename attachment correctly, but they offer zero control over the internal structure of the spreadsheet. To include multiple sheets, you need to construct the binary OpenXML format manually or, more pragmatically, leverage a third-party library.

The native PHP streams API can write the necessary ZIP archive containing the XML files that make up an .xlsx file, but this approach demands a deep understanding of the Office Open XML schema. You must manually create the _rels/.rels, xl/workbook.xml, and individual xl/worksheets/sheet1.xml files. While possible, maintaining the correct byte-order marks, encoding namespaces, and ZIP compression ratios for every new sheet adds significant complexity. For production environments, relying on a dedicated library is almost always the superior choice to avoid subtle bugs related to file formatting.

The most robust solution involves using PhpSpreadsheet, a modern fork of PHPSpreadsheet that actively supports multiple worksheets out of the box. Unlike older libraries like PEAR Spreadsheet_Writer_Excel (which often produce .xls files with limited multi-sheet support or require complex class hierarchies), PhpSpreadsheet generates standard .xlsx files using the XML namespace approach described above but abstracts the complexity away from you.

Here is how you can structure a workbook with multiple sheets using the Composer-installed phpoffice/phpspreadsheet:

composer require phpoffice/phpspreadsheet

Once installed, the workflow shifts from managing HTTP headers to managing worksheet objects. You instantiate the Spreadsheet class and then call createSheet() for each new table you need. This method returns a reference to the active sheet or allows you to set it later using setActiveSheetIndex().

use PhpOffice\PhpSpreadsheet\Spreadsheet;
use PhpOffice\PhpSpreadsheet\Writer\Xlsx;

$spreadsheet = new Spreadsheet();

// Create multiple worksheets
$sheet1 = $spreadsheet->createSheet('Sales');
$sheet2 = $spreadsheet->createSheet('Inventory');
$sheet3 = $spreadsheet->createSheet('Shipping');

// Set the active sheet to be the first one by default
$spreadsheet->setActiveSheetIndex(0);

// Populate data on specific sheets
$sheet1->setCellValue('A1', 'Q1 Revenue');
$sheet2->setCellValue('A1', 'Stock Levels');

// Write to file
$writer = new Xlsx($spreadsheet);
$filename = 'multi_sheet_report.xlsx';

header("Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
header("Content-Disposition: attachment;filename=$filename");
header("Cache-Control: max-age=0"); // Prevent caching issues in browsers

$writer->save($filename);

A critical detail often overlooked when generating Excel files programmatically is the Content-Disposition header. While application/vnd.ms-excel works for legacy .xls files, modern browsers expect application/vnd.openxmlformats-officedocument.spreadsheetml.sheet for .xlsx files to trigger the correct download behavior without displaying a warning dialog. Failing to set this correctly can result in users seeing a file extension mismatch or being blocked by strict security filters.

Another consideration is memory usage. Loading large datasets into memory before writing them to the worksheet object can consume significant RAM, especially if you have many sheets with thousands of rows each. For massive reports, you might consider streaming the output directly to PHP_OUTPUT_BUFFER instead of saving to a temporary file path, though this requires ensuring the output buffer is not flushed prematurely by other parts of your application.

If you are building a larger application using Laravel, packages like maatwebsite/excel provide an elegant facade for this exact use case, allowing you to define custom Excel readers and writers with multi-sheet support through simple class definitions. However, if you are outside the Laravel ecosystem or require low-level control over the XML generation, sticking to the core PhpSpreadsheet classes remains the industry standard for reliability and feature parity.

Our network