Laravel 导出:excel 格式问题
Laravel Export : Problem with the excel formatting
导出操作的 excel 格式有问题,这是我的代码
SubmissionDetailExportExcel.php
<?php
namespace App\Exports;
use App\Http\Repository\PengajuanDetailRepository;
use App\Submission;
use Illuminate\Contracts\View\View;
use Maatwebsite\Excel\Concerns\FromView;
use Maatwebsite\Excel\Concerns\WithEvents;
use Maatwebsite\Excel\Events\AfterSheet;
use PhpOffice\PhpSpreadsheet\Cell\Coordinate;
use PhpOffice\PhpSpreadsheet\Style\Alignment;
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 -->
当我打开导出的文件时,它变成了这样:
第一个数据之后的第二个数据向右移动而不是在同一列中,我该如何解决这个问题
通过删除“rowspan”修复了它
<th>Nama Barang</th>
<th>Gambar</th>
<th>Jumlah Barang</th>
<th>Harga Satuan</th>
<th>Harga Total</th>
<th>Keterangan</th>
导出操作的 excel 格式有问题,这是我的代码
SubmissionDetailExportExcel.php
<?php
namespace App\Exports;
use App\Http\Repository\PengajuanDetailRepository;
use App\Submission;
use Illuminate\Contracts\View\View;
use Maatwebsite\Excel\Concerns\FromView;
use Maatwebsite\Excel\Concerns\WithEvents;
use Maatwebsite\Excel\Events\AfterSheet;
use PhpOffice\PhpSpreadsheet\Cell\Coordinate;
use PhpOffice\PhpSpreadsheet\Style\Alignment;
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 -->
当我打开导出的文件时,它变成了这样:
第一个数据之后的第二个数据向右移动而不是在同一列中,我该如何解决这个问题
通过删除“rowspan”修复了它
<th>Nama Barang</th>
<th>Gambar</th>
<th>Jumlah Barang</th>
<th>Harga Satuan</th>
<th>Harga Total</th>
<th>Keterangan</th>