Read XLS in PHP using PhpSpreadsheet

Read XLS in PHP using PhpSpreadsheet

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

Stefan Bogdanescu

Founder & Senior Architect

Read XLS in PHP using PhpSpreadsheet

The error you are encountering stems from a subtle but critical distinction between file formats that often trips up developers migrating from legacy libraries. When IOFactory::load() successfully processes an .xlsx file but fails on an .xls file, the issue is almost never a bug in PhpOffice\PhpSpreadsheet. Instead, it indicates that the factory did not dispatch to the correct reader class for the binary format you provided.

The PHPExcel and BasicExcel libraries historically struggled with this exact scenario because they often assumed a specific format or required manual instantiation of readers based on file extension heuristics that weren't robust enough. PhpSpreadsheet, however, is built on a much more rigorous architecture designed specifically to handle the complexities of the OOXML (.xlsx) versus the older BIFF8 (.xls) formats.

The Factory Pattern and Format Detection

The core mechanism here is the IOFactory class. Its job is to inspect the uploaded file or local path and instantiate the appropriate reader object behind the scenes. For .xlsx files, this works seamlessly because the library uses a ZIP-based structure which is distinct and easy to detect via magic bytes or extension.

However, with .xls files, the detection logic can sometimes falter if the file extension does not match the internal magic number, or if the factory's default configuration is too strict. When IOFactory::load() throws an exception for an .xls file while accepting the .xlsx counterpart, it means the factory could not determine that you wanted to load a legacy Excel 97-2003 format.

In your first example, you relied entirely on the factory's automatic detection. In many cases involving mixed environments or corrupted headers, this fails silently or throws an exception before you even see the data. The solution is to bypass the factory for binary .xls files and instantiate the specific reader class directly. This removes any ambiguity about which format loader is being used.

Direct Reader Instantiation

To resolve the issue where IOFactory::load() fails on .xls, you must explicitly create a XlsReader. This approach gives you full control over the loading process and ensures that the library attempts to parse the Binary Interchange File Format (BIFF) correctly, regardless of what the file extension claims.

Here is the corrected implementation:

require 'vendor/autoload.php';

use PhpOffice\PhpSpreadsheet\Reader\Xls;
use PhpOffice\PhpSpreadsheet\Spreadsheet;

// Create a dedicated reader for the legacy XLS format
$reader = new Xls();

// Load the file explicitly using the specific reader
$spreadsheet = $reader->load('lista.xls');

// Proceed with standard iteration
$worksheet = $spreadsheet->getActiveSheet();

echo '<table>' . PHP_EOL;
foreach ($worksheet->getRowIterator() as $row) {
    echo '<tr>' . PHP_EOL;
    
    // Iterate through all cells, including empty ones if needed
    $cellIterator = $row->getCellIterator();
    $cellIterator->setIterateOnlyExistingCells(FALSE);
    
    foreach ($cellIterator as $cell) {
        echo '<td>' . 
             htmlspecialchars($cell->getValue()) . 
             '</td>' . PHP_EOL;
    }
    echo '</tr>' . PHP_EOL;
}
echo '</table>' . PHP_EOL;

echo "<br>fin";

Handling Iteration and Empty Cells

A common pitfall when working with legacy .xls files is the handling of empty cells. By default, cell iterators only return cells that have a value. In older Excel versions, it was very common to leave rows with sparse data or trailing empty columns. If your application expects a grid where empty cells render as blank <td> tags rather than being omitted entirely, you must set setIterateOnlyExistingCells(FALSE).

This setting ensures that the loop iterates through every column index for that row, maintaining the table structure even if specific cells contain no data. Failing to do this can result in misaligned columns when the file is re-imported or displayed in a UI component expecting a fixed grid.

Why This Matters for Legacy Data

Migrating projects from PHPExcel to PhpSpreadsheet is often necessary due to security vulnerabilities and lack of maintenance in the former. However, the transition requires attention to these specific loader nuances. If you are processing user uploads, always validate the file type before loading. While checking the extension is a first step, relying solely on it for .xls files without explicit reader instantiation can lead to runtime exceptions if the library's internal magic number check disagrees with the file system.

If you find yourself dealing with complex legacy data or require a more robust pipeline for handling mixed Excel formats in a Laravel application, consider reaching out to specialized resources like LaravelCompany for further guidance on integrating these libraries efficiently.

Our network