SolvedLaravel Excel [QUESTION] XLS export not working
✔️Accepted Answer
@edanao, try cleanin the buffer before calling download function. There must be some space symbol in the code which triggers the error.
if (ob_get_level() > 0) { ob_end_clean(); }
Other Answers:
Hi, I've encountered the same bug.
I've read all the issues about removing the blank spaces before "<?php" and I removed all last empty lines.
That does not change the fact that xls/xlsx export doesnt not work as it should.
Here is my code:
routes.php
Route::get('{id_me}/export/{table}', 'FileController@exportCsv')->where('table', 'shops|societies|axes|programs|scenarios|users|surveys');
FileController.php
public function exportCsv(Request $request)
{
$table = $request->route('table');
$model = $this->loadModel($table);
$society_id = $request->route('id_me');
$rows = $model
->where('society_id', $society_id)
->get();
$rows = $rows->toArray();
Excel::create($table, function($excel) use ($rows, $table)
{
$excel->setTitle("Exportation des " . $table);
$excel->sheet('shops', function($sheet) use ($rows)
{
$sheet->fromArray($rows);
});
})->download('xls');
}
This code works like a charm with csv extension not xls or xlsx.
By not working i mean that it render unreadable data.
Here is my setup:
Laravel 5.1
"maatwebsite/excel": "~2.1.0"
I'm pretty sure i've removed all trailling spaces before <?php and at the end of files, I've checked for like 2 hours more or less even in files that is probably not parsed in this case.
No offense, but this should be highly considered as a bug to fixe.
Even if removing what's have to be removed to solve the problem is an "ok" solution for some, it is too much of a project maintainability issue for what can be a minor usage of this library (in my case).
Thanks for your work and attention, I really like to use Laravel-excel even if I'm still struggling to get some of the implemented feature to work as planned
@edanao, try cleanin the buffer before calling download function. There must be some space symbol in the code which triggers the error.
if (ob_get_level() > 0) { ob_end_clean(); }
Thanks it worked For Me
I've read through the older issues about the blank space prior to the <?php. But I've searched and could not find any in the controller, model, and route in relation to the export call.
I'm using the latest Laravel, & Laravel-Excel Package.
My Link code;
My export code;
When I try to open the file. It is corrupt. This same code works fine with CSV