How to Fix Header Row using freezePane() in Laravel Maatwebsite Excel?

Share Me
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  

Today now in this post i will show you How to Fix Header Row using freezePane() in Laravel Maatwebsite Excel?Here i would like to share with you about laravel maatwebsite excel how to freeze column. Now here we will look at an example of laravel maatwebsite excel how to fix header row. If you want to know about an example of laravel excel fix header row then i hope you are in right place. Here you’ll learn about laravel maatwebsite freezePane() example. So Let’s see the bellow example freeze panes in excel laravel php.

So we can use freezePane() function for set fixed rows of heading in laravel maatwesite composer package. you can use this example with any version of laravel as like laravel 6, laravel 7 and laravel 8 version.

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');
   
            },
        ];
    }
}

So we can know here how to use freezePane() in Advance:

Freezing Lines

How to Freeze first line:

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

How to Freeze second line:

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

How to Freeze first column:

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

How to Freeze second column:

$event->sheet->getDelegate()->freezePane('C1');
Freezing Columns and Lines

How to Freeze first column and first line:

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

How to Freeze fourth column and first line:

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

Read Also : How to Set Font Color in Laravel Maatwebsite Excel ?

Thanks for read. I hope it help you. For more you can follow us on facebook.

About Shahriar Sagor

My name is Shahriar sagor. I'm a developer. I live in Bangladesh and I love to write tutorials and tips that will help to other Developer's. I am a big fan of PHP, Javascript, JQuery, Laravel, Codeigniter, VueJS, AngularJS and Bootstrap from the early stage.

View all posts by Shahriar Sagor →