Search code examples
phplaravelcsvexportmaatwebsite-excel

Set background color for heading in export CSV format in laravel maatwebsite


I am trying to set background color for the below export CSV file. But background color is not setting for the first row. Its only works for the xls format. Please help. Thanks in advance.

$fileName = $filename."_".date('dmYHis');                                                                                                                       
        \Excel::create($fileName,function($excel) use($newarray){
            $excel->setTitle('Tasks');
            $excel->setCreator('Admin');
            $excel->setCompany('Company');
            $excel->setDescription('Time Sheet');
            $excel->sheet('sheet1',function($sheet) use($newarray){                             
                $sheet->fromArray($newarray, null, 'A1', false, false);
                $sheet->cells('A1:N1', function($cells) {
                         $cells->setBackground('#AAAAFF'); 
                });
            });

        })->download('csv');                                                            

Solution

  • Comma-separated values (CSV) file stores tabular data (numbers and text) in plain text, so you can't assign a background color in a CSV file. You should use Excel (xls or xlsx) format.