Laravel 8 使用 Maatwebsite\Excel 导出数组到Excel下载文件为空如何解决?
问题修复方案
核心错误点排查
- 首先删除代码中的调试终止语句:
$sheets = collect($sheets); dd($sheets);这里的dd()会直接中断程序执行,后续导出逻辑完全没有运行,是导出为空的最直接原因。 - Maatwebsite Excel 3.x 版本不支持直接将集合传入
Excel::download()方法,必须创建对应导出类实现官方要求的接口,你开头引入的FromCollection、Exportable没有实际使用。 - 数组键名存在语法错误:
granted_comments"、inspections"、oze_installations"三个键名多了多余的双引号,会导致数据映射异常。 - 文件名包含特殊字符:
now()默认输出的时间带有冒号,在Windows等系统中属于非法文件名字符,会导致文件生成失败。 - 字符串拼接优先级错误:street、ovens字段的拼接逻辑中,
??运算符优先级低于.,没有加括号会导致取值逻辑错误,比如你调试输出中street字段只有ul.就是该问题导致的。
具体修复步骤
1. 创建导出类
在app/Exports目录下创建ImmovablesDataExport.php文件,代码如下:
<?php namespace App\Exports; use Maatwebsite\Excel\Concerns\FromCollection; use Maatwebsite\Excel\Concerns\WithHeadings; // 可选,用于生成表头 use Maatwebsite\Excel\Concerns\Exportable; class ImmovablesDataExport implements FromCollection, WithHeadings { use Exportable; protected $data; // 构造函数传入处理好的数据 public function __construct($data) { $this->data = $data; } // 返回要导出的集合 public function collection() { return $this->data; } // 可选,设置Excel表头,和你数据的键名对应 public function headings(): array { return [ 'pesel', 'name', 'surname', 'email1', 'email2', 'email3', 'phone1', 'phone2', 'phone3', 'community', 'city', 'street', 'building_number', 'apartment_number', 'county', 'district', 'province', 'building_type', 'ovens', 'granted_comments', 'inspections', 'oze_installations', ]; } }
2. 修正控制器方法
修改你的downloadData1方法,修复所有错误点:
use App\Exports\ImmovablesDataExport; public function downloadData1(ReportRequest $request) { $data = ImmovablesExport::select('*')->orderBy('id', 'DESC')->take(10)->get()->toArray(); // 格式化文件名,避免特殊字符 $fileName = 'Raport z ' . now()->format('Y-m-d_H-i-s') . '.xlsx'; $sheets = []; foreach ($data as $i => $item) { $sheets[$i]['pesel'] = $this->prepareStringToExport($item['pesel'] ?? ''); $sheets[$i]['name'] = $this->prepareStringToExport($item['name'] ?? ''); $sheets[$i]['surname'] = $this->prepareStringToExport($item['surname'] ?? ''); $sheets[$i]['email1'] = $this->prepareStringToExport($item['email1'] ?? ''); $sheets[$i]['email2'] = $this->prepareStringToExport($item['email2'] ?? ''); $sheets[$i]['email3'] = $this->prepareStringToExport($item['email3'] ?? ''); $sheets[$i]['phone1'] = $this->prepareStringToExport($item['phone1'] ?? ''); $sheets[$i]['phone2'] = $this->prepareStringToExport($item['phone2'] ?? ''); $sheets[$i]['phone3'] = $this->prepareStringToExport($item['phone3'] ?? ''); $sheets[$i]['community'] = $this->prepareStringToExport($item['community'] ?? ''); $sheets[$i]['city'] = $this->prepareStringToExport($item['city'] ?? ''); // 修正拼接优先级,给??部分加括号 $sheets[$i]['street'] = $this->prepareStringToExport(($item['address']['prefix'] ?? '') . ' ' . ($item['address']['name'] ?? '')); $sheets[$i]['building_number'] = $this->prepareStringToExport($item['building_number'] ?? ''); $sheets[$i]['apartment_number'] = $this->prepareStringToExport($item['apartment_number'] ?? ''); $sheets[$i]['county'] = $this->prepareStringToExport($item['county'] ?? ''); $sheets[$i]['district'] = $this->prepareStringToExport($item['district'] ?? ''); $sheets[$i]['province'] = $this->prepareStringToExport($item['province']['name'] ?? ''); $sheets[$i]['building_type'] = $this->prepareStringToExport($item['building_type']['name'] ?? ''); $ovens = ''; if (count($item['ovens']) > 0) { foreach ($item['ovens'] as $ii => $oven) { // 修正拼接优先级,每个??部分加括号 $ovens .= $oven['name'] . "(moc: " . $oven['power'] . "), źródło: " . ($oven['heat_source']['name'] ?? '') . ', funkcje: ' . ($oven['oven_function']['name'] ?? '') . ', klasa: ' . ($oven['oven_class_number']['name'] ?? '') . ', paliwo: ' . ($oven['fuel_type']['name'] ?? '') . '. '; } } $sheets[$i]['ovens'] = $this->prepareStringToExport($ovens); // 修正键名多余的双引号 $sheets[$i]['granted_comments'] = $this->prepareStringToExport($item['granted_comments'] ?? ''); $sheets[$i]['inspections'] = $this->prepareStringToExport($item['inspections'] ?? ''); $sheets[$i]['oze_installations'] = $this->prepareStringToExport($item['oze_installations'] ?? ''); }; $sheets = collect($sheets); // 用导出类实例调用download方法 return (new ImmovablesDataExport($sheets))->download($fileName); }
修改后即可正常导出包含数据的Excel文件。
内容的提问来源于stack exchange,提问作者tarafikttes
相关产品推荐
相关产品推荐

