Laravel-Excel: [QUESTION] XLS export not working
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;
$.post(export_url, export_data, function(data){
var uri = 'data:application/vnd.ms-excel;charset=UTF-8,' + escape(data);
var link = document.createElement('a');
link.href = uri;
link.style = "visibility:hidden";
link.download = table_config.toolbar.tools.export.name+'.'+file_type;
document.body.appendChild(link);
link.click();
document.body.removeChild(link);
});
My export code;
return Excel::create($name, function($excel) use($records) {
$excel->sheet('Sheet 1', function($sheet) use($records) {
$sheet->fromArray($records);
});
})->export($type);
When I try to open the file. It is corrupt. This same code works fine with CSV
About this issue
- Original URL
- State: closed
- Created 8 years ago
- Reactions: 5
- Comments: 24 (1 by maintainers)
@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(); }
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
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
Thanks it worked For Me
Trust me on this one, it’s a problem with spaces as they get prepended/appended to the stream. Everybody who had this issue in the end found a space somewhere. You have to check all files, not only the once which get called.
It’s not a bug, if you remove the incorrect spaces (which shouldn’t be there in the first place) it will work fine. As well it’s not a problem with this package, but with the parent package PHPExcel, if you do it with that package you will get the exact same error. Nothing we can do on this side.
Thank you @vitaliyb