Reading an Excel file in PHP

Reading an Excel file in PHP

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

Stefan Bogdanescu

Founder & Senior Architect

Reading an Excel file in PHP

Handling Excel files in a PHP environment often hits an immediate roadblock: you cannot rely on the Microsoft Office application being installed on the server, especially when operating on shared hosting. Even if you have access to Office 2003 locally and can extract XML structures from .xls files generated by that version, attempting to parse them directly with native PHP libraries is a dead end. The binary format of older Excel versions (.xls) and newer ones (.xlsx) requires specific parsers that understand the underlying OOXML or BIFF structures.

The most pragmatic solution for reading these files without installing desktop applications is php-excel-reader. While you can find references to this library in various tutorials, implementation details often get glossed over. This guide focuses on the practical mechanics of integrating a third-party reader into your application stack.

The Parser Choice: Why php-excel-reader?

The landscape of PHP Excel libraries is fragmented. You will frequently encounter abandoned projects or solutions that only handle CSV exports. However, for reading existing spreadsheets, php-excel-reader remains the standard open-source choice. It is a lightweight library specifically designed to parse the binary structure of .xls files (Excel 97–2003 format) and the OOXML structure of .xlsx files.

The primary advantage is that it requires no external dependencies like Microsoft Office or LibreOffice. You can drop the code into a shared hosting environment where composer install might be restricted, provided you manually place the library files in your project directory.

Implementation Strategy

Before diving into the code, consider the source material constraints. If you are dealing with an Office 2003 file, it is an .xls file. The parser needs to distinguish between the row-based and column-based structures depending on how the data was entered.

To get started, you need to include the autoloader or manually require the main class file. Here is a basic setup using a legacy-style include common in shared hosting environments:

require 'vendor/autoload.php'; // Or direct require for older setups
use PHPOffice\Excel_Reader;

$filename = 'uploaded_data.xls';

try {
    $reader = new Excel_Reader($filename);
    
    // Access the sheet name
    $sheetName = $reader->getWorksheet()->getName();
    
    // Iterate through rows and columns
    foreach ($reader->getWorksheet() as $row) {
        foreach ($row as $column => $value) {
            echo "Row {$row}, Column {$column}: {$value}\n";
        }
    }
} catch (Exception $e) {
    // Handle errors gracefully, especially for malformed files
    echo "Error reading file: " . $e->getMessage();
}

Handling Data Types and Pitfalls

One of the trickiest aspects of reading Excel files is preserving data types. Excel cells store data as strings internally to maintain formatting. When you read a cell that contains a number, it often arrives as a string object or a numeric string rather than a pure integer or float. If your application expects strict typing for database insertion, you may need to cast these values explicitly.

Additionally, empty cells in Excel are not always represented as null; sometimes they appear as empty strings depending on how the parser handles the specific version of the file. Always check if a value is actually populated before attempting arithmetic operations.

Integration with Modern Stacks

If your project uses Laravel, the integration follows the same principles but utilizes Composer for dependency management. You would run composer require php-excel-reader/php-excel-reader to fetch the package. Once installed, you can wrap the reader logic in a Service Provider or a dedicated Model trait to keep your controllers clean.

However, be wary of mixing readers and writers in the same namespace without careful scoping. The library is excellent for reading but not suitable for writing complex spreadsheets; for that, PhpSpreadsheet is the modern alternative. If you need both capabilities, consider using a single project dependency management strategy rather than mixing two different legacy libraries.

Limitations to Consider

While php-excel-reader solves the immediate problem of reading Office 2003 files on a server without Office installed, it has limitations. It does not support complex formulas calculation; it reads the result of the formula as stored in the cell. If you need to evaluate dynamic formulas or handle very large files that exceed memory limits, this approach might require streaming logic or switching to a different tool entirely.

For most data migration tasks and legacy file processing on shared hosts, this remains the most robust path forward without requiring virtualization or expensive commercial licenses.

Our network