1 year ago

#315010

test-img

Abdurrahman Naufal

Auto Merge Row Data in Maatwebsite Export Excel

I have dynamic data that will be exported into excel using maatwebsite. I have a case where I need to merge the same data. How can I do it from a collection?

From This:

Old

To This: New

class PerencanaanExport implements
    FromCollection,
    WithCustomStartCell,
    WithEvents,
    ShouldAutoSize,
    WithStyles
{
    /**
     * @return \Illuminate\Support\Collection
     */
    public function __construct(string $tahun)
    {
        $this->tahun = $tahun;
    }

    public function startCell(): string
    {
        return "A3";
    }

    public function registerEvents(): array
    {
        return [
            AfterSheet::class => function (AfterSheet $event) {
                /** @var Sheet $sheet */
                $tahun =
                    $this->tahun == "null"
                        ? ""
                        : ($this->tahun == "all"
                            ? ""
                            : $this->tahun);
                $sheet = $event->sheet;

                $sheet->mergeCells("A1:E1");
                $sheet->setCellValue("A1", "Kode");
                $sheet->setCellValue("A2", "Urusan");
                $sheet->setCellValue("B2", "Bidang Urusan");
                $sheet->setCellValue("C2", "Program");
                $sheet->setCellValue("D2", "Kegiatan");
                $sheet->setCellValue("E2", "Sub Kegiatan");

                $sheet->mergeCells("F1:F2");
                $sheet->setCellValue(
                    "F1",
                    "Urusan/ Bidang Urusan Pemerintahan Daerah dan Program Kegiatan"
                );

                $sheet->mergeCells("G1:G2");
                $sheet->setCellValue(
                    "G1",
                    "Indikator Kinerja Program/ Kegiatan"
                );

                $sheet->mergeCells("H1:K1");
                $sheet->setCellValue("H1", "Rencana Tahun " . $tahun);
                $sheet->setCellValue("H2", "LOKASI");
                $sheet->setCellValue("I2", "TARGET KINERJA");
                $sheet->setCellValue("J2", "ANGGARAN");
                $sheet->setCellValue("K2", "SUMBER DANA");

                $styleArray = [
                    "borders" => [
                        "allBorders" => [
                            "borderStyle" =>
                                \PhpOffice\PhpSpreadsheet\Style\Border::BORDER_THIN,
                            "color" => ["argb" => "000000"],
                        ],
                    ],
                ];

                $cellRange = "A1:K" . $sheet->getHighestRow(); // All headers
                $event->sheet
                    ->getDelegate()
                    ->getStyle($cellRange)
                    ->applyFromArray($styleArray);
                $event->sheet
                    ->getStyle("A1:K2")
                    ->getAlignment()
                    ->setVertical(StyleAlignment::VERTICAL_TOP)
                    ->setHorizontal(StyleAlignment::VERTICAL_CENTER)
                    ->setWrapText(true);
            },
        ];
    }

    public function collection()
    {
        $arr = [];
        $tahun = $this->tahun;
        $jointahun = "";
        if ($tahun) {
            $jointahun = " AND d.tahun =" . $tahun;
        }
        $query = "XXXXX";
        $query .= "XXXXX";
        $data = DB::select($query);

        $oldProgram = "";
        $oldKegiatan = "";
        foreach ($data as $key => $value) {
            if ($key != 0) {
                $oldProgram = $data[$key - 1]->program;
                $oldKegiatan = $data[$key - 1]->kegiatan;
            }
            if ($oldProgram != $value->program) {
                array_push($arr, [
                    $value->kode_urusan,
                    $value->kode_bidang_urusan,
                    $value->kode_program,
                    "",
                    "",
                    $value->program,
                ]);
            }
            if ($oldKegiatan != $value->kegiatan) {
                array_push($arr, [
                    $value->kode_urusan,
                    $value->kode_bidang_urusan,
                    $value->kode_program,
                    $value->kode_kegiatan,
                    "",
                    $value->kegiatan,
                    $value->indikator_kegiatan,
                ]);
            }
            array_push($arr, [
                $value->kode_urusan,
                $value->kode_bidang_urusan,
                $value->kode_program,
                $value->kode_kegiatan,
                $value->kode_sub_kegiatan,
                $value->sub_kegiatan,
                $value->indikator_perencanaan,
                $value->lokasi,
                $value->target_kinerja,
                $value->anggaran,
                $value->sumber_dana,
            ]);
        }

        return collect($arr);
    }

    public function styles(Worksheet $sheet)
    {
        $sheet
            ->getStyle("1:2")
            ->getFont()
            ->setBold(true);
    }

php

laravel

export-to-excel

maatwebsite-excel

0 Answers

Your Answer

Accepted video resources