82 lines
2.7 KiB
PHP
82 lines
2.7 KiB
PHP
<?php
|
|
|
|
namespace App\Exports;
|
|
|
|
use App\Models\Warga;
|
|
use Maatwebsite\Excel\Concerns\FromCollection;
|
|
use Maatwebsite\Excel\Concerns\WithHeadings;
|
|
use Maatwebsite\Excel\Concerns\WithCustomStartCell;
|
|
use Maatwebsite\Excel\Concerns\WithEvents;
|
|
use Maatwebsite\Excel\Concerns\WithMapping;
|
|
use Maatwebsite\Excel\Concerns\WithCustomValueBinder;
|
|
use PhpOffice\PhpSpreadsheet\Cell\StringValueBinder;
|
|
use Maatwebsite\Excel\Events\AfterSheet;
|
|
use PhpOffice\PhpSpreadsheet\Style\Alignment;
|
|
|
|
class WargaExport extends StringValueBinder implements FromCollection, WithHeadings, WithCustomStartCell, WithEvents, WithMapping, WithCustomValueBinder
|
|
{
|
|
public function collection()
|
|
{
|
|
return Warga::with('wilayah')->orderBy('nama')->get();
|
|
}
|
|
|
|
public function map($warga): array
|
|
{
|
|
return [
|
|
$warga->nik,
|
|
$warga->nama,
|
|
$warga->wilayah ? $warga->wilayah->dusun : $warga->dusun,
|
|
$warga->wilayah ? $warga->wilayah->rw : $warga->rw,
|
|
];
|
|
}
|
|
|
|
public function headings(): array
|
|
{
|
|
return [
|
|
'NIK',
|
|
'Nama Lengkap',
|
|
'Dusun',
|
|
'RW',
|
|
];
|
|
}
|
|
|
|
public function startCell(): string
|
|
{
|
|
return 'A9';
|
|
}
|
|
|
|
public function registerEvents(): array
|
|
{
|
|
return [
|
|
AfterSheet::class => function(AfterSheet $event) {
|
|
$sheet = $event->sheet->getDelegate();
|
|
|
|
// Set Kop Data
|
|
$sheet->setCellValue('A1', 'PEMERINTAH KABUPATEN MOJOKERTO');
|
|
$sheet->setCellValue('A2', 'KECAMATAN PUNGGING');
|
|
$sheet->setCellValue('A3', 'DESA PUNGGING');
|
|
$sheet->setCellValue('A4', 'Jalan Raya Pungging No. 27 Pungging, Mojokerto, Jawa Timur');
|
|
$sheet->setCellValue('A5', 'Kode Pos 61384 Telp. (0321) 532733');
|
|
$sheet->setCellValue('A7', 'DATA WARGA BLT-DD');
|
|
|
|
// Merge Cells
|
|
for ($i = 1; $i <= 7; $i++) {
|
|
$sheet->mergeCells("A{$i}:D{$i}");
|
|
}
|
|
|
|
// Styling
|
|
$sheet->getStyle('A1:A7')->getAlignment()->setHorizontal(Alignment::HORIZONTAL_CENTER);
|
|
$sheet->getStyle('A1:A3')->getFont()->setBold(true)->setSize(14);
|
|
$sheet->getStyle('A7')->getFont()->setBold(true)->setSize(12);
|
|
$sheet->getStyle('A9:D9')->getFont()->setBold(true);
|
|
$sheet->getStyle('A9:D9')->getAlignment()->setHorizontal(Alignment::HORIZONTAL_CENTER);
|
|
|
|
// Auto-size columns
|
|
foreach (range('A', 'D') as $col) {
|
|
$sheet->getColumnDimension($col)->setAutoSize(true);
|
|
}
|
|
},
|
|
];
|
|
}
|
|
}
|