# Laravel Excel Examples These examples are representative patterns. For complete feature coverage, use `../references/package.md`. ## Collection Export ```php latest()->get(); } public function headings(): array { return ['Name', 'Email', 'Created At']; } public function map($user): array { return [ $user->name, $user->email, $user->created_at?->toDateTimeString(), ]; } } ``` ```php use App\Exports\UsersExport; use Maatwebsite\Excel\Facades\Excel; return Excel::download(new UsersExport, 'users.xlsx'); ``` ## Queued Query Export ```php with('customer')->whereNotNull('paid_at'); } public function headings(): array { return ['Order', 'Customer', 'Total', 'Paid At']; } public function map($order): array { return [ $order->number, $order->customer->name, $order->total, $order->paid_at?->toDateString(), ]; } public function chunkSize(): int { return 1000; } } ``` ```php Excel::queue(new OrdersExport, 'exports/orders.xlsx', 's3'); ``` ## Multi-Sheet Export ```php new UsersExport, 'orders' => new OrdersExport, ]; } } ``` ## Styling, Events, And CSV Settings ```php NumberFormat::FORMAT_CURRENCY_USD_SIMPLE, ]; } public function getCsvSettings(): array { return [ 'delimiter' => ';', 'use_bom' => true, ]; } public function registerEvents(): array { return [ AfterSheet::class => function (AfterSheet $event): void { $event->sheet->freezePane('A2'); }, ]; } public function styles(Worksheet $sheet): array { return [ 1 => ['font' => ['bold' => true]], ]; } } ``` ## Validated Chunked Import ```php $row['name'], 'email' => $row['email'], ]); } public function rules(): array { return [ '*.name' => ['required', 'string', 'max:255'], '*.email' => ['required', 'email', 'distinct'], ]; } public function batchSize(): int { return 1000; } public function chunkSize(): int { return 1000; } } ``` ```php $import = new UsersImport; $import->import($request->file('users')); if ($import->failures()->isNotEmpty()) { // Return validation feedback to the user. } ``` ## Mapped Cells Import ```php 'B2', 'customer_name' => 'B3', 'total' => 'E20', ]; } public function array(array $array): void { // Handle mapped values. } } ``` ## Testing With The Fake ```php use App\Exports\UsersExport; use Maatwebsite\Excel\Facades\Excel; Excel::fake(); $this->get('/users/export')->assertOk(); Excel::assertDownloaded('users.xlsx', function (UsersExport $export): bool { return true; }); ```