4

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

cokeman19
  • 2,405
  • 1
  • 25
  • 40
Yuda Pratama
  • 41
  • 1
  • 3

4 Answers4

4

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.

Dmitry Gultyaev
  • 111
  • 1
  • 3
0

For me I have added event by extends WithEvents and use

 use Maatwebsite\Excel\Concerns\WithEvents;

Then I apply like this....

public function registerEvents(): array
{
    $alphabetRange  = range('A', 'Z');
    $alphabet       = $alphabetRange[$this->totalValue+6]; // returns Alphabet

    $totalRow       = (count($this->attributeSets) * 3) + count($this->allItems)+1;
    $cellRange      = 'A1:'.$alphabet.$totalRow;

    return [
        AfterSheet::class    => function(AfterSheet $event) use($cellRange) {
            $event->sheet->getStyle($cellRange)->applyFromArray([
                'borders' => [
                    'allBorders' => [
                        'borderStyle' => \PhpOffice\PhpSpreadsheet\Style\Border::BORDER_THIN,
                        'color' => ['argb' => '000000'],
                    ],
                ],
            ])->getAlignment()->setWrapText(true);
        },
    ];
}

For Details or more you can follow The doc https://docs.laravel-excel.com/3.1/getting-started/

Zahid Hassan Shaikot
  • 1,066
  • 10
  • 18
0

@Zahid answer little bit update for v3.1

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

                    $alphabet       = $event->sheet->getHighestDataColumn();
                    $totalRow       = $event->sheet->getHighestDataRow();
                    $cellRange      = 'A7:'.$alphabet.$totalRow;

                    $event->sheet->getStyle($cellRange)->applyFromArray([
                        'borders' => [
                            'allBorders' => [
                                'borderStyle' => Border::BORDER_HAIR,
                            ],
                        ],
                    ])->getAlignment()->setWrapText(true);

                },
            ];
        }
NC64
  • 369
  • 5
  • 12
-1

I think this might help you what you looking for : phpspreadsheet simpler

There is a method

// $titleName : pass data that you wants to set (not array)
// $mergeTill : pass count of array dataset (Int)

public function setTitle($titleName, $mergeTill, $startCell ,$styleName = "")

go through this example given link

Pragnesh P
  • 155
  • 2
  • 13