Laravel Maatwebsite Excel Set Background Color Example

Here, i will show you laravel maatwebsite excel set background color. if you have question about laravel excel sheet set background color then i will give simple example with solution. This article goes in detailed on how to set background color in excel cell heading laravel php. step by step explain phpexcel set background color laravel. Let’s get started with laravel maatwebsite background color.

We will use WithEvents and AfterSheet for set background color of column in laravel maatwesite composer package. you can use this example with laravel 6, laravel 7 and laravel 8 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 AppExports;

use AppModelsUser;

use MaatwebsiteExcelConcernsFromCollection;

use MaatwebsiteExcelConcernsWithHeadings;

use MaatwebsiteExcelConcernsWithEvents;

use MaatwebsiteExcelEventsAfterSheet;

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(PhpOfficePhpSpreadsheetStyleFill::FILL_SOLID)

->getStartColor()

->setARGB('DD4B39');

},

];

}

}

Output:

Laravel Maatwebsite Excel Set Background Color Example

I hope it can help you…

Leave a Comment