You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
147 lines
5.9 KiB
147 lines
5.9 KiB
<?php |
|
|
|
namespace App\Exports; |
|
|
|
use Maatwebsite\Excel\Concerns\FromArray; |
|
use Maatwebsite\Excel\Concerns\WithHeadings; |
|
use Maatwebsite\Excel\Concerns\WithCustomStartCell; |
|
use Maatwebsite\Excel\Events\AfterSheet; |
|
use PhpOffice\PhpSpreadsheet\Worksheet\Drawing; |
|
use PhpOffice\PhpSpreadsheet\Worksheet\Worksheet; |
|
use Maatwebsite\Excel\Concerns\WithEvents; |
|
use Maatwebsite\Excel\Concerns\FromCollection; |
|
use Illuminate\Support\Collection; |
|
use PhpOffice\PhpSpreadsheet\Style\Alignment; |
|
use PhpOffice\PhpSpreadsheet\Style\Border; |
|
use PhpOffice\PhpSpreadsheet\Worksheet\PageSetup; |
|
|
|
class ArrayExportH implements FromArray, WithHeadings, WithCustomStartCell, WithEvents |
|
{ |
|
protected $data = []; |
|
protected $headings = null; |
|
protected $startCell = 'A1'; |
|
protected $maxWidthThreshold = 50; // 設定相對的最大值 |
|
protected $printOrientation = 1; // 預設橫式列印 1 = 橫式列印, 0 = 直式列印 |
|
protected $showPageNumber = 0; // 是否顯示頁碼 1 = 顯示, 0 = 不顯示 |
|
protected $showBorder = 0; // 顯示框線 |
|
|
|
|
|
public function __construct(array $data, array $headings = null, array $option = []) |
|
{ |
|
$this->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; |
|
// } |
|
|
|
}
|
|
|