PHP code example of asignua / filament-xlsx-export

1. Go to this page and download the library: Download asignua/filament-xlsx-export library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

asignua / filament-xlsx-export example snippets


use Asignua\FilamentXlsxExport\Actions\XlsxExportAction;

protected function getHeaderActions(): array
{
    return [
        XlsxExportAction::make(),
    ];
}

use Asignua\FilamentXlsxExport\Actions\XlsxExportBulkAction;

$table->toolbarActions([
    BulkActionGroup::make([
        XlsxExportBulkAction::make(),
    ]),
]);

XlsxExportAction::make()
    ->title('Orders')                               // bold first row, also the sheet name
    ->footer(fn () => 'Generated '.now()->format('Y-m-d H:i'))   // under the data and totals
    ->caption(fn (array $data, int $rowCount) => "Open orders, {$rowCount} rows")
    ->fileName(fn () => 'orders-'.now()->format('Y-m-d'))
    ->rowLimit(10_000)
    ->columnFormats([...]);

use Asignua\FilamentXlsxExport\ColumnFormat;

XlsxExportAction::make()->columnFormats([
    'total'      => ColumnFormat::make()->money('€')->sum(),        // "€" #,##0.00 and a bold total row
    'price'      => ColumnFormat::make()->divideBy(100)->decimal(2), // stored in cents
    'ratio'      => ColumnFormat::make()->percent(1),
    'weight'     => ColumnFormat::make()->number('0.000'),
    'zip'        => ColumnFormat::make()->text(),                    // keeps "00123"
    'created_at' => ColumnFormat::make('Created')->date('dd.mm.yyyy')->width(14),
    'paid'       => ColumnFormat::make()->boolean(),
    'internal'   => ColumnFormat::make()->exclude(),                 // never offered
    'notes'      => ColumnFormat::make()->unselected(),              // offered, not ticked
    'vat'        => ColumnFormat::make('VAT')                        // virtual column
                        ->value(fn (Order $record, $state, array $data) => $record->total * 0.2)
                        ->decimal(2),
]);

XlsxExportAction::make()
    ->exportOptions([
        Toggle::make('only_paid')->label('Only paid orders'),
    ])
    ->queryUsing(fn (Builder $query, array $data) => ($data['only_paid'] ?? false)
        ? $query->where('paid', true)
        : $query)
    ->caption(fn (array $data) => $data['only_paid'] ? 'Paid orders' : 'All orders');

XlsxExportAction::make()->streamed();        // always stream
XlsxExportAction::make()->streamed(false);   // never; stay in Livewire (bound by row_limit)

Route::middleware(['web'])
    ->get('exports/{token}', \Asignua\FilamentXlsxExport\Http\DownloadController::class)
    ->where('token', '[A-Za-z0-9]{48}')
    ->name(\Asignua\FilamentXlsxExport\Support\StreamedExports::ROUTE); // 'filament-xlsx-export.download'

use Asignua\FilamentXlsxExport\Concerns\ExportsTypedXlsx;

class OrderExporter extends Exporter
{
    use ExportsTypedXlsx;

    public function xlsxColumnFormats(): array
    {
        return [
            'total'      => ColumnFormat::make()->money(),
            'created_at' => ColumnFormat::make()->dateTime(),
            'paid'       => ColumnFormat::make()->boolean(),
        ];
    }
}
bash
php artisan vendor:publish --tag=filament-xlsx-export-config