R P
R P

Reputation: 169

Laravel excel export each cell style

How set style for each cells or particular cell in Laravel excel export ??

my code is like this

Excel::create('Output', function($excel) use ($records) {
  $excel->sheet('Records', function($sheet) use ($records) {
    $i = 0;
    foreach ($records as $key => $record) {
       $sheet->row($i, $record);
       $sheet->row($i, function ($row) {
         $row->setFontWeight('bold');
         $row->setBackground('#FF5300');
       });
       $i++;
    }
  });
})->export('xls');

By this i can set style for rows. But i want to set style for each cell.

Upvotes: 1

Views: 7988

Answers (3)

Jahanzaib Jb
Jahanzaib Jb

Reputation: 76

You can do styling in the export file

use Maatwebsite\Excel\Concerns\WithEvents;
use Maatwebsite\Excel\Events\AfterSheet;
public function registerEvents(): array
    {
        return [
                AfterSheet::class    => function(AfterSheet $event) {
                    $cellRange = 'A1:W1';
                    $sheet = $event->sheet;
                    $sheetDelegate = $sheet->getDelegate();
                    $sheet->getStyle('B9')->getFont()->getColor()->setARGB('767171');
                    $sheet->getStyle('B10')->getFont()->setBold(true);

                  }
            },
        ];
    }

Upvotes: 0

Brajesh Kanungo
Brajesh Kanungo

Reputation: 129

Please try this work for me

$sheet->cell('A1', function($cell) 
{

  $cells->setFontWeight('bold');

});

Upvotes: 0

Bizzon
Bizzon

Reputation: 28

need to set style for each cell

   Excel::create('Output', function($excel) use ($records) {
      $excel->sheet('Records', function($sheet) use ($records) {
        $i = 1;
        foreach ($records as $record) {
            $j = 'A';
            foreach($record as $value) {
                $sheet->cell($j.$i, function($cell) use ($value) {
                    $cell->setValue($value);
                    $cell->setBackground('#FF5300');
                });
                $j++;
            }
            $i++;
        }
      });
    })->export('xls');

Upvotes: 1

Related Questions