How to export datatables in multiple tabs in laravel?

Member

by brandy , in category: PHP Frameworks , 14 days ago

How to export datatables in multiple tabs in laravel?

Facebook Twitter LinkedIn Telegram Whatsapp

1 answer

Member

by denis , 13 days ago

@brandy 

To export data tables in multiple tabs in Laravel, you can use the Laravel Excel package. Here's a step-by-step guide to achieve this:

  1. Install Laravel Excel package by running the following composer command:
1
composer require maatwebsite/excel


  1. After installing the package, publish the configuration file by running the following command:
1
php artisan vendor:publish --provider="MaatwebsiteExcelExcelServiceProvider"


  1. Create a new export class for each data table you want to export. You can do this by running the following command:
1
php artisan make:export DataTable1Export --model=DataTable1Model


  1. In the export class, define the sheets method to export data in multiple tabs. Here's an example of how to export data in multiple tabs:
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
use MaatwebsiteExcelConcernsExportable;
use MaatwebsiteExcelConcernsWithMultipleSheets;
use MaatwebsiteExcelSheet;

class ExportMultipleTabs implements WithMultipleSheets
{
    use Exportable;

    public function sheets(): array
    {
        $sheets = [];

        $sheets[] = new DataTable1Export(); // Export data table 1 in the first tab
        $sheets[] = new DataTable2Export(); // Export data table 2 in the second tab

        return $sheets;
    }
}


  1. To export the data tables in multiple tabs, you can use the following code in your controller or route:
1
2
3
4
5
6
7
use AppExportsExportMultipleTabs;
use MaatwebsiteExcelFacadesExcel;

public function exportMultipleTabs()
{
    return Excel::download(new ExportMultipleTabs, 'multiple_tabs.xlsx');
}


  1. Finally, create a route to trigger the export function:
1
Route::get('/export-multiple-tabs', 'YourController@exportMultipleTabs');


After following these steps, your data tables will be exported in multiple tabs in an Excel file when you visit the specified route.