- How do I install PhpSpreadsheet in a Laravel application?
- Install it with Composer by running composer require phpoffice/phpspreadsheet. There is no Laravel service provider or config file to publish, so you use the PhpSpreadsheet classes directly through Composer autoloading. Make sure your application runs PHP 8.2 or newer, which is the current minimum for PhpSpreadsheet.
- What PHP and Laravel versions does PhpSpreadsheet support?
- The current release requires PHP 8.2 as the minimum version, and upstream support for that PHP version runs until June 2027. It works in Laravel applications running PHP 8.2 or newer, including Laravel 11 and later. There is no native Laravel integration, so compatibility is mainly determined by your PHP version and Composer.
- Can PhpSpreadsheet read and write XLSX, CSV, and ODS files in Laravel?
- Yes. PhpSpreadsheet supports XLSX, ODS, CSV, and several other spreadsheet formats through IOFactory and the writer classes. You can load a file with IOFactory::load or create the appropriate reader, then write with classes such as Writer\Xlsx. Some advanced features, like charts, may not round-trip perfectly in every format, so test your specific files.
- How do I export a large Excel file from Laravel without running out of memory?
- Use chunked processing, cell caching, and setMemoryCacheSize rather than loading an entire large workbook into memory. For exports over roughly 100K rows, move the work into a Laravel queue job and stream the output instead of building the full response. Monitor memory_get_usage during development and set a realistic memory_limit for your workers.
- How do I stream an XLSX download in Laravel with PhpSpreadsheet?
- Use Laravel's response()->streamDownload helper with a closure that calls the writer's save method on php://output. Pass the desired filename and headers so the browser downloads the file correctly. This approach avoids holding the entire generated spreadsheet in memory before sending it to the client.
- Does PhpSpreadsheet work with Laravel queues for background exports?
- Yes. Create a ShouldQueue job that generates the spreadsheet and stores it using Laravel's Storage facade, then notify the user when it is ready. PhpSpreadsheet does not support parallel or multi-threaded processing, so use separate queue workers rather than trying to run parallel exports in one process. This pattern keeps large exports from blocking HTTP requests.
- How do I import an uploaded Excel or CSV file into Laravel with PhpSpreadsheet?
- Get the uploaded file with $request->file, validate it, and load it using IOFactory::load or IOFactory::createReaderForFile. Iterate the rows with a row iterator or toArray, map the data to your Eloquent models, and consider bulk inserts for performance. Watch for Excel date serial numbers, merged cells, and empty rows when validating imported data.
- How does PhpSpreadsheet handle Excel dates and time zones in Laravel?
- Excel stores dates as serial numbers rather than native PHP date values. When reading, convert them with PhpSpreadsheet\Shared\Date::excelToDateTimeObject(), and when writing, set the cell format and use Date::PHPToExcel or DateTime objects. Keep your Laravel application timezone consistent and test dates around daylight saving transitions.
- How do I test PhpSpreadsheet imports and exports in Laravel?
- Use PHPUnit or Pest with fixture XLSX and CSV files to test reading, writing, formulas, merged cells, formats, and date conversions. Assert cell values and styles after generation, and test chunk boundaries for large-file processing. Mock Laravel Storage and queue dispatch when testing jobs that store exported files.
- Is PhpSpreadsheet a good alternative to Laravel Excel for spreadsheet exports?
- Laravel Excel, the maatwebsite/excel package, wraps PhpSpreadsheet and adds Laravel-specific imports, exports, and queued export helpers. Using PhpSpreadsheet directly gives you more control and fewer package layers, but you write more boilerplate for Laravel integration. Spout is a lighter alternative for streaming, though it has less formatting and formula support than PhpSpreadsheet.