How to set background color for row in Laravel Excel?

Stefan Bogdanescu

Founder & Senior Architect · 2026-06-29

Laravel Company
Title: Efficiently Set Background Color for Rows Using Laravel Excel's Events Handling Body:

If you are working on a project that involves utilizing the Laravel Excel library, you might have encountered situations where you need to set background colors for specific rows in your spreadsheets. This blog post will provide a comprehensive solution to this problem using Laravel Excel's powerful events handling feature.

Preparing the Events Class

First, create a new Events class in your application. This class will be responsible for listening to specific events and executing tasks according to their types:
use Maatwebsite\Excel\Events\AfterSheet;

class ExcelEvents
{
    public function registerEvents(): array
    {
        return [
            AfterSheet::class => [
                'methodName' => 'afterSheetCallback',
            ],
        ];
    }
}
Here, we use the registerEvents() method to register our custom events. We pass in an array with the event name as key and callable function as value. In this case, we are listening for the AfterSheet event and assigning a custom callback function called afterSheetCallback().

Creating the Callback Functions

Now, create the following functions: 1. The setHeaderRowBackgroundColor() function to set background colors for a specific range of cells (headers). 2. The afterSheetCallback() function to be called after all the columns are exported and ready for use. In this callback, we can apply our custom styles.
public function setHeaderRowBackgroundColor(Worksheet $worksheet)
{
    $cellRange = 'A1:W1'; // All headers
    $styleArray = [
        'fill' => [
            'color' => [
                'rgb' => '000000',
            ],
        ],
    ];

    $worksheet->getDelegate()->getStyle($cellRange)->applyFromArray($styleArray);
}

public function afterSheetCallback(AfterSheet $event)
{
    $this->setHeaderRowBackgroundColor($event->sheet);
}
In the afterSheetCallback(), we call the custom function that sets background colors for a specific range of cells. We use the $cellRange property to define our desired range and apply the desired style array (fill color) using the applyFromArray() method.

Registering the Events with Laravel Excel

Finally, include our newly created ExcelEvents class in your application's service provider. You can inject it as a dependency and register your events:
use Maatwebsite\Excel\Events\AfterSheet;
use App\Services\ExcelEvents;

class ExportServiceProvider extends ServiceProvider
{
    public function boot()
    {
        $this->app->bind(ExcelEvents::class, function () {
            return new ExcelEvents();
        });
    }
}
In conclusion, by using Laravel Excel's events handling feature and creating custom functions to handle specific tasks, you can now set background colors for rows in your spreadsheets with ease. Feel free to explore more options within the Laravel Excel library and apply them to enhance the functionality of your applications.