120 lines
3.9 KiB
PHP
120 lines
3.9 KiB
PHP
<?php
|
|
|
|
namespace App\Exports;
|
|
|
|
use Maatwebsite\Excel\Concerns\FromView;
|
|
use Maatwebsite\Excel\Concerns\WithStyles;
|
|
use PhpOffice\PhpSpreadsheet\Worksheet\Worksheet;
|
|
use Illuminate\Contracts\View\View;
|
|
use PhpOffice\PhpSpreadsheet\Style\NumberFormat;
|
|
use PhpOffice\PhpSpreadsheet\Style\Alignment;
|
|
|
|
class GpcOutputExport implements FromView, WithStyles
|
|
{
|
|
protected $inventoryYear, $gpcOutputs, $gpcOutputRList;
|
|
|
|
public function __construct($inventoryYear, $gpcOutputs, $gpcOutputRList)
|
|
{
|
|
$this->inventoryYear = $inventoryYear;
|
|
$this->gpcOutputs = $gpcOutputs;
|
|
$this->gpcOutputRList = $gpcOutputRList;
|
|
}
|
|
|
|
public function view(): View
|
|
{
|
|
return view('reports.gpc-output.report', [
|
|
'inventoryYear' => $this->inventoryYear,
|
|
'gpcOutputs' => $this->gpcOutputs,
|
|
'gpcOutputRList' => $this->gpcOutputRList,
|
|
'isExport' => true
|
|
]);
|
|
}
|
|
|
|
public function styles(Worksheet $sheet)
|
|
{
|
|
$tableStartRow = 6;
|
|
$tableHeaderRow = $tableStartRow;
|
|
$tableEndRow = $sheet->getHighestRow();
|
|
$columnStart = 'A';
|
|
$columnEnd = $sheet->getHighestColumn();
|
|
|
|
$sheet->getStyle('A1:A3')->applyFromArray([
|
|
'font' => ['bold' => true],
|
|
'alignment' => ['horizontal' => Alignment::HORIZONTAL_LEFT],
|
|
]);
|
|
$sheet->mergeCells('A1:' . $columnEnd . '1');
|
|
$sheet->mergeCells('A2:' . $columnEnd . '2');
|
|
$sheet->mergeCells('A3:' . $columnEnd . '3');
|
|
|
|
// Apply table header styles
|
|
$sheet->getStyle($columnStart . $tableStartRow . ':' . $columnEnd . $tableHeaderRow)->applyFromArray([
|
|
'font' => ['bold' => true],
|
|
'alignment' => ['horizontal' => Alignment::HORIZONTAL_CENTER],
|
|
]);
|
|
|
|
// Apply borders to the entire table
|
|
$sheet->getStyle($columnStart . $tableStartRow . ':' . $columnEnd . $tableEndRow)->applyFromArray([
|
|
'borders' => [
|
|
'allBorders' => [
|
|
'borderStyle' => \PhpOffice\PhpSpreadsheet\Style\Border::BORDER_THIN,
|
|
'color' => ['argb' => 'FF000000'],
|
|
],
|
|
],
|
|
]);
|
|
|
|
// Set dynamic column styles based on data
|
|
foreach ($sheet->getRowIterator() as $row) {
|
|
$rowIndex = $row->getRowIndex();
|
|
|
|
// Skip the header rows
|
|
if ($rowIndex >= $tableStartRow && $rowIndex <= $tableHeaderRow) {
|
|
continue;
|
|
}
|
|
|
|
foreach ($row->getCellIterator() as $cell) {
|
|
$value = $cell->getValue();
|
|
$formatCode = NumberFormat::FORMAT_GENERAL;
|
|
|
|
if (is_numeric($value)) {
|
|
$cell->getStyle()->getAlignment()->setHorizontal(Alignment::HORIZONTAL_RIGHT);
|
|
|
|
$formatCode = floor($value) != $value ? '#,##0.0#' : '#,##0';
|
|
$cell->getStyle()->getNumberFormat()->setFormatCode($formatCode);
|
|
} else {
|
|
$cell->getStyle()->getAlignment()->setHorizontal(Alignment::HORIZONTAL_LEFT);
|
|
}
|
|
}
|
|
}
|
|
|
|
// Auto-fit columns
|
|
$highestColumn = $sheet->getHighestColumn();
|
|
$columns = $this->generateColumnsRange('A', $highestColumn);
|
|
foreach ($columns as $column) {
|
|
$sheet->getColumnDimension($column)->setAutoSize(true);
|
|
}
|
|
|
|
|
|
// // Style footer row
|
|
// $sheet->getStyle($columnStart . $tableEndRow . ':' . $columnEnd . $tableEndRow)->applyFromArray([
|
|
// 'font' => ['bold' => true],
|
|
// 'alignment' => ['horizontal' => \PhpOffice\PhpSpreadsheet\Style\Alignment::HORIZONTAL_RIGHT],
|
|
// ]);
|
|
|
|
return [];
|
|
}
|
|
|
|
function generateColumnsRange($start, $end)
|
|
{
|
|
$columns = [];
|
|
$current = $start;
|
|
|
|
while ($current !== $end) {
|
|
$columns[] = $current;
|
|
$current++;
|
|
}
|
|
$columns[] = $end;
|
|
|
|
return $columns;
|
|
}
|
|
}
|