This package is a plugin of Laravel DataTables for handling server-side exporting using Queue and OpenSpout, with optional Livewire and DataTables button integrations.
- PHP >=8.3 (OpenSpout 5.x, if installed, requires PHP 8.4+)
- Laravel 13
- Laravel Livewire (optional)
- OpenSpout
- Laravel DataTables 13.x
- jQuery DataTables 2.x
| Laravel | Package |
|---|---|
| 8.x | 0.x |
| 9.x | 1.x |
| 10.x | 10.x |
| 11.x | 11.x |
| 12.x | 12.x |
| 13.x | 13.x |
composer require yajra/laravel-datatables-export:"^13.0"
The package also requires batch job:
php artisan queue:batches-table
php artisan migratephp artisan vendor:publish --tag=datatables-export --force
Publish the package assets and include the queued export button script after jQuery, DataTables, and DataTables Buttons:
php artisan vendor:publish --tag=datatables-export<script src="/vendor/datatables/dataTables.queuedExport.js"></script>Add WithExportQueue to the DataTable service and configure the button with the existing HTML builder:
use Yajra\DataTables\Html\Button;
use Yajra\DataTables\Html\Builder as HtmlBuilder;
use Yajra\DataTables\WithExportQueue;
class UsersDataTable extends DataTable
{
use WithExportQueue;
public function html(): HtmlBuilder
{
return $this->builder()
->buttons([
Button::make([
'extend' => 'queuedExport',
'text' => 'Export Excel',
'exportType' => 'xlsx',
'filename' => 'users.xlsx',
'sheetName' => 'Users',
'autoDownload' => true,
]),
]);
}
}The button sends the table's current search, ordering, and filter parameters, displays export progress, polls the queued batch, and downloads the completed file. Supported options are exportType, filename, sheetName, emailTo, autoDownload, pollInterval, processingText, and errorText.
JavaScript callbacks can be supplied as onStart, onProgress, onSuccess, and onError. The table element also dispatches datatables-export:start, datatables-export:progress, datatables-export:success, and datatables-export:error events:
document.querySelector('#users-table').addEventListener('datatables-export:success', function (event) {
console.log(event.detail.export.download_url);
});Applications may listen for the server-side ExportStarted, ExportCompleted, and ExportFailed events to add notifications, broadcasting, or archival workflows. Broadcasting is optional and can use an application's own authenticated private channels.
Status and download URLs use encrypted access tokens. Their lifetime defaults to 1,440 minutes and can be changed with the token_ttl option in datatables-export.php.
Install Livewire when using the Livewire component:
composer require livewire/livewire- Add the export-button livewire component on your view file that uses dataTable class.
<livewire:export-button :table-id="$dataTable->getTableId()"/>- On your
DataTableclass, useWithExportQueue
use Yajra\DataTables\WithExportQueue;
class PermissionsDataTable extends DataTable
{
use WithExportQueue;
...
}- Run your queue worker. Ex:
php artisan queue:work
On app\Console\Kernel.php, register the purge command
$schedule->command('datatables:purge-export')->weekly();You can set the export filename by setting the property.
<livewire:export-button :table-id="$dataTable->getTableId()" filename="my-table.xlsx"/>
<livewire:export-button :table-id="$dataTable->getTableId()" filename="my-table.csv"/>
<livewire:export-button :table-id="$dataTable->getTableId()" :filename="$filename"/>You can set the export button name by setting the buttonName property.
<!-- Examples demonstrating how to customize the button name for different scenarios -->
<livewire:export-button :table-id="$dataTable->getTableId()" type="xlsx" buttonName="Export Excel"/>
<livewire:export-button :table-id="$dataTable->getTableId()" type="csv" buttonName="Export CSV"/>You can set the export type by setting the property to csv or xlsx. Default value is xlsx.
<livewire:export-button :table-id="$dataTable->getTableId()" type="xlsx"/>
<livewire:export-button :table-id="$dataTable->getTableId()" type="csv"/>Option 1: You can set the Excel sheet name by setting the property.
<livewire:export-button :table-id="$dataTable->getTableId()" sheet-name="Monthly Report"/>Option 2: You can also set the Excel sheet name by overwriting the method.
protected function sheetName() : string
{
return "Yearly Report";
}You can format the column by setting it via Column definition on you DataTable service class.
Column::make('mobile')->exportFormat('00000000000'),The format above will treat mobile numbers as text with leading zeroes.
The package will auto-detect numeric fields and can be used with custom formats.
Column::make('total')->exportFormat('0.00'),
Column::make('count')->exportFormat('#,##0'),
Column::make('average')->exportFormat('#,##0.00'),The package will auto-detect date fields when used with a valid format or is a DateTime instance.
Column::make('report_date')->exportFormat('mm/dd/yyyy'),
Column::make('created_at'),
Column::make('updated_at')->exportFormat(NumberFormat::FORMAT_DATE_DATETIME),Valid date formats can be adjusted on datatables-export.php config file.
'date_formats' => [
'mm/dd/yyyy',
NumberFormat::FORMAT_DATE_DATETIME,
NumberFormat::FORMAT_DATE_YYYYMMDD,
NumberFormat::FORMAT_DATE_XLSX22,
NumberFormat::FORMAT_DATE_DDMMYYYY,
NumberFormat::FORMAT_DATE_DMMINUS,
NumberFormat::FORMAT_DATE_DMYMINUS,
NumberFormat::FORMAT_DATE_DMYSLASH,
NumberFormat::FORMAT_DATE_MYMINUS,
NumberFormat::FORMAT_DATE_TIME1,
NumberFormat::FORMAT_DATE_TIME2,
NumberFormat::FORMAT_DATE_TIME3,
NumberFormat::FORMAT_DATE_TIME4,
NumberFormat::FORMAT_DATE_TIME5,
NumberFormat::FORMAT_DATE_TIME6,
NumberFormat::FORMAT_DATE_TIME7,
NumberFormat::FORMAT_DATE_XLSX14,
NumberFormat::FORMAT_DATE_XLSX15,
NumberFormat::FORMAT_DATE_XLSX16,
NumberFormat::FORMAT_DATE_XLSX17,
NumberFormat::FORMAT_DATE_YYYYMMDD2,
NumberFormat::FORMAT_DATE_YYYYMMDDSLASH,
]Option to force auto-detected numeric value as text format.
Column::make('id')->exportFormat('@'),
Column::make('id')->exportFormat(NumberFormat::FORMAT_GENERAL),
Column::make('id')->exportFormat(NumberFormat::FORMAT_TEXT),Option to automatically download the exported file.
<livewire:export-button :table-id="$dataTable->getTableId()" filename="my-table.xlsx" auto-download="true"/>Please see CONTRIBUTING for details.
If you discover any security related issues, please email aqangeles@gmail.com instead of using the issue tracker.
The MIT License (MIT). Please see License File for more information.