SimpleXLSX is a lightweight PHP library used to read Excel .xlsx files. It is a convenient alternative for working with Excel files when you need to extract data but do not require complex features offered by more heavyweight libraries like PHPExcel or PhpSpreadsheet.

The class file, simplexlsx.class.php, provides an easy way to parse Excel spreadsheets in PHP without requiring additional extensions beyond the built-in PHP functionality.

Features of SimpleXLSX

Read .xlsx Files

Parse and retrieve data from Excel .xlsx files. Does not support the older .xls format.

Lightweight and Easy to Use

No need to install additional dependencies. Small file size and minimal performance overhead.

Compatibility

Works with most modern PHP versions.

How to Use simplexlsx.class.php

Download the Library

Include the File in Your Project

Save simplexlsx.class.php in your project directory and include it in your script:

require_once 'simplexlsx.class.php';

Read an Excel File

Use the SimpleXLSX class to load and parse an .xlsx file.

Example:

<?php
require_once 'simplexlsx.class.php';

if ($xlsx = SimpleXLSX::parse('example.xlsx')) {
    // Print rows from the first sheet
    foreach ($xlsx->rows() as $row) {
        print_r($row);
    }
} else {
    // Handle error
    echo SimpleXLSX::parseError();
}
?>

Key Methods in SimpleXLSX

Method Description
parse($filename) Loads the .xlsx file. Returns an object on success or false on failure.
rows() Returns all rows from the first sheet as an array.
rowsEx() Returns rows with additional cell information (e.g., data type).
sheetNames() Returns an array of sheet names.
dimension() Returns the dimensions of the sheet ([rows, cols]).
parseError() Returns the last error message, if any.

Advanced Example: Handling Multiple Sheets

If your Excel file contains multiple sheets, you can loop through them.

<?php
require_once 'simplexlsx.class.php';

if ($xlsx = SimpleXLSX::parse('example.xlsx')) {
    foreach ($xlsx->sheetNames() as $sheetIndex => $sheetName) {
        echo "Sheet: $sheetName\n";
        
        foreach ($xlsx->rows($sheetIndex) as $row) {
            print_r($row);
        }
    }
} else {
    echo SimpleXLSX::parseError();
}
?>

Error Handling

To handle errors, use the parseError() method. It will provide helpful error messages if the file is invalid or unreadable.

Example:

<?php
require_once 'simplexlsx.class.php';

if (!$xlsx = SimpleXLSX::parse('invalid_file.xlsx')) {
    echo 'Error: ' . SimpleXLSX::parseError();
}
?>

Advantages and Limitations

Advantages

  1. Ease of Use: Minimal setup and simple syntax.
  2. Performance: Lightweight and faster for simple Excel file parsing compared to alternatives.
  3. No External Dependencies: Works with native PHP functionality.

Limitations

  1. Read-Only: Can only read .xlsx files; it cannot write or modify them.
  2. Limited Features: Does not support advanced Excel features like charts, macros, or complex formatting.
  3. No .xls Support: Only works with .xlsx files (Office Open XML format).

Alternatives to SimpleXLSX

If your project requires more advanced Excel handling capabilities, consider:

  1. PhpSpreadsheet:
    • Supports reading and writing .xls and .xlsx files.
    • Offers advanced features like formatting, charts, and formulas.
    • PhpSpreadsheet GitHub
  2. PHPExcel (Deprecated):

simplexlsx.class.php is an excellent choice for lightweight and straightforward Excel file parsing in PHP. It is ideal for projects requiring basic data extraction without heavy dependencies. For advanced use cases, PhpSpreadsheet might be a better fit.