Reading a Spreadsheet with IOFactory
The simplest way to read a spreadsheet is usingIOFactory::load(), which automatically detects the file format:
What This Code Does
- Auto-detects format:
IOFactory::load()identifies whether the file is XLSX, XLS, ODS, CSV, etc. - Loads the spreadsheet: Reads the entire file into memory
- Converts to array:
toArray()extracts all cell data as a PHP array - Array format: Returns data indexed by column letters and row numbers (e.g.,
['A' => [1 => 'value']])
Reading Specific File Formats
Reading Only Data (No Formatting)
For better performance, you can read only the cell values without loading formatting, styles, or other metadata:Accessing Cell Data
Once you’ve loaded a spreadsheet, you can access cell data in multiple ways:Reading Multiple Worksheets
Error Handling
Always wrap file operations in try-catch blocks:Key Takeaways
- Use
IOFactory::load()for automatic format detection - Use specific reader classes for better control over file loading
- Enable
setReadDataOnly(true)for faster loading when you don’t need formatting - Use
toArray()to get all data at once or iterators for row-by-row processing - Always wrap file operations in try-catch blocks

