Laravel Maatwebsite Excel Fix Header Row using freezePane()

By Hardik Savani April 16, 2024 Category : Laravel

Hello,

Today, i would like to show you laravel maatwebsite excel freeze column. We will look at example of laravel maatwebsite excel fix header row. if you want to see example of laravel excel fix header row then you are a right place. you'll learn laravel maatwebsite freezePane() example. Let's see bellow example freeze panes in excel laravel php.

We will use freezePane() for set fixed rows of heading in laravel maatwesite composer package. you can use this example with laravel 6, laravel 7, laravel 8, laravel 9, laravel 10 and laravel 11 version.

You can follow bellow url for checking full example of Import and Export Excel File with laravel. Here i will give you solution with export class:

Laravel Import Export Excel and CSV File Tutorial

Solution:

app/Exports/UsersExport.php

<?php

namespace App\Exports;

use App\Models\User;

use Maatwebsite\Excel\Concerns\FromCollection;

use Maatwebsite\Excel\Concerns\WithHeadings;

use Maatwebsite\Excel\Concerns\WithEvents;

use Maatwebsite\Excel\Events\AfterSheet;

class UsersExport implements FromCollection, WithHeadings, WithEvents

{

/**

* Write code on Method

*

* @return response()

*/

public function collection()

{

return User::select("id", "name", "email")->get();

}

/**

* Write code on Method

*

* @return response()

*/

public function headings() :array

{

return [

'ID',

'Name',

'Email',

];

}

/**

* Write code on Method

*

* @return response()

*/

public function registerEvents(): array

{

return [

AfterSheet::class => function(AfterSheet $event) {

$event->sheet->getDelegate()->freezePane('A2');

},

];

}

}

Output:

You can see how to use freezePane() in Advance:

Freezing Lines

Freeze first line:

$event->sheet->getDelegate()->freezePane('A2');

Freeze second line:

$event->sheet->getDelegate()->freezePane('A3');

Freezing Columns

Freeze first column:

$event->sheet->getDelegate()->freezePane('B1');

Freeze second column:

$event->sheet->getDelegate()->freezePane('C1');

Freezing Columns and Lines

Freeze first column and first line:

$event->sheet->getDelegate()->freezePane('B2');

Freeze fourth column and first line:

$event->sheet->getDelegate()->freezePane('D2');

I hope it can help you...

Tags :
Shares