How to convert Excel XLS to CSV using PHP

How to convert Excel XLS to CSV using PHP

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

Stefan Bogdanescu

Founder & Senior Architect

How to convert Excel XLS to CSV using PHP

Converting an Excel spreadsheet to a CSV file in PHP requires handling binary data streams and understanding the underlying format differences. The legacy .xls format is distinct from the modern .xlsx standard, and mixing them up often leads to parsing errors or incomplete data extraction. Before writing a single line of code, you must ensure your environment has the necessary libraries installed, as PHP's core does not include native Excel readers.

The most straightforward approach relies on the PHPSpreadsheet library. This tool is a port of Apache POI and provides a robust interface for reading and manipulating spreadsheet files. To get started, you need to install the package via Composer. Run the following command in your project directory:

composer require phpspreadsheet/phpspreadsheet

Once installed, you can instantiate the library using the IOFactory. This factory pattern is crucial because it automatically detects the file extension and selects the appropriate reader class. If you attempt to use a generic reader on an .xls file, it will fail silently or throw exceptions. You must explicitly load the legacy XLS reader for files ending in .xls:

use PhpOffice\PhpSpreadsheet\IOFactory;

$filename = 'documents.xls';
$inputFile = new \PhpOffice\PhpSpreadsheet\Reader\Xls();
$data = $inputFile->load($filename);

After loading the data, you iterate through the rows and columns to build your CSV content. The getActiveSheet() method retrieves the active worksheet, which is usually where the data resides. You can then loop through each row and column, retrieving cell values using the $cell->getValue() method. It is important to handle different cell types; numeric cells might contain floats, while text cells are strings. Additionally, empty cells should be treated as empty strings to maintain CSV integrity.

$writer = new \PhpOffice\PhpSpreadsheet\Writer\Csv($data);
$outputFile = 'documents.csv';
$writer->save($outputFile);

Using the Writer class simplifies the process significantly compared to manually constructing a file with fopen. The writer handles encoding, BOM insertion, and proper quoting of fields containing commas or newlines automatically. This prevents malformed CSV files that break standard parsers.

A common pitfall occurs when dealing with large datasets. Loading an entire workbook into memory works fine for small lists of documents, but if the spreadsheet contains millions of rows, you might hit memory limits. In such scenarios, a streaming approach is necessary. However, PHPSpreadsheet's primary design focuses on in-memory manipulation. For massive files, consider processing the file in chunks or switching to a lower-level parser like php-read-xls which offers more granular control but requires manual error handling for binary parsing issues.

Another critical consideration is the encoding of the output file. By default, the CSV writer uses UTF-8 without a BOM (Byte Order Mark). If you intend to open this file directly in Microsoft Excel or LibreOffice on Windows, they often expect a UTF-8-SF (UTF-8 with BOM) file for proper character rendering, particularly for non-ASCII characters. You can force the inclusion of a BOM by setting $writer->setUseBOM(true) before saving.

If you are building a larger application where this conversion is part of a data pipeline, integrating this logic into a Laravel controller or service class makes sense. The phpspreadsheet package is widely compatible with the Laravel ecosystem, allowing you to leverage existing dependency management and testing frameworks. For more complex requirements involving complex formatting preservation or macro retention, note that CSV inherently loses formatting; it is purely data-centric. If your "list of documents" includes merged cells or specific styling that must be preserved for downstream processing, a simple CSV conversion might not meet the business requirement without additional logic to encode metadata into custom columns.

Our network