Laravel-Excel — P10: Export from View

Generate visually rich Excel files directly from your Blade views

We covered exporting items from a database table to a sheet, but we haven’t really covered on how to customize that sheet.

What we want to do is create a unique way to display content, maybe even injecting some items that are not stored in the database. This involves using blade components in Laravel.

One step at a time though. First, we’ll need to create our Exporter.

php artisan make:export ViewExporter --model=User

This is what the command generates.

<?php

namespace App\Exports;

use App\Models\User;
use Maatwebsite\Excel\Concerns\FromCollection;

class ViewExporter implements FromCollection
{
    /**
    * @return \Illuminate\Support\Collection
    */
    public function collection()
    {
        return User::all();
    }
}

But instead of implementing FromCollection we need to implement FromView. The FromView concern requires the view method. It will return a View.

namespace App\Exports;

use App\Models\User;
use Illuminate\Contracts\View\View;
use Maatwebsite\Excel\Concerns\FromView;

class ViewExporter implements FromView
{
    public function view(): View
    {
        // TODO: Implement view() method.
    }
}

Next, we need a view. Create your view the exact same way that you normally create your views. I’m going to create a view called users.blade.php in a subdirectory called exports under views.

<?php
project_name/resources/views/exports/users.blade.php

Inside of the view, we need a table. That table will be translated to data within our Excel sheet. Add your header data in the thead component of the table. Cell data will go inside of our tbody.

<table>
    <thead>
    <tr>
        <th>Full Name</th>
        <th>Email</th>
    </tr>
    </thead>
    <tbody>
        <tr>
            <td>Some Name</td>
            <td>Some Email</td>
        </tr>
    </tbody>
</table>

To call the view, add the following snippet to your Exporter:

return view('exports.users');
<?php

namespace App\Exports;

use App\Models\User;
use Illuminate\Contracts\View\View;
use Maatwebsite\Excel\Concerns\FromView;

class ViewExporter implements FromView
{
    public function view(): View
    {
        return view('exports.users');
    }
}

We just need to create a call to our ViewExporter to initiate the download.

<?php

namespace App\Http\Controllers;

//...
use App\Exports\ViewExporter;
use Illuminate\Http\Request;
use Maatwebsite\Excel\Facades\Excel;

class UserController extends Controller
{
    // ...

    public function export_users_from_view() {
        return Excel::download(
            new ViewExporter(),
            'users.xlsx'
        );
    }
}
<?php

use Illuminate\Support\Facades\Route;
use App\Http\Controllers\UserController;
use Illuminate\Support\Facades\Artisan;

//...

Route::get(
    '/export-users-from-view',
    [UserController::class, 'export_users_from_view']
);

The results that we get are as follows.

To get actual user data to display, we just have to use our Laravel knowledge and pass the User data as an argument to the view. We can then use blade directives to loop through the data.

<?php

namespace App\Exports;

use App\Models\User;
use Illuminate\Contracts\View\View;
use Maatwebsite\Excel\Concerns\FromView;

class ViewExporter implements FromView
{
    public function view(): View
    {
        return view('exports.users', [
            'users' => User::all()
        ]);
    }
}
<table>
    <thead>
    <tr>
        <th>Full Name</th>
        <th>Email</th>
    </tr>
    </thead>
    <tbody>
    @foreach($users as $user)
        <tr>
            <td>{{ $user->name }}</td>
            <td>{{ $user->email }}</td>
        </tr>
    @endforeach
    </tbody>
</table>

Calling the route again yields the following file.

What if we wrapped our headers with strong tags?

<table>
    <thead>
    <tr>
        <th><strong>Full Name</strong></th>
        <th><strong>Email</strong></th>
    </tr>
    </thead>
    <tbody>
    @foreach($users as $user)
        <tr>
            <td>{{ $user->name }}</td>
            <td>{{ $user->email }}</td>
        </tr>
    @endforeach
    </tbody>
</table>

We get bold titles.

 

Have fun experimenting with this.

Laravel Series

Continue your Laravel Learning.

Laravel-Excel — P9: Exports

Effortlessly generate Excel files for reliable data sharing

Laravel-Excel — P9: Exports

In the ninth installment of our Laravel-Excel series, discover how to efficiently generate Excel files from your application’s data. Learn best practices for handling large datasets, customizing export structures, and ensuring consistent, secure file delivery.

 

Laravel-Excel — P10: Export from View

Generate visually rich Excel files directly from your Blade views.

Laravel-Excel — P10: Export from View

In the tenth installment of our Laravel-Excel series, discover how to export data directly from Blade views. Learn to incorporate dynamic HTML content into your spreadsheets, preserving the styling and layout for custom, visually appealing Excel files.

Leave a Reply