Laravel Excel 正在运行,但无法打开文件

Laravel Excel is working but the file cannot be opened

我正在使用 Laravel Excel 创建包含许多 sheet 的 excel 文档。我一直在关注他们如何做的例子,但是当我去下载文件时:

Excel cannot open the file 'kingdoms (1).xlsx' because the file format or file extension is not valid. Verify that the file has not been corrupted and that the file extension matches the format of the file.

我不确定为什么。

我是这样做的:

控制器方法:

public function export() {
    return Excel::download(new KingdomsExport, 'kingdoms.xlsx', \Maatwebsite\Excel\Excel::XLSX);
}

导出Class

class KingdomsExport implements WithMultipleSheets {

    use Exportable;

    /**
     * @return array
     */
    public function sheets(): array {
        $sheets   = [];

        $sheets[] = new BuildingsSheet;

        // .. other commented out sheets.

        return $sheets;
    }
}

建筑物Sheet

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,它可以正常打开。但由于我计划有多个 sheet,它似乎没有将它们包含在 csv 文件中。

我还有其他 sheet,但为简单起见,我将它们注释掉以查看其中是否有问题。唉,即使只有一个 sheet,它仍然打不开。 我下载excel文件的方式有问题吗?

所以对于遇到此问题的人来说,这可能并不明显,但是:

    $response = Excel::download(new KingdomsExport, 'kingdoms.xlsx', \Maatwebsite\Excel\Excel::XLSX);
    ob_end_clean();

    return $response;

诀窍:ob_end_clean。似乎缓冲区没有得到正确清理,因此添加了一个额外的 space 。但是这样做,文件下载并能够打开。