Laravel Excel Column Width Auto

Laravel Excel – Auto Column Width

Laravel Excel is a powerful package that allows you to easily import and export Excel files in Laravel applications. By default, when exporting data to Excel using Laravel Excel, the width of the columns is not automatically adjusted according to the content.

To enable auto column width adjustment while exporting data to Excel using Laravel Excel, you can make use of the `AutoSize` class provided by the package.

Step 1: Install Laravel Excel

To begin, first, you need to install Laravel Excel package. You can do this via Composer by running the following command:

composer require maatwebsite/excel

Step 2: Create Export Class

Next, you need to create an export class that extends the `Maatwebsite\Excel\Concerns\FromCollection` interface. This class will define the data that you want to export to Excel. You can create a new export class using the following command:

php artisan make:export UsersExport --model=User

This command will create a new `UsersExport` class inside the `app/Exports` directory. Open the `UsersExport` class and modify it as follows:

<?php

namespace App\Exports;

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

class UsersExport implements FromView, WithAutoSizeColumns
{
    public function view(): View
    {
        return view('exports.users', [
            'users' => User::all()
        ]);
    }
    
    public function autoSizeColumns(): array
    {
        return ['A', 'B', 'C']; // Range of columns that you want to auto-size
    }
}

In the `UsersExport` class above, we implement the `FromView` interface to define the data that we want to export. We also implement the `WithAutoSizeColumns` interface to enable auto column width adjustment for specified columns in the `autoSizeColumns()` method. In this example, we have specified columns ‘A’, ‘B’, ‘C’ for auto-sizing.

Step 3: Create Export View

Next, you need to create a view file that defines the layout and data for exporting to Excel. Create a new Blade view file in the `resources/views/exports` directory (if not exist) with the name `users.blade.php` and add the following code:

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

In the above view file, we have defined a basic HTML table structure and output the user data within the table rows. Feel free to customize the view as per your requirements.

Step 4: Export Data to Excel

Finally, to export the data to Excel with auto column width adjustment, you can make use of the `download()` method provided by Laravel Excel package. Modify your controller or route method as follows:

<?php

namespace App\Http\Controllers;

use App\Exports\UsersExport;
use Maatwebsite\Excel\Facades\Excel;

class ExportController extends Controller
{
    public function export()
    {
        return Excel::download(new UsersExport(), 'users.xlsx');
    }
}

In the `export()` method above, we are using Laravel Excel’s `download()` method to initiate the export process with the `UsersExport` class we created earlier. The exported file will be saved as `users.xlsx` in this example.

Conclusion

That’s it! You have learned how to enable auto column width adjustment while exporting data to Excel using Laravel Excel. By implementing the `WithAutoSizeColumns` interface and specifying the range of columns in the export class, the column widths will automatically adjust according to the content in those columns.

Thanks for reading!

Similar post

Leave a comment