TableHeadExport.php 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. <?php
  2. namespace App\Exports;
  3. use Illuminate\Support\Collection;
  4. use Maatwebsite\Excel\Concerns\FromCollection;
  5. use Maatwebsite\Excel\Concerns\WithCustomValueBinder;
  6. use Maatwebsite\Excel\Concerns\WithHeadings;
  7. use Maatwebsite\Excel\Concerns\WithStrictNullComparison; // 导出 0 原样显示,不为 null
  8. use PhpOffice\PhpSpreadsheet\Cell\DefaultValueBinder;
  9. class TableHeadExport extends DefaultValueBinder implements WithCustomValueBinder , FromCollection, WithStrictNullComparison,withHeadings
  10. {
  11. /**
  12. * @return \Illuminate\Support\Collection
  13. */
  14. public function __construct($data,$headers)
  15. {
  16. $this->data = $data;
  17. $this->headers = $headers;
  18. }
  19. //数组转集合
  20. public function collection()
  21. {
  22. return new Collection($this->createData());
  23. }
  24. //业务代码
  25. public function createData()
  26. {
  27. $data = $this->export();
  28. return $data;
  29. }
  30. // 自定义表头,需实现withHeadings接口
  31. public function headings(): array
  32. {
  33. return $this->headers;
  34. }
  35. private function export(){
  36. $list = [];
  37. // dump($this->data);die;
  38. foreach ($this->data as $v){
  39. $list[] = $v;
  40. }
  41. return $list;
  42. }
  43. }