PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Sunday, January 2, 2022

[FIXED] How to set background color for row in Laravel Excel?

 January 02, 2022     laravel, laravel-5, laravel-excel     No comments   

Issue

I use Laravel Excel library and I have tried this code:

public function registerEvents(): array
{
    return [
        AfterSheet::class    => function(AfterSheet $event) {

            $styleArray =  array('fill' => array(
                'color' => array('rgb' => '000000')
            ));

            $cellRange = 'A1:W1'; // All headers
            $event->sheet->getDelegate()->getStyle($cellRange)->applyFromArray($styleArray);
        },
    ];
}

As result I get headers without black background color.

Also I tried this array settings:

$styleArray = [
                    'font' => [
                        'bold' => true,
                    ],
                    'background' => [
                        'color'=> '#000000'
                    ]
                ];

I use events, not creating. Please don't recommend not relevant answers


Solution

Try this

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

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

Then You can also use a more Excel-ish notation :

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


Answered By - sss S
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing