- How does spatie/simple-excel handle large Excel/CSV files without memory issues?
- The package uses PHP generators and Laravel’s LazyCollection to process files row-by-row, avoiding loading entire spreadsheets into memory. This makes it ideal for files over 100MB, as each row is streamed dynamically during iteration.
- Can I use spatie/simple-excel in a non-Laravel PHP project?
- Yes, but with limitations. While it works with vanilla PHP, you’ll need to handle collections manually (e.g., using IteratorAggregate) since it relies on Laravel’s LazyCollection by default. The core functionality remains the same for reading/writing files.
- What Laravel versions does spatie/simple-excel support?
- The package supports Laravel 8.x, 9.x, and 10.x. Check the [GitHub repo](https://github.com/spatie/simple-excel) for the latest compatibility details, as minor updates may align with new Laravel releases.
- How do I convert CSV headers to snake_case automatically?
- Use the `headersToSnakeCase()` method when creating the reader: `SimpleExcelReader::create($file)->headersToSnakeCase()->getRows()`. This ensures headers like `FirstName` become `first_name` in your processed data.
- Does spatie/simple-excel support multi-sheet Excel files?
- No, the package focuses on single-sheet operations. For multi-sheet Excel files, consider alternatives like PhpSpreadsheet or PhpOffice/PhpSpreadsheet, though they require more memory for large files.
- How can I validate or sanitize CSV data before processing?
- Use the `formatHeadersUsing()` or `transformUsing()` methods to preprocess rows. For example, `transformUsing(fn($row) => filter_var($row['email'], FILTER_VALIDATE_EMAIL))` ensures only valid emails pass through.
- Will spatie/simple-excel work with Laravel queues or jobs?
- Absolutely. Since it returns a LazyCollection, you can chain operations like `getRows()->each()` directly in a job or queue. This is perfect for background processing of large imports without blocking requests.
- How do I handle malformed Excel/CSV files (e.g., missing headers, corrupt data)?
- The package skips malformed rows by default. For stricter control, wrap the `getRows()` iteration in a try-catch block or use `transformUsing()` to validate each row before processing.
- Can I stream Excel files directly to a browser for download?
- Yes, use the `toBrowser()` method on `SimpleExcelWriter` to stream files directly. For large files, manually flush output buffers (`ob_flush()`) to avoid timeouts or memory spikes during download.
- What are the alternatives to spatie/simple-excel for Laravel Excel handling?
- For lightweight needs, this package is ideal. For advanced Excel features (e.g., formulas, charts), use PhpSpreadsheet. For CSV-only tasks, League/Csv is a minimal alternative. Choose based on memory constraints and feature requirements.