DefaultExport.php 908 B

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. <?php
  2. namespace App\Exports;
  3. use Maatwebsite\Excel\Concerns\FromCollection;
  4. class DefaultExport implements FromCollection
  5. {
  6. private $head;
  7. private $body;
  8. public function __construct($head, $body)
  9. {
  10. $this->head = $head;
  11. $this->body = $body;
  12. }
  13. /**
  14. * @return \Illuminate\Support\Collection
  15. */
  16. public function collection()
  17. {
  18. $head = $this->head;
  19. $body = $this->body;
  20. // 表头设置
  21. foreach ($head[0] as $key=>$value) {
  22. $head_arr[] = $key;
  23. }
  24. // 表数据
  25. foreach ($body as $key => &$value) {
  26. $js = [];
  27. for ($i = 0; $i < count($head_arr); $i++) {
  28. $js = array_merge($js,[ $head_arr[$i] => $value[ $head_arr[$i] ] ]);
  29. }
  30. array_push($head, $js);
  31. unset($val);
  32. }
  33. return collect($head);
  34. }
  35. }