Skip to content
Advertisement

Laravel Export : Problem with the excel formatting

I’m having a problem with the excel formatting of export operation, here’s my code

SubmissionDetailExportExcel.php

<?php

namespace AppExports;

use AppHttpRepositoryPengajuanDetailRepository;
use AppSubmission;
use IlluminateContractsViewView;
use MaatwebsiteExcelConcernsFromView;
use MaatwebsiteExcelConcernsWithEvents;
use MaatwebsiteExcelEventsAfterSheet;
use PhpOfficePhpSpreadsheetCellCoordinate;
use PhpOfficePhpSpreadsheetStyleAlignment;

class SubmissionDetailExportExcel implements FromView, WithEvents
{
    private $submission;
    private $submissionDetail;

    /**
     * RealizationExport constructor.
     * @param $submission
     */
    public function __construct(Submission $submission)
    {
        $realisasiRepository = new PengajuanDetailRepository();
        $this->submissionDetail = $realisasiRepository->getBySubmission($submission);
        $this->submission = $submission->load('programStudies');
    }

    /**
     * @inheritDoc
     */
    public function view(): View
    {
        $submissionDetail = $this->submissionDetail;
        return view('exports.pengajuan_detail', compact('submissionDetail'));
    }

    /**
     * @inheritDoc
     */
    public function registerEvents(): array
    {
        return [
            AfterSheet::class => function (AfterSheet $event) {
                $lastColumn = Coordinate::stringFromColumnIndex($this->submissionDetail[0]->count() + 1);
                $beforeLastColumn = Coordinate::stringFromColumnIndex($this->submissionDetail[0]->count());

                $lastRow = $this->submissionDetail->count() + 5 + 2 + 1;

                $styleTextCenter = [
                    'alignment' => [
                        'horizontal' => Alignment::HORIZONTAL_CENTER
                    ],
                    'font' => [
                        'bold' => true
                    ]
                ];

                $styleTextLeft = [
                    'alignment' => [
                        'horizontal' => Alignment::HORIZONTAL_LEFT
                    ]
                ];

                $styleTextRight = [
                    'alignment' => [
                        'horizontal' => Alignment::HORIZONTAL_RIGHT
                    ],
                    'font' => [
                        'bold' => true
                    ]
                ];

                $event->sheet->insertNewRowBefore(1, 5);

                $event->sheet->mergeCells(sprintf('A1:%s1', $lastColumn));
                $event->sheet->mergeCells(sprintf('A2:%s2', $lastColumn));
                $event->sheet->mergeCells(sprintf('A%d:%s%d', $lastRow, $beforeLastColumn, $lastRow));

                $event->sheet->setCellValue('A1', 'Laporan Detail Pengajuan Prodi ' . ucwords($this->submission->programStudies->implode('nama_prodi', ', ')));
                $event->sheet->setCellValue('A2', 'Tahun Akademik ' . $this->submission->tahun_akademik . ' Semester ' . ucwords($this->submission->semester));
                $event->sheet->setCellValue('A3', 'Siswa');
                $event->sheet->setCellValue('B3', $this->submission->siswa);
                $event->sheet->setCellValue('A4', 'Pagu');
                $event->sheet->setCellValue('B4', $this->submission->pagu);
                $event->sheet->setCellValue(sprintf('A%d', $lastRow), 'Total Biaya');

                $percent = round((($this->submissionDetail->sum('harga_total') / $this->submission->pagu) * 100), 2) . '%';
                $event->sheet->setCellValue(sprintf('%s%d', $lastColumn, $lastRow), $this->submissionDetail->sum('harga_total') . ' ( ' . $percent . ' )');

                $event->sheet->getStyle('A1:A2')->applyFromArray($styleTextCenter);
                $event->sheet->getStyle('B3:B4')->applyFromArray($styleTextLeft);
                $event->sheet->getStyle(sprintf('A6:%s7', $lastColumn))->getFont()->setBold(true);
                $event->sheet->getStyle(sprintf('A%d', $lastRow))->applyFromArray($styleTextRight);
            }
        ];
    }
}

pengajuan_detail.blade.php

<div class="table-responsive">
    <table id="detail-pengajuan-table" class="table table-striped w-100">
        <thead>
        <tr class="text-bold">
            <th rowspan="2">Nama Barang</th>
            <th rowspan="2">Gambar</th>
            <th rowspan="2">Jumlah Barang</th>
            <th rowspan="2">Harga Satuan</th>
            <th rowspan="2">Harga Total</th>
            <th rowspan="2">Keterangan</th>
        </tr>
        </thead>
        <tbody>
        @php $fmt = new NumberFormatter('id_ID', NumberFormatter::CURRENCY) @endphp
        @foreach($submissionDetail as $detail)
            <tr>
                <td>{{ $detail->nama_barang }}</td>
                <td><img src="{{ 'storage'. str_replace('public', '', $detail->image_path) }}" alt="Gambar Barang"
                         width="250"></td>
                <td>{{ $detail->jumlah }}</td>
                <td>{{ $fmt->format($detail->harga_satuan) }}</td>
                <td>{{ $fmt->format($detail->harga_total) }}</td>
                <td>{{ $detail->keterangan }}</td>
            </tr>
        @endforeach
        </tbody>
    </table>
</div>
<!-- /.datatable -->

When i opened the exported file, it became something like this: enter image description here

The second data after the first one goes to the right instead in the same column, how do i fix this

Advertisement

Answer

Fixed it by deleting the “rowspan”

<th>Nama Barang</th>
<th>Gambar</th>
<th>Jumlah Barang</th>
<th>Harga Satuan</th>
<th>Harga Total</th>
<th>Keterangan</th>
User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement