data = $data; $this->headings = $headings; $this->startCell = $option['startCell'] ?? 'A1'; $this->maxWidthThreshold = $option['maxWidth'] ?? 50; $this->printOrientation = $option['printDef'] ?? 1; $this->showPageNumber = $option['showPN'] ?? 1; $this->showBorder = $option['showBorder'] ?? 1; } public function array(): array { return $this->data; } public function headings(): array { // 定義標題列 return $this->headings ?? array_keys($this->data[0]); } public function startCell(): string { return $this->startCell ?? 'A1'; } public function registerEvents(): array { return [ AfterSheet::class => function (AfterSheet $event) { // 在這裡設定欄位寬度 $sheet = $event->sheet; // 設定頁尾顯示 if ($this->showPageNumber) { // $sheet->getDelegate()->getHeaderFooter()->setOddHeader('&L &D &T &R 第 &P 頁,共 &N 頁'); $sheet->getDelegate()->getHeaderFooter()->setOddFooter('&R 第 &P 頁,共 &N 頁'); } // 設定列印邊界為窄邊界 $sheet->getDelegate()->getPageMargins()->setTop(0.75); $sheet->getDelegate()->getPageMargins()->setBottom(0.75); $sheet->getDelegate()->getPageMargins()->setLeft(0.25); $sheet->getDelegate()->getPageMargins()->setRight(0.25); // 設定橫式列印 if ($this->printOrientation) $sheet->getDelegate()->getPageSetup()->setOrientation(PageSetup::ORIENTATION_LANDSCAPE); // 最高的行和列 $highestRow = $sheet->getDelegate()->getHighestRow(); $highestColumn = $sheet->getDelegate()->getHighestColumn(); $sheet->getDelegate()->mergeCells('A1:' . $highestColumn . '1'); // 合併 A1:C1 儲存格 $sheet->getDelegate()->mergeCells('A2:' . $highestColumn . '2'); // 合併 A1:C1 儲存格 // $sheet->getDelegate()->mergeCells('A3:C3'); // 合併 A1:C1 儲存格 // $sheet->getDelegate()->mergeCells('D1:G2'); // 合併 A1:C1 儲存格 // 自動設定框線 if ($this->showBorder) { $range = 'A1:' . $highestColumn . $highestRow; $sheet->getDelegate()->getStyle($range)->getBorders()->getAllBorders()->setBorderStyle('thin'); } // 設定資料內容置中 $range = 'A3:' . $highestColumn . $highestRow; $sheet->getDelegate()->getStyle($range)->getAlignment()->setHorizontal(Alignment::HORIZONTAL_CENTER); $sheet->getDelegate()->getStyle($range)->getAlignment()->setVertical(Alignment::VERTICAL_CENTER); // 測量欄位內容寬度並設定寬度 $maxWidths = []; foreach ($sheet->getColumnIterator() as $column) { $columnLetter = $column->getColumnIndex(); $maxWidth = 0; foreach ($column->getCellIterator() as $cell) { $width = strlen((string)$cell->getValue()) * 1.2; // 設定寬度倍率 if ($width > $maxWidth) { $maxWidth = $width; } } $maxWidths[$columnLetter] = $maxWidth; } foreach ($maxWidths as $columnLetter => $width) { $sheet->getColumnDimension($columnLetter)->setWidth($width > $this->maxWidthThreshold ? $this->maxWidthThreshold : $width); } }, ]; } protected function wrapTextIfNeeded($value) { // 檢查是否需要換行顯示 return strlen($value) > $this->maxWidthThreshold ? wordwrap($value, $this->maxWidthThreshold, "\n", true) : $value; } // public function drawings() // { // $drawings = []; // foreach ($this->data as $row) { // if (isset($row[1]) && !empty($row[1])) { // $imagePath = public_path($row[1]); // 圖片路徑 // if (file_exists($imagePath)) { // $drawing = new Drawing(); // $drawing->setName('Image'); // $drawing->setDescription('Image'); // $drawing->setPath($imagePath); // $drawing->setHeight(100); // $drawing->setCoordinates('A' . ($this->key() + 2)); // 從第二行開始 // $drawings[] = $drawing; // } // } // } // return $drawings; // } }