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
- You can download the simplexlsx.class.php file from the official GitHub repository:
- SimpleXLSX GitHub Repository
Include the File in Your Project
Save simplexlsx.class.php in your project directory and include it in your script:
Read an Excel File
Use the SimpleXLSX class to load and parse an .xlsx file.
Example:
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.
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:
require_once 'simplexlsx.class.php';
if (!$xlsx = SimpleXLSX::parse('invalid_file.xlsx')) {
echo 'Error: ' . SimpleXLSX::parseError();
}
?>
Advantages and Limitations
Advantages
- Ease of Use: Minimal setup and simple syntax.
- Performance: Lightweight and faster for simple Excel file parsing compared to alternatives.
- No External Dependencies: Works with native PHP functionality.
Limitations
- Read-Only: Can only read .xlsx files; it cannot write or modify them.
- Limited Features: Does not support advanced Excel features like charts, macros, or complex formatting.
- 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:
- PhpSpreadsheet:
- Supports reading and writing .xls and .xlsx files.
- Offers advanced features like formatting, charts, and formulas.
- PhpSpreadsheet GitHub
- PHPExcel (Deprecated):
- Legacy library replaced by PhpSpreadsheet.
- PHPExcel GitHub
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.


