Laravel-excel: First row, background color with values

Created on 9 Jun 2015  路  10Comments  路  Source: Maatwebsite/Laravel-Excel

I'm probably being dumb, but how can I set the background color on the first row of an Excel export, and add the values I want to the cells on that row?

The documentation says you can do this:

$sheet->row(1, function($row) {

    // call cell manipulation methods
    $row->setBackground('#000000');

});

But where do I call the method that takes the values?

Most helpful comment

@die660

$sheet->row(1, ['Col 1', 'Col 2', 'Col 3']); // etc etc
$sheet->row(1, function($row) { $row->setBackground('#CCCCCC'); });

All 10 comments

Found the solution.

what is the solution?

@die660

$sheet->row(1, ['Col 1', 'Col 2', 'Col 3']); // etc etc
$sheet->row(1, function($row) { $row->setBackground('#CCCCCC'); });

thank you

You can also change $sheet->row() to $sheet->cell() and keep passing a row number as first argument.

$sheet->cell(1, function($row) { 
    $row->setBackground('#CCCCCC'); 
});

The documentation does't mention it. I don't know how or why it works, but it does.

You can also use a more Excel-ish notation :

$sheet->cells('A1:D1', function ($cells) {
    $cells->setBackground('#008686');
    $cells->setAlignment('center');
});

thanks

@dsampaolo where is this code used within the project? I can't find anywhere in the docs that explains where to use it

If you are working with version 3.1 you need to use event triggers and set the following;

...
public static function afterSheet(AfterSheet $event)
    {
            $event->sheet->getDelegate()->getStyle('A1:'.$event->sheet->getDelegate()->getHighestColumn().'1')
            ->getFill()->setFillType(Fill::FILL_SOLID)->getStartColor()->setARGB('ffff15');
    }
...

This will highlight the whole row 1 upto the highest column as per the data contained.
Details on events :
https://docs.laravel-excel.com/3.1/exports/extending.html#events

for Particular cell ,

$sheet->cell(1, function($row) {
$row->setBackground('#CCCCCC');
});

Was this page helpful?
0 / 5 - 0 ratings