Introduction
PhpSpreadsheet is the successor to PHPExcel and provides a comprehensive set of classes for working with spreadsheet files in PHP. It handles reading existing files, creating new spreadsheets programmatically, and writing output in multiple formats without requiring any external office software.
What PhpSpreadsheet Does
- Reads and writes XLSX, XLS, ODS, CSV, HTML, and SYLK file formats
- Supports cell formatting including fonts, colors, borders, and number formats
- Handles formulas with a built-in calculation engine
- Generates charts embedded in spreadsheet files
- Exports spreadsheets to PDF via multiple rendering backends
Architecture Overview
The library follows an object-oriented design with a central Spreadsheet object containing Worksheet objects, each holding Cell objects. Readers and Writers are format-specific implementations that serialize and deserialize this object model. The calculation engine evaluates cell formulas using a recursive descent parser.
Self-Hosting & Configuration
- Install via Composer as a project dependency
- Requires PHP 8.1 or higher with ext-xml and ext-zip extensions
- Optional ext-gd or ext-imagick for image handling in spreadsheets
- Configure memory limits in php.ini for large spreadsheets
- Use chunk reading for processing files that exceed available memory
Key Features
- Full formula support with 400+ built-in functions
- Cell merging, conditional formatting, and data validation
- Auto-filter, freeze panes, and print area configuration
- Reading and writing password-protected XLSX files
- Streaming writer for generating large files with low memory usage
Comparison with Similar Tools
- Laravel Excel (Maatwebsite) — Wrapper around PhpSpreadsheet with Laravel integration
- box/spout (OpenSpout) — Faster streaming reader/writer but fewer features
- PhpExcel — Predecessor library, now deprecated in favor of PhpSpreadsheet
- Python openpyxl — Similar capabilities but for the Python ecosystem
- Apache POI — Java equivalent with broader format support
FAQ
Q: How does PhpSpreadsheet handle large files? A: Use the chunk reading filter to process files in segments, or use the streaming Xlsx writer (Xlsxwriter) for low-memory output generation.
Q: Can I use it without Laravel? A: Yes. PhpSpreadsheet is framework-agnostic and works in any PHP project via Composer.
Q: Does it support Excel macros? A: It can preserve macros in XLSM files but cannot execute or create VBA code.
Q: What is the maximum file size it can handle? A: There is no hard limit, but practical limits depend on available PHP memory. The streaming writer can generate files of any size.