excel - Laravel Excel 工作正常,但文件无法打开
问题描述
我正在使用Laravel Excel创建包含许多工作表的 excel 文档。我一直在按照他们的例子说明他们是如何做到的,但是当我去下载文件时:
Excel 无法打开文件“王国 (1).xlsx”,因为文件格式或文件扩展名无效。验证文件没有损坏并且文件扩展名与文件格式匹配。
我不确定为什么。
所以这是我的做法:
控制器方法:
public function export() {
return Excel::download(new KingdomsExport, 'kingdoms.xlsx', \Maatwebsite\Excel\Excel::XLSX);
}
出口类
class KingdomsExport implements WithMultipleSheets {
use Exportable;
/**
* @return array
*/
public function sheets(): array {
$sheets = [];
$sheets[] = new BuildingsSheet;
// .. other commented out sheets.
return $sheets;
}
}
建筑物表
class BuildingsSheet implements FromView, WithTitle, ShouldAutoSize {
/**
* @return View
*/
public function view(): View {
return view('admin.exports.kingdoms.sheets.buildings', [
'buildings' => GameBuilding::all(),
]);
}
/**
* @return string
*/
public function title(): string {
return 'Buildings';
}
}
建筑物视图
<table>
<thead>
<tr>
<th>name</th>
<th>description</th>
<th>max_level</th>
<th>base_durability</th>
<th>base_defence</th>
<th>required_population</th>
<th>units_per_level</th>
<th>only_at_level</th>
<th>is_resource_building</th>
<th>trains_units</th>
<th>is_walls</th>
<th>is_church</th>
<th>is_farm</th>
<th>wood_cost</th>
<th>clay_cost</th>
<th>stone_cost</th>
<th>iron_cost</th>
<th>time_to_build</th>
<th>time_increase_amount</th>
<th>decrease_morale_amount</th>
<th>increase_population_amount</th>
<th>increase_morale_amount</th>
<th>increase_wood_amount</th>
<th>increase_clay_amount</th>
<th>increase_stone_amount</th>
<th>increase_iron_amount</th>
<th>increase_durability_amount</th>
<th>increase_defence_amount</th>
</tr>
</thead>
<tbody>
@foreach($buildings as $building)
<tr>
<td>{{$building->name}}</td>
<td>{{$building->description}}</td>
<td>{{$building->max_level}}</td>
<td>{{$building->base_durability}}</td>
<td>{{$building->base_defence}}</td>
<td>{{$building->required_population}}</td>
<td>{{$building->units_per_level}}</td>
<td>{{$building->only_at_level}}</td>
<td>{{$building->is_resource_building}}</td>
<td>{{$building->trains_units}}</td>
<td>{{$building->is_walls}}</td>
<td>{{$building->is_church}}</td>
<td>{{$building->is_farm}}</td>
<td>{{$building->wood_cost}}</td>
<td>{{$building->clay_cost}}</td>
<td>{{$building->stone_cost}}</td>
<td>{{$building->iron_cost}}</td>
<td>{{$building->time_to_build}}</td>
<td>{{$building->time_increase_amount}}</td>
<td>{{$building->decrease_morale_amount}}</td>
<td>{{$building->increase_population_amount}}</td>
<td>{{$building->increase_morale_amount}}</td>
<td>{{$building->increase_wood_amount}}</td>
<td>{{$building->increase_clay_amount}}</td>
<td>{{$building->increase_stone_amount}}</td>
<td>{{$building->increase_iron_amount}}</td>
<td>{{$building->increase_durability_amount}}</td>
<td>{{$building->increase_defence_amount}}</td>
</tr>
@endforeach
</tbody>
</table>
我在这里看不到任何问题,但无法打开文件。我试图用它打开它,vi
但它都是胡言乱语。如果我尝试以 csv 格式下载,它可以正常打开。但由于我计划有多张工作表,它似乎没有将它们包含在 csv 文件中。
我确实有其他工作表,但为简单起见,我将它们注释掉,看看其中一张是否有问题。唉,即使只有一张纸,它仍然不会打开。我如何下载excel文件有问题吗?
解决方案
因此,对于有此问题的人来说,这可能并不明显,但是:
$response = Excel::download(new KingdomsExport, 'kingdoms.xlsx', \Maatwebsite\Excel\Excel::XLSX);
ob_end_clean();
return $response;
诀窍:ob_end_clean
。似乎缓冲区没有得到适当的清理,所以增加了一个额外的空间。但是这样做,文件下载并能够打开。
推荐阅读
- datetime - How to get ordinal indicators (1st, 2nd, 3rd, etc) in Go
- swift - 在 NavigationBar 中将自定义视图添加到 PreferredLargeTitles
- c# - 如何解决 DbUpdateConcurrency 异常
- graph-theory - 图算法将图循环划分为具有 0 个输入边的块
- python - 在单个命令中获取视频裁剪部分的场景更改
- vb.net - 在 VB.Net 中存储数据表
- c# - 无效的线程操作,Invoke 方法不起作用
- angular - 在 AoT 编译的 Angular 应用程序中访问窗口属性
- python - 在python中的赋值错误之前引用的变量
- android - 在回收站视图中运行时遇到一些问题