What is the best way to create XLS file in PHP

What is the best way to create XLS file in PHP

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

Stefan Bogdanescu

Founder & Senior Architect

What is the best way to create XLS file in PHP

Many developers start by assuming that creating an .xls file is as simple as setting the HTTP header to application/vnd.ms-excel and echoing comma-separated values. While this trick works for rudimentary data export, it produces a CSV file with an incorrect extension, not a true Microsoft Excel workbook. This distinction matters because CSVs lack formatting, merged cells, formulas, and rich styling capabilities that users expect from an .xls or .xlsx file.

If your goal is simply to generate a spreadsheet that Excel can open without requiring external libraries, the most straightforward approach is to use HTML tables. When you set the Content-Type header to application/vnd.ms-excel, browsers and Excel interpret the response as a worksheet. You can output standard HTML <table> elements with CSS styling, images, and even simple JavaScript for interactivity. Excel will render this HTML directly into its cells.

However, relying solely on HTML has limitations. It does not support complex calculations, advanced filtering, or specific Excel features like data validation. Furthermore, the resulting file is technically an HTML document with an .xls extension, which can cause issues in enterprise environments that strictly validate file formats. This method is best suited for lightweight reports where visual presentation outweighs functional complexity.

For scenarios requiring genuine spreadsheet functionality—such as writing formulas, defining named ranges, or creating pivot tables—you need a dedicated library. Historically, the PHPExcel library was the de facto standard for PHP developers needing to generate .xls files. It offered robust support for both the older BIFF8 format (.xls) and the newer OOXML format (.xlsx). While the original PHPExcel project has been deprecated in favor of its successor, PhpSpreadsheet, the logic remains relevant for understanding the ecosystem.

Using a library like PhpSpreadsheet allows you to instantiate a Spreadsheet object, create sheets, define cells with values and styles, and then save the file to disk or force a download. This approach ensures that the generated file is a true workbook compatible with Excel's expectations. Here is a conceptual example of how a library-based solution structures the output:

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

$spreadsheet = new Spreadsheet();
$sheet = $spreadsheet->getActiveSheet();
$sheet->setCellValue('A1', 'Name');
$sheet->setCellValue('B1', 'Value');
$sheet->setCellValue('A2', 'Item 1');
$sheet->setCellValue('B2', 100);

$writer = new Xlsx($spreadsheet);
$filename = 'report.xlsx';
$writer->save($filename);

If you are constrained to avoiding third-party libraries entirely, as suggested in some legacy discussions, the HTML table method remains your only viable option. You would extend your existing class to output an HTML view that mimics Excel's grid structure. This involves setting headers like Content-Type: application/vnd.ms-excel, adding cache-control directives to prevent caching, and ensuring the file is downloaded via a forced filename parameter in the response.

A common pitfall when handling Excel files in PHP is ignoring the difference between .xls and .xlsx. The original .xls format (BIFF8) has strict size limits and memory constraints compared to the newer .xlsx format, which uses compressed XML. Modern libraries often default to .xlsx, which is generally preferred for compatibility with recent versions of Excel. If you must support the older .xls format specifically, you need to ensure your library explicitly targets that output type.

In a Laravel application, generating these files often involves defining custom routes or controller methods that trigger the download event. You might place this logic in a dedicated service class to keep your controllers clean. Whether you choose the HTML workaround for simplicity or integrate a full-fledged library like PhpSpreadsheet for feature richness, the decision hinges on whether you need Excel's formula engine or just a structured data export.

For teams already using Laravel, checking out resources from https://laravelcompany.com can provide deeper insights into building robust file handling services that scale with your application's needs.

Our network