| 123456789101112131415161718192021222324252627282930313233343536373839404142 |
- <?php
- namespace App\Exports;
- use Maatwebsite\Excel\Concerns\FromCollection;
- class DefaultExport implements FromCollection
- {
- private $head;
- private $body;
- public function __construct($head, $body)
- {
- $this->head = $head;
- $this->body = $body;
- }
- /**
- * @return \Illuminate\Support\Collection
- */
- public function collection()
- {
- $head = $this->head;
- $body = $this->body;
- // 表头设置
- foreach ($head[0] as $key=>$value) {
- $head_arr[] = $key;
- }
- // 表数据
- foreach ($body as $key => &$value) {
- $js = [];
- for ($i = 0; $i < count($head_arr); $i++) {
- $js = array_merge($js,[ $head_arr[$i] => $value[ $head_arr[$i] ] ]);
- }
- array_push($head, $js);
- unset($val);
- }
- return collect($head);
- }
- }
|