I am using the Laravel Excel to create excel documents with many sheets. I have been following their example of how they do it, but when I go to download the file its:
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.
I am unsure why.
So heres how I do it:
Controller Method:
public function export() {
return Excel::download(new KingdomsExport, 'kingdoms.xlsx', \Maatwebsite\Excel\Excel::XLSX);
}
Export Class
class KingdomsExport implements WithMultipleSheets {
use Exportable;
/**
* @return array
*/
public function sheets(): array {
$sheets = [];
$sheets[] = new BuildingsSheet;
// .. other commented out sheets.
return $sheets;
}
}
Buildings 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';
}
}
Buildings view
<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>
I can't see anything wrong here with any of this, yet the file cannot be opened. I have tried to open it with vi
but it's all gibberish. If I try and download as csv, it opens fine. but as I plan to have multiple sheets, it doesn't seem to include them with csv files.
I do have other sheets, but for simplicity I commented them out to see if one of them was having an issue. Alas even with just one sheet, it still won't open. Is it something wrong with how I download the excel file?