123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155 |
- <?php
- namespace App\Exports;
- use Illuminate\Support\Collection;
- use Illuminate\Support\Facades\Redis;
- use Maatwebsite\Excel\Concerns\FromCollection;
- use Maatwebsite\Excel\Concerns\WithCustomValueBinder;
- use Maatwebsite\Excel\Concerns\WithEvents; // 自动注册事件监听器
- use Maatwebsite\Excel\Concerns\WithHeadings;
- use Maatwebsite\Excel\Concerns\WithStrictNullComparison; // 导出 0 原样显示,不为 null
- use Maatwebsite\Excel\Events\AfterSheet;
- use PhpOffice\PhpSpreadsheet\Cell\Cell;
- use PhpOffice\PhpSpreadsheet\Cell\DataType;
- use PhpOffice\PhpSpreadsheet\Cell\DefaultValueBinder;
- class ExportOrder extends DefaultValueBinder implements WithCustomValueBinder , FromCollection, WithEvents, WithStrictNullComparison,withHeadings
- {
- /**
- * @return \Illuminate\Support\Collection
- */
- public function __construct($data,$type=1,$headers)
- {
- $this->data = $data;
- $this->type = $type;
- $this->headers = $headers;
- }
- public function registerEvents(): array
- {
- //区分不通状态的合同导出,格式不同
- $type = $this->type.'_set';
- return $this->$type();
- }
- //数组转集合
- public function collection()
- {
- return new Collection($this->createData());
- }
- //业务代码
- public function createData()
- {
- $name = $this->type;
- $data = $this->export();
- return $data;
- }
- public function bindValue(Cell $cell, $value)
- {
- if (is_numeric($value)) {
- $cell->setValueExplicit($value, DataType::TYPE_STRING2);
- return true;
- }
- // else return default behavior
- return parent::bindValue($cell, $value);
- }
- // 自定义表头,需实现withHeadings接口
- public function headings(): array
- {
- return $this->headers;
- }
- private function export(){
- $list = [];
- // dump($this->data);die;
- foreach ($this->data as $v){
- $list[] = $v;
- }
- return $list;
- }
- private function default_set(){
- return [
- AfterSheet::class => function (AfterSheet $event) {
- $count = count($this->data);
- //设置区域单元格水平居中
- $event->sheet->getDelegate()->getStyle('A1:'.'M'.($count+1))->getAlignment()->setHorizontal(\PhpOffice\PhpSpreadsheet\Style\Alignment::HORIZONTAL_CENTER);
- // 定义列宽度
- $widths = ['A' => 20, 'B' => 20, 'C' => 20, 'D' => 20, 'E' => 20, 'F' => 20, 'G' => 20, 'H' => 20, 'I' => 20, 'J' => 20, 'K' => 20, 'L' => 20, 'M' => 20, 'O' => 20, 'P' => 20, 'Q' => 20, 'R' => 20, 'S' => 20, 'T' => 20, 'U' => 20, 'V' => 20, 'W' => 20, 'X' => 20, 'Y' => 20, 'Z' => 20];
- foreach ($widths as $k => $v) {
- // 设置列宽度
- $event->sheet->getDelegate()->getColumnDimension($k)->setWidth($v);
- }
- },
- ];
- }
- private function kq_set(){
- return [
- AfterSheet::class => function(AfterSheet $event) {
- $event->sheet->getStyle('A1:D1')->applyFromArray([
- 'font' => ['bold' => true],
- 'fill' => ['fillType' => 'solid', 'color' => ['rgb' => 'FFCC00']],
- 'borders' => ['allBorders' => ['borderStyle' => 'thin']]
- ]);
- // 自动调整列宽
- for ($i = 'A'; $i !== 'E'; $i++) {
- $event->sheet->getColumnDimension($i)->setAutoSize(true);
- }
- },
- ];
- }
- private function kq2_set(){
- return [
- AfterSheet::class => function(AfterSheet $event) {
- // 自动调整列宽
- for ($i = 'A'; $i !== 'E'; $i++) {
- $event->sheet->getColumnDimension($i)->setAutoSize(true);
- }
- // 获取工作表实例
- $sheet = $event->sheet->getDelegate();
- // 合并第一行单元格(A1到E1)
- $sheet->mergeCells('A1:AD1');
- $sheet->mergeCells('A2:B2');
- $sheet->mergeCells('A3:B3');
- // 设置第一行样式
- $styleArray = [
- 'font' => [
- 'bold' => true, // 加粗
- ],
- 'alignment' => [
- 'horizontal' => \PhpOffice\PhpSpreadsheet\Style\Alignment::HORIZONTAL_CENTER, // 水平居中
- 'vertical' => \PhpOffice\PhpSpreadsheet\Style\Alignment::VERTICAL_CENTER, // 垂直居中
- ],
- ];
- // 应用样式到第一行
- $sheet->getStyle('A1:E1')->applyFromArray($styleArray);
- $sheet->getStyle('A2:B2')->applyFromArray($styleArray);
- $sheet->getStyle('A3:B3')->applyFromArray($styleArray);
- $lastRow = $sheet->getHighestRow();
- $range = 'A' . $lastRow . ':B' . $lastRow;
- $sheet->mergeCells($range);
- $sheet->getStyle($range)->applyFromArray($styleArray);
- },
- ];
- }
- }
|