The Developer’s Complete Guide to Using FastestExcel When dealing with large-scale data imports and exports in Laravel, standard Excel packages often hit a wall. Memory exhaustion errors, CPU spikes, and sluggish execution times are common when processing sheets with tens of thousands of rows.
Enter FastestExcel, a Laravel wrapper built on top of the ultra-fast box/spout library. It is designed specifically to handle massive Excel and CSV files quickly while keeping memory consumption near zero. This guide covers everything you need to know to implement FastestExcel in your projects. Why Choose FastestExcel?
Traditional Excel packages for Laravel load the entire spreadsheet into memory to build an object tree. While feature-rich, this approach fails on larger datasets.
FastestExcel takes a different approach by using streaming readers and writers. Instead of loading the whole file at once, it reads and writes data row-by-row.
Minimal Memory Footprint: Keeps memory usage low and constant, regardless of file size.
Speed: Speeds up import and export operations by up to 10x compared to heavy DOM-based alternatives.
Developer-Friendly: Offers a clean, fluent syntax that integrates natively with Laravel Collections and Eloquent models. Installation
Getting started requires a quick Composer installation. Run the following command in your terminal: composer require rap2hpoutre/fastest-excel Use code with caution.
The package automatically registers its service provider and facade if you are using a modern version of Laravel. High-Performance Exports
Exporting large database tables to Excel is one of the most common use cases for FastestExcel. You can export Eloquent models, collections, or generators. 1. Basic Export from a Collection
To export a basic Laravel collection to an Excel sheet, pass the collection directly to the export method:
use Rap2hpoutre\FastestExcel\FastestExcel; \(users = User::all(); (new FastestExcel(\)users))->export(‘users.xlsx’); Use code with caution. 2. Exporting with Custom Mapping
You rarely want to dump raw database columns directly into a client-facing spreadsheet. FastestExcel allows you to map your data dynamically using a closure:
use Rap2hpoutre\FastestExcel\FastestExcel; (new FastestExcel(User::all()))->export(‘users.xlsx’, function (\(user) { return [ 'ID' => \)user->id, ‘Full Name’ => \(user->first_name . ' ' . \)user->last_name, ‘Email Address’ => \(user->email, 'Joined Date' => \)user->created_at->format(‘Y-m-d’), ]; }); Use code with caution. 3. Memory-Safe Database Streaming
For truly massive datasets (e.g., 500,000 rows), loading all models via User::all() will still crash your server due to PHP memory limits.
To solve this, combine FastestExcel with Laravel’s lazy loading queries (cursor or chunk):
use Rap2hpoutre\FastestExcel\FastestExcel; function usersGenerator() { foreach (User::cursor() as \(user) { yield [ 'ID' => \)user->id, ‘Email’ => \(user->email, ]; } } // Pass the generator directly to keep memory usage at zero (new FastestExcel(usersGenerator()))->export('huge_users_list.xlsx'); </code> Use code with caution. Rapid Data Imports</p> <p>Importing data is just as straightforward. FastestExcel parses files row-by-row and converts them into Laravel Collections for easy manipulation. 1. Basic Import to Collection</p> <p>To read an uploaded Excel file and get a collection of arrays:</p> <p><code>use Rap2hpoutre\FastestExcel\FastestExcel; \)collection = (new FastestExcel)->import(‘customers.xlsx’); // \(collection contains rows mapped by their header columns </code> Use code with caution. 2. Importing Directly to Database Models</p> <p>You can pass a closure to the <code>import</code> method to process and save each row immediately to the database:</p> <p><code>use Rap2hpoutre\FastestExcel\FastestExcel; use App\Models\Product; (new FastestExcel)->import('products.csv', function (\)line) { return Product::create([ ‘sku’ => \(line['SKU'], 'name' => \)line[‘Product Name’], ‘price’ => \(line['Price'], ]); }); </code> Use code with caution. 3. Handling Multi-Sheet Files</p> <p>If you are importing a workbook with multiple sheets, you can target specific sheets by their index or loop through all of them:</p> <p><code>use Rap2hpoutre\FastestExcel\FastestExcel; // Import only the second sheet (index 1) \)sheets = (new FastestExcel)->sheet(1)->import(‘data.xlsx’); // Import all sheets at once \(sheets = (new FastestExcel)->importSheets('data.xlsx'); </code> Use code with caution. Advanced Configurations</p> <p>FastestExcel provides several configuration utilities to format and structure your outputs cleanly. CSV Customization</p> <p>When working with CSV files instead of XLSX files, you can alter the default delimiter, enclosure, and encoding styles:</p> <p><code>use Rap2hpoutre\FastestExcel\FastestExcel; (new FastestExcel(\)data)) ->setDelimiter(‘;’) ->setEnclosure(‘“’) ->export(‘data.csv’); Use code with caution. Skipping Rows and Styling Headers
You can customize simple style headers or bypass empty lines during import tasks using native configurations:
// Skip the header row if manually processing lines $import = (new FastestExcel)->withoutHeaders()->import(‘file.xlsx’); Use code with caution. Summary: When to Use (and When to Avoid)
FastestExcel is a specialized tool optimized for performance. While highly effective, it does require a few structural trade-offs.
Use it if: You need to import or export massive raw datasets quickly, parse standard CSV/XLSX files, and keep server resource consumption to an absolute minimum.
Avoid it if: You require heavy visual formatting, embedded charts, complex cell styling, conditional formatting, or multi-row spanning headers. For those highly styled presentation reports, traditional tools like Maatwebsite Laravel Excel remain the preferred option.
By integrating FastestExcel into your data workflows, you can eliminate timeout errors and build lightning-fast background jobs capable of processing millions of rows seamlessly.
If you want to tailor this guide to a specific project, let me know: What size of datasets are you planning to process? Are you looking to handle XLSX, CSV, or both?
Will you be running these processes in real-time web requests or background queues?
I can provide optimization tips specific to your environment.
Leave a Reply