| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081 |
- <?php
- namespace App\Exports;
- use Maatwebsite\Excel\Concerns\FromCollection;
- use Maatwebsite\Excel\Concerns\ShouldAutoSize;
- use Maatwebsite\Excel\Concerns\WithColumnFormatting;
- use Maatwebsite\Excel\Concerns\WithCustomValueBinder;
- use Maatwebsite\Excel\Concerns\WithEvents;
- use Maatwebsite\Excel\Concerns\WithStrictNullComparison;
- use Maatwebsite\Excel\Events\AfterSheet;
- use PhpOffice\PhpSpreadsheet\Cell\StringValueBinder;
- use PhpOffice\PhpSpreadsheet\Style\NumberFormat;
- class Export extends StringValueBinder implements FromCollection,
- ShouldAutoSize,WithColumnFormatting,WithCustomValueBinder,WithStrictNullComparison,WithEvents
- {
- private $row;
- private $data;
- private $mergeCell;
- private $columnName;
- private $formatNumber;
- /*
- * $mergeCell $columnName :合并单元格所需参数;
- * $mergeCell 需要合并的位置数组以MAP形式存储 [开始行=>结束行]
- * $columnName 需要合并列 与合并行数结合使用ARRAY存储 ['A','B']
- */
- public function __construct($row,$data,$mergeCell=null,$columnName=null,$formatNumber=['D','H','I','L','O','Q','S','T','U','V'])
- {
- $this->row = $row;
- $this->data = $data;
- $this->mergeCell = $mergeCell;
- $this->columnName = $columnName;
- $this->formatNumber = $formatNumber;
- }
- public function collection()
- {
- $row = $this->row;
- $data = $this->data;
- //设置表头
- foreach ($row[0] as $key => $value) {
- $key_arr[] = $key;
- }
- //输入数据
- foreach ($data as $key => &$value) {
- $js = [];
- for ($i=0; $i < count($key_arr); $i++) {
- $js = array_merge($js,[ $key_arr[$i] => $value[ $key_arr[$i] ] ]);
- }
- array_push($row, $js);
- unset($val);
- }
- return collect($row);
- }
- public function registerEvents(): array
- {
- if ($this->mergeCell && $this->columnName){
- return [
- AfterSheet::class => function(AfterSheet $event){
- foreach ($this->columnName as $column){
- foreach ($this->mergeCell as $key=>$value){
- $event->sheet->getDelegate()->mergeCells($column.$key.':'.$column.$value);
- }
- }
- }
- ];
- }
- return [];
- }
- public function columnFormats(): array{
- $formatNumber = [];
- foreach ($this->formatNumber as $column){
- $formatNumber[$column] = NumberFormat::FORMAT_TEXT;
- }
- return $formatNumber;
- }
- }
|