Laravel excel export multiple sheets example

Learn how to export data in Excel or CSV format with Laravel Excel.

Laravel excel export multiple sheets example

☑ Laravel excel multiple sheets export example

☑ Export large data

☑ Display logo in excel sheet

☑ format cell in excel (eg: bold, border, color, etc)

Excel Export provides a way to export an Excel file with multiple sheets. This can be useful when you need to export data from different grids into a single Excel file.

How it works

Exporting the grid into different sheets follows a specific process:

  1. You start the process by calling the getSheetDataForExcel method on a grid instance to get the data exported for a specific sheet.
  2. You call this method multiple times either on the same grid with different data (or different export params) or on different instances of the grid, and you store each exported data set as an element of an Array.
  3. Once all the needed sheets have been stored in the Array, call the exportMultipleSheetsAsExcel or getMultipleSheetsAsExcel methods to package them in a single Excel workbook.

When using modules, the exportMultipleSheetsAsExcel and getMultipleSheetsAsExcel functions can be imported directly from the excel-export module as import { exportMultipleSheetsAsExcel, getMultipleSheetsAsExcel } from '@ag-grid-enterprise/excel-export'.

Calling getSheetDataForExcel starts a Multiple Sheet export process, that can only be ended by calling the exportMultipleSheetsAsExcel or getMultipleSheetsAsExcel methods. Until one of these two methods is called to complete the process, no data can be exported from the grid using exportDataAsExcel or getDataAsExcel.

Using Selected Rows

In this example, we use the onlySelected=true property to segment the grid data into multiple sheets, each containing 100 data rows.

Note the following:

  • The header is exported on each page, so each page will contain 101 records (including the header).
  • Because each export did not have a specified sheetName, they will be named ag-grid, ag-grid_1, ag-grid_2 and so on.

Using Data Filtering

In this example, we filter on the sport column to segment the grid data into multiple sheets, each containing all the data for a specific sport value.

Note the following:

  • The exported Excel file will contain one sheet for each sport result.
  • Each sheet was exported using the sport name as the name of the sheet.

Multiple Grids to Multiple Sheets

In this example, we export two grids, each into a separate sheet of the same Excel file. Drag a few rows from the grid on the left into the grid on the right and click the export button above the grid.

Note the following:

  • The contents of the Athletes grid will be exported to the Athletes sheet.
  • The contents of the Selected Athletes grid will be exported to the Selected Athletes sheet.
  • Only the onExcelExport method is relevant to Excel Export

API

API Methods

Next Up

Continue to the next section: Rows.

  • Excel Export - Multiple Sheets
  • How it works
  • Using Selected Rows
  • Using Data Filtering
  • Multiple Grids to Multiple Sheets
  • API
  • API Methods
  • Next Up

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

Pick a username Email AddressPassword

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Chinweuba Elijah Azubuike

Overview

In this shot, we will learn how to export multiple models from a database to an Excel sheet, and also name the sheet.

We will use the FastExcel package to carry out this task.

Package installation

You can use a composer command to install the package:

composer require rap2hpoutre/fast-excel

Code example

<?php

namespace use App\Models\User;
use App\Models\Project;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use Rap2hpoutre\FastExcel\FastExcel;
use 
class exportModels extends Controller
{
  public function index(){
$sheets = new SheetCollection([// creating a sheet collection
    'Users' => User::all(),//Fetch users model with sheet name as user
    'Second sheet' => Project::all()//Fetch project model with sheet name as project 
]);
(new FastExcel($sheets))->export('file.xlsx');// passing the created sheet model and saving the file as `*file.xlsx*
}
}

Explanation

In the example above, we create a sheet collection that helps save both the users and projects models. Then, we pass the models to FastExcel, which fetches the model data to the Excel sheet alongside the export method, which allows us to name the file and save it.

RELATED TAGS

communitycreator

laravel

php

CONTRIBUTOR

Chinweuba Elijah Azubuike

How do I export multiple sheets in Excel laravel?

To allow the export to have multiple sheets, the WithMultipleSheets concern should be used. The sheets() method expects an array of sheet export objects to be returned. The InvoicesPerMonthSheet can implement concerns like FromQuery , FromCollection , ...

How can I export multiple sheets in Excel using PHP?

php file, in this file first we have make database connection, and fetch data from customer table and display on web page. After this we have make form with input select option for select how many records you want to export in single Excel file and below you can find submit button.

How do I export data from laravel to Excel?

You can read about Laravel Import From CSV/ Excel Files Here!.
Step 1 - Installation. To Install the Laravel Excel Package via composer run command below. ... .
Step 2 - Create an Exports Class inside app/Exports. Create Exports Class by using artisan command. ... .
Step 3 - Handle Export To Excel Function..

Can you import data to multiple tables from one Excel spreadsheet using laravel?

Below you can find complete step by step process of how to use Laravel Maatwebsite package for import Excel sheet data into multiple tables of database in Laravel. This will create two tables named as customers and courses.