rap2hpoutre/fast-excel
Fast, memory-efficient Excel/CSV/ODS import/export for Laravel using Spout. Export Eloquent models or collections to XLSX/CSV/ODS with custom column mapping, and download from controllers. Import files to collections, configure CSV options, or transform rows into DB inserts.
Install via Composer:
composer require rap2hpoutre/fast-excel
First Use Case: Export a Laravel Eloquent collection to an Excel file:
use Rap2hpoutre\FastExcel\FastExcel;
use App\Models\User;
// Export all users to a file
(new FastExcel(User::all()))->export('users.xlsx');
Where to Look First:
Export a Model or Collection:
// Export Eloquent results
(new FastExcel(User::where('active', 1)->get()))->export('active_users.xlsx');
// Export a Collection
$users = collect([['name' => 'John'], ['name' => 'Jane']]);
(new FastExcel($users))->export('users.xlsx');
Download Directly in a Controller:
return (new FastExcel(Order::all()))->download('orders.xlsx');
Import a File:
$importedData = (new FastExcel)->import('data.xlsx');
Transform data before export:
(new FastExcel(User::all()))
->export('users.csv', function ($user) {
return [
'Full Name' => $user->first_name . ' ' . $user->last_name,
'Email' => strtolower($user->email),
];
});
Avoid memory issues with generators:
function largeUserGenerator() {
foreach (User::cursor()->where('status', 'active') as $user) {
yield $user;
}
}
(new FastExcel(largeUserGenerator()))->export('large_export.xlsx');
Export multiple collections to separate sheets:
use Rap2hpoutre\FastExcel\SheetCollection;
$sheets = new SheetCollection([
'Users' => User::all(),
'Invoices' => Invoice::all(),
]);
(new FastExcel($sheets))->export('multi_sheet.xlsx');
Configure CSV-specific settings:
$importedData = (new FastExcel)
->configureCsv(';', '"', 'gbk') // Delimiter, enclosure, encoding
->import('data.csv');
Apply styles to headers/rows:
use OpenSpout\Common\Entity\Style\Style;
$headerStyle = (new Style())->setFontBold();
$rowStyle = (new Style())->setBackgroundColor('EDEDED');
(new FastExcel(User::all()))
->headerStyle($headerStyle)
->rowsStyle($rowStyle)
->download('styled_export.xlsx');
Directly insert imported data into the database:
(new FastExcel)->import('users.xlsx', function ($row) {
return User::create([
'name' => $row['Name'],
'email' => $row['Email'],
]);
});
Add to config/app.php:
'FastExcel' => Rap2hpoutre\FastExcel\Facades\FastExcel::class,
Then use in controllers:
FastExcel::data(User::all())->export('users.xlsx');
Use fastexcel() anywhere in the app:
$users = fastexcel()->import('users.xlsx');
fastexcel($users)->export('processed_users.xlsx');
Offload large exports to a queue job:
// Job class
public function handle() {
(new FastExcel(User::all()))->export(storage_path('app/large_export.xlsx'));
}
PHP Version Requirement:
Memory Limits with Large Exports:
memory_limit.memory_limit in php.ini or process in smaller batches.CSV Encoding Issues:
gbk) may cause corruption if the file isn’t saved with the correct encoding.Multi-Sheet Import Limitations:
importSheets() returns an array of collections, but sheet names are not preserved by default.withSheetsNames() to retain sheet names:
$sheets = (new FastExcel)->withSheetsNames()->importSheets('file.xlsx');
Styling Limitations:
CellInterface for granular control if needed.Facade Limitations:
data() to set the export source.No Event Hooks:
FastExcel class to add hooks.Check File Paths:
export()/import() are absolute or relative to the correct directory.storage_path() for reliable paths:
(new FastExcel(User::all()))->export(storage_path('app/exports/users.xlsx'));
Validate Imported Data:
$data = (new FastExcel)->import('file.xlsx');
\Log::info('Imported data:', $data->toArray());
Memory Usage:
memory_get_usage():
$start = memory_get_usage();
(new FastExcel(User::all()))->export('users.xlsx');
$end = memory_get_usage();
\Log::info('Memory used:', $end - $start);
CSV Delimiter Issues:
,) before customizing.Custom Writers/Readers:
Rap2hpoutre\FastExcel\FastExcel to add custom logic:
class CustomFastExcel extends FastExcel {
public function customMethod() { ... }
}
Add Event Hooks:
$fastExcel = new FastExcel(User::all());
$fastExcel->setEventDispatcher($dispatcher); // Hypothetical
Support Additional Formats:
xls).Batch Processing Middleware:
$fastExcel = new FastExcel(User::all());
$fastExcel->setTransformer(new MyTransformer());
Spout Configuration:
tempDir).config/spout.php (if using Laravel’s config publishing).Facade vs. Direct Usage:
Sheet Naming in Multi-Sheet Exports:
SheetCollection must be strings (not objects). Non-string keys are ignored.Use Generators for Large Exports:
yield for datasets >10K rows to avoid memory spikes.Disable Unused Features:
(new FastExcel(User::all()))->export('users.xlsx'); // No styles
Leverage CSV for Large Imports:
(new FastExcel)->
How can I help you explore Laravel packages today?