Laravel excel multi sheet using view return multi sheets but empty

Stefan Bogdanescu

Founder & Senior Architect · 2026-06-29

Laravel Company

Mastering Multi-Sheet Exports in Laravel: Leveraging FromView and WithMultipleSheets

As developers working with large datasets, exporting information into structured formats like Excel is a common requirement. When dealing with complex reports that require multiple, distinct views or data sets—such as generating separate sheets for different analytical breakdowns—standard export methods can become cumbersome. This is where powerful packages like Maatwebsite\Excel in the Laravel ecosystem shine, offering elegant solutions through features like FromView and WithMultipleSheets.

This post will dive deep into how you can use these features to generate multiple Excel workbooks (sheets) dynamically from different Blade views within a single export process.

The Challenge: Exporting Multiple Views Simultaneously

Imagine you have several analytical dashboards, each requiring its own summary sheet in an Excel file. You don't want to write five separate export classes or manually manage the opening and closing of Excel files for each view. You want one cohesive process that spits out five distinct sheets.

The core challenge is marrying the dynamic data generation (the Blade views) with the structure definition (specifying which sheet corresponds to which view). Laravel Excel provides the exact tools to solve this efficiently.

The Solution: Implementing FromView and WithMultipleSheets

To achieve multi-sheet exporting from multiple views, you need to implement two key interfaces provided by the Maatwebsite\Excel package: FromView and WithMultipleSheets.

1. Using FromView for Dynamic Data Generation

The FromView contract allows you to pull data directly from a Blade view during the export process. This is perfect when your report sections are visually defined in separate files. Inside the view() method, you render the necessary HTML content and pass the required data to it.

2. Defining Structure with WithMultipleSheets

The WithMultipleSheets contract tells the Excel writer that this export operation will result in more than one sheet. The crucial part is the sheets() method, which must return an array of strings—the names of the sheets you wish to generate.

By combining these two methods, you instruct Laravel Excel to execute your data retrieval logic (in view()) and then map the resulting views into distinct sheets within the final workbook.

Code Deep Dive: Structuring Your Export Class

Let's examine how this concept translates into practice, using your provided example structure for an export class designed to generate multiple reports:

use Maatwebsite\Excel\Concerns\FromView;
use Maatwebsite\Excel\Concerns\WithMultipleSheets;
use Maatwebsite\Excel\Excel;

class ScansExport implements FromView, WithMultipleSheets {
    // ... constructor and other methods

    public function view(): View {
        // 1. Generate all necessary data for the report(s) here.
        $data = []; // Initialize an array to hold all data sets

        // Example: Fetch data for the first sheet (e.g., Countries)
        $data['countries'] = $this->getCountryData(); // Assume this method calculates the required stats

        // Example: Fetch data for the second sheet (e.g., Cities)
        $data['cities'] = $this->getCityData(); 

        // ... populate $data with all calculated results

        return $data;
    }

    public function sheets(): array {
        // 2. Define which views/data sets become separate sheets.
        return [
            'Countries Report', // This will be Sheet 1
            'Cities Report',    // This will be Sheet 2
        ];
    }
}

In this setup, the view() method handles the heavy lifting—calculating all the necessary metrics (like country counts, city counts, and time-series data points, as seen in your original complex logic). The sheets() method then dictates the final output structure. When Laravel Excel processes this class, it executes view() to get the raw data, and then uses sheets() to create distinct tabs for each piece of data generated.

Best Practices for Complex Exports

When dealing with intricate exports involving multiple views, keep these best practices in mind:

  1. Separate Logic: Keep your complex data aggregation logic entirely within the view() method. This separates what you are exporting from how it is formatted into sheets.
  2. Naming Convention: Use descriptive names in your sheets() array. This makes the final Excel file immediately understandable to the end-user.
  3. Data Consistency: Ensure that the data passed back from view() is structured consistently. Since you are pulling data from different report views, ensure all returned arrays or objects conform to a predictable structure so the Excel writer can map them correctly.

Conclusion

Leveraging Laravel Excel's FromView and WithMultipleSheets contracts transforms complex reporting into streamlined processes. By separating the concerns—data preparation in view() and sheet definition in sheets()—you create export classes that are not only powerful but also highly maintainable. This approach allows you to generate sophisticated, multi-sheet Excel reports directly from your Laravel application, significantly improving the user experience for data consumption. For more deep dives into building robust data pipelines within Laravel, exploring resources from laravelcompany.com is always recommended.