rap2hpoutre/fast-excel
Fast Excel import/export for Laravel powered by Spout. Quickly export Eloquent models or collections to XLSX/ODS/CSV, customize column mapping, and download from controllers. Import files to collections, configure CSV options, or persist rows directly to the database.
Install via Composer:
composer require rap2hpoutre/fast-excel
First Use Case: Export a Laravel Eloquent collection to Excel.
use Rap2hpoutre\FastExcel\Facades\FastExcel;
$users = User::all();
FastExcel::data($users)->export('users.xlsx');
Where to Look First:
// Export all users to XLSX
(new FastExcel(User::all()))->export('users.xlsx');
// Export with custom column mapping
(new FastExcel(User::all()))->export('users.xlsx', function ($user) {
return [
'Full Name' => $user->first_name . ' ' . $user->last_name,
'Email' => $user->email,
];
});
public function exportUsers()
{
return (new FastExcel(User::all()))
->headerStyle((new Style())->setFontBold())
->download('users.xlsx');
}
function usersGenerator()
{
foreach (User::cursor() as $user) {
yield $user;
}
}
(new FastExcel(usersGenerator()))->export('large_users.xlsx');
$rows = (new FastExcel)->import('data.xlsx');
$rows = (new FastExcel)
->configureCsv(';', '"', 'gbk')
->import('data.csv');
(new FastExcel)->import('users.xlsx', function ($row) {
User::create([
'name' => $row['Name'],
'email' => $row['Email'],
]);
});
use Rap2hpoutre\FastExcel\SheetCollection;
$sheets = new SheetCollection([
'Users' => User::all(),
'Projects' => Project::all(),
]);
(new FastExcel($sheets))->export('multi_sheet.xlsx');
$users = (new FastExcel)->sheet(1)->import('multi_sheet.xlsx');
$sheets = (new FastExcel)->withSheetsNames()->importSheets('multi_sheet.xlsx');
use OpenSpout\Common\Entity\Style\Style;
$headerStyle = (new Style())->setFontBold()->setBackgroundColor('DDDDDD');
$rowStyle = (new Style())->setFontSize(12);
(new FastExcel(User::all()))
->headerStyle($headerStyle)
->rowsStyle($rowStyle)
->export('styled_users.xlsx');
(new FastExcel)->configureCsv(',', "'", 'utf-8')->import('data.csv');
FastExcel::data(User::all())->export('users.xlsx');
fastexcel(User::all())->export('users.xlsx');
Route::get('/export/{type}', function ($type) {
$data = match ($type) {
'users' => User::all(),
'invoices' => Invoice::where('status', 'paid')->get(),
default => throw new \Exception('Invalid type'),
};
return (new FastExcel($data))->download("{$type}.xlsx");
});
// Dispatch a job for large exports
ExportUsersJob::dispatch();
// Job class
public function handle()
{
(new FastExcel(User::cursor()))->export(storage_path('app/large_users.xlsx'));
}
public function test_export_users()
{
$users = User::factory()->count(3)->create();
$file = tempnam(sys_get_temp_dir(), 'test_');
(new FastExcel($users))->export($file);
$this->assertFileExists($file);
unlink($file);
}
public function test_import_users()
{
$file = tempnam(sys_get_temp_dir(), 'test_');
(new FastExcel)->export($file, function () {
return [
['name' => 'John', 'email' => 'john@example.com'],
['name' => 'Jane', 'email' => 'jane@example.com'],
];
});
$imported = (new FastExcel)->import($file);
$this->assertCount(2, $imported);
unlink($file);
}
Memory Limits with Large Datasets
memory_limit.cursor() or yield:
(new FastExcel(User::cursor()))->export('large_file.xlsx');
CSV Encoding Issues
é, ü) may corrupt in CSV.(new FastExcel)->configureCsv(',', '"', 'utf-8')->import('file.csv');
Sheet Name Limitations
$sheetName = substr($name, 0, 30);
Facade vs. Class Instantiation
FastExcel::data()) does not support constructor options (e.g., configureCsv).(new FastExcel)->configureCsv(';', '"', 'gbk')->import('file.csv');
PHP 8+ Requirement
v2.x).Large File Downloads
return response()->streamDownload(function () {
$excel = new FastExcel(User::cursor());
$excel->export('large_file.xlsx');
}, 'large_file.xlsx');
Multi-Byte Character Truncation
$excel->setColumnWidths([1 => 50]); // Set column B width to 50 chars
Check File Permissions
chmod -R 775 storage/app
Validate CSV Delimiters
(new FastExcel)->configureCsv('|', '"', 'utf-8')->import('test.csv');
Log Import Errors
try-catch to log malformed rows:
try {
$rows = (new FastExcel)->import('data.xlsx');
} catch (\Exception $e) {
Log::error("Import failed: " . $e->getMessage());
}
Verify Sheet Indexes
1):
$sheet = (new FastExcel)->sheet(1)->import('file.xlsx');
Test with Minimal Data
Rap2hpoutre\FastExcel\FastExcel to add custom Spout writers:
class CustomExcel extends FastExcel
{
public function __construct()
{
parent::__construct
How can I help you explore Laravel packages today?