How to Set Background Color in Laravel Maatwebsite Excel ?

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

Today now in this post i will show How to Set Background Color in Laravel Maatwebsite Excel ? Here, i will know about laravel maatwebsite excel how to set background color. if you have any question about laravel excel sheet how to set background color then here i will give you a simple example also with solution. So now in this article i will go in detailed on about how to set background color in excel cell heading in laravel php. Here i will show this example step by step also with explain php excel set background color in laravel. So let’s get started with the laravel maatwebsite background color.

Here We will use WithEvents and AfterSheet method for set background color of column in laravel maatwesite composer package. So we can use this example with any version of laravel such as 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()->getStyle('A1:C1')
                        ->getFill()
                        ->setFillType(\PhpOffice\PhpSpreadsheet\Style\Fill::FILL_SOLID)
                        ->getStartColor()
                        ->setARGB('DD4B39');
  
            },
        ];
    }
}

Read Also :Laravel Join with Subquery in Query Builder Example

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 →