konrms
1 month ago
170
4
Laravel

Formatting exported excel file

Posted 1 month ago by konrms

Hello guys!

I use the maatwebsite/excel package to produce excel files from database tables. The export is successful, but I need some help with formatting it.

In fact I want the excel to have bigger font size for headings, highlighted headings and borders.

So far I have succeeded only in creating bigger size header fonts using setSize() as you can see in my code excerpt.

My exports file (C:\xampp\htdocs\laravel\app\Exports\MembersExport.php) is the following:

<?php

namespace App\Exports;

use App\Member;
use Maatwebsite\Excel\Concerns\FromCollection;
use Maatwebsite\Excel\Concerns\WithHeadings; 
use Maatwebsite\Excel\Concerns\ShouldAutoSize;

use Maatwebsite\Excel\Concerns\WithEvents;
use Maatwebsite\Excel\Events\AfterSheet;

class MembersExport implements FromCollection, WithHeadings, ShouldAutoSize, WithEvents
{
    
    public function collection()
    {
        return Member::all();
    }
    
    public function headings(): array
    {
        return 
        [
            'head1',
            'head2',
            'head3',
            'head4',
            'head5',
        'head6',
        'head7',
        ];
    }
    
     public function registerEvents(): array
        {
        return [
            AfterSheet::class    => function(AfterSheet $event) 
            {

                       $cellRange = 'A1:G1'; // All headers
                       $event->sheet->getDelegate()->getStyle($cellRange)->getFont()->setName('Calibri');
               $event->sheet->getDelegate()->getStyle($cellRange)->getFont()->setSize(14);

        
            },
              ];
       }
    
    
    
}

For example I have found the following code in a guide. How can I integrate it in the excerpt above?

$styleArray = [
    'borders' => [
        'outline' => [
            'borderStyle' => \PhpOffice\PhpSpreadsheet\Style\Border::BORDER_THICK,
            'color' => ['argb' => 'FFFF0000'],
        ],
    ],
];

$worksheet->getStyle('A1:G1')->applyFromArray($styleArray);

Thanks a lot!

Please sign in or create an account to participate in this conversation.