133 lines
3.5 KiB
PHP
133 lines
3.5 KiB
PHP
<?php
|
|
|
|
namespace App\Exports;
|
|
|
|
use App\Models\Brand;
|
|
use Maatwebsite\Excel\Concerns\FromCollection;
|
|
use Maatwebsite\Excel\Concerns\ShouldAutoSize;
|
|
use Maatwebsite\Excel\Concerns\WithColumnWidths;
|
|
use Maatwebsite\Excel\Concerns\WithEvents;
|
|
use Maatwebsite\Excel\Concerns\WithHeadings;
|
|
use Maatwebsite\Excel\Concerns\WithMapping;
|
|
use Maatwebsite\Excel\Concerns\WithStyles;
|
|
use Maatwebsite\Excel\Concerns\WithTitle;
|
|
use Maatwebsite\Excel\Events\AfterSheet;
|
|
use PhpOffice\PhpSpreadsheet\Style\Alignment;
|
|
use PhpOffice\PhpSpreadsheet\Style\Border;
|
|
use PhpOffice\PhpSpreadsheet\Style\Fill;
|
|
use PhpOffice\PhpSpreadsheet\Worksheet\Worksheet;
|
|
|
|
class BrandExport implements FromCollection, ShouldAutoSize, WithColumnWidths, WithEvents, WithHeadings, WithMapping, WithStyles, WithTitle
|
|
{
|
|
/**
|
|
* @return \Illuminate\Support\Collection
|
|
*/
|
|
public function collection()
|
|
{
|
|
return Brand::all();
|
|
}
|
|
|
|
public function title(): string
|
|
{
|
|
return 'Data Merek';
|
|
}
|
|
|
|
public function headings(): array
|
|
{
|
|
return [
|
|
'Nama',
|
|
'Tanggal Dibuat',
|
|
'Tanggal Diperbarui',
|
|
];
|
|
}
|
|
|
|
/**
|
|
* @param Brand $brand
|
|
*/
|
|
public function map($brand): array
|
|
{
|
|
return [
|
|
$brand->name,
|
|
formatDateLocalized($brand->created_at, 'd/m/Y H:i'),
|
|
formatDateLocalized($brand->updated_at, 'd/m/Y H:i'),
|
|
];
|
|
}
|
|
|
|
public function columnWidths(): array
|
|
{
|
|
return [
|
|
'A' => 20, // Brand Name
|
|
'B' => 18, // Created Date
|
|
'C' => 18, // Updated Date
|
|
];
|
|
}
|
|
|
|
/**
|
|
* @return array
|
|
*/
|
|
public function styles(Worksheet $sheet)
|
|
{
|
|
$lastRow = $sheet->getHighestRow();
|
|
$lastColumn = $sheet->getHighestColumn();
|
|
|
|
// Header style
|
|
$headerStyle = [
|
|
'font' => [
|
|
'bold' => true,
|
|
'size' => 12,
|
|
'color' => ['rgb' => 'FFFFFF'],
|
|
],
|
|
'fill' => [
|
|
'fillType' => Fill::FILL_SOLID,
|
|
'startColor' => ['rgb' => '71717A'], // Zinc - elegant
|
|
],
|
|
'alignment' => [
|
|
'horizontal' => Alignment::HORIZONTAL_CENTER,
|
|
'vertical' => Alignment::VERTICAL_CENTER,
|
|
],
|
|
'borders' => [
|
|
'allBorders' => [
|
|
'borderStyle' => Border::BORDER_THIN,
|
|
'color' => ['rgb' => '000000'],
|
|
],
|
|
],
|
|
];
|
|
|
|
// Data rows style
|
|
$dataStyle = [
|
|
'borders' => [
|
|
'allBorders' => [
|
|
'borderStyle' => Border::BORDER_THIN,
|
|
'color' => ['rgb' => '000000'],
|
|
],
|
|
],
|
|
'alignment' => [
|
|
'vertical' => Alignment::VERTICAL_TOP,
|
|
],
|
|
];
|
|
|
|
return [
|
|
// Header styling
|
|
1 => $headerStyle,
|
|
|
|
// Data rows styling
|
|
'A2:'.$lastColumn.$lastRow => $dataStyle,
|
|
];
|
|
}
|
|
|
|
public function registerEvents(): array
|
|
{
|
|
return [
|
|
AfterSheet::class => function (AfterSheet $event) {
|
|
// Auto-size columns with max width
|
|
foreach (range('A', 'C') as $column) {
|
|
$event->sheet->getColumnDimension($column)->setAutoSize(true);
|
|
}
|
|
|
|
// Set row height for header
|
|
$event->sheet->getRowDimension(1)->setRowHeight(25);
|
|
},
|
|
];
|
|
}
|
|
}
|