Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to make all cells wrap text and have complete all borders in Laravel Excel

I want to make export results with Laravel Excel, but the results did not match what I wanted. enter image description here

what I want is like this enter image description here

I want all cells to be wrapped in text and have a complete border.

This is my code:

class SdgsExportView2 implements FromView, WithTitle {

    protected $id_perangkat_daerah;

    function __construct($id_perangkat_daerah) {
        $this->id_perangkat_daerah = $id_perangkat_daerah;
    }

    public function view(): View
    {
        $data_sdgs = DataSdgs::where('id_perangkat_daerah',$this->id_perangkat_daerah)
        ->with('indikator.target.sdgs')
        ->with('detail_data_sdgs.kegiatan.program.perangkat_daerah')
        ->get();
        return view('template.exportMatrik2', [
            'data_sdgs' => $data_sdgs
        ]);
    }

    public function registerEvents(): array
    {
        return [
            AfterSheet::class    => function(AfterSheet $event) {
                $cellRange = 'A1:W100'; // All headers
                $event->sheet->getDelegate()->getStyle($cellRange)->getFont()->setSize(14);
                $event->sheet->getDelegate()->getStyle($cellRange)->getAlignment()->setWrapText(true);
            }
        ];
    }

    public function title() : string
    {
        return 'MATRIK 2';
    }
}

I really hope for your help

like image 661
Yuda Pratama Avatar asked Oct 20 '25 12:10

Yuda Pratama


1 Answers

I know it is a bit late to answer (after 8 months), but to add border format to your code is relatively simple. In your event function AfterSheet::class => function(AfterSheet $event) you need to add following code:

    $styleHeader = [
                'borders' => [
                    'allBorders' => [
                        'borderStyle' => 'thin',
                        'color' => ['rgb' => '808080']
                    ],
                ]
            ];
    $event->sheet->getStyle("A1:C1")->applyFromArray($styleHeader);

And replace "A1:C1" with your header range.

like image 199
Dmitry Gultyaev Avatar answered Oct 23 '25 08:10

Dmitry Gultyaev



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!