Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[QUESTION] XLS export not working #762

Closed
dlopez opened this issue Apr 13, 2016 · 24 comments
Closed

[QUESTION] XLS export not working #762

dlopez opened this issue Apr 13, 2016 · 24 comments

Comments

@dlopez
Copy link

dlopez commented Apr 13, 2016

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

@MehdiTAIEB
Copy link

MehdiTAIEB commented Jun 3, 2016

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

@mabadir
Copy link

mabadir commented Jun 18, 2016

I am facing a similar issue, with Laravel 5.2. Storing the file (XLS/XLSX) works fine, however the download('xlsx') function returns a corrupted file.

@patrickbrouwers
Copy link
Member

patrickbrouwers commented Jun 23, 2016

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.

@mabadir
Copy link

mabadir commented Sep 16, 2016

Indeed that was a wrong space from my end.

@LordRahl90
Copy link

Please once i upgraded to php7, i keep having this site cannot be reached in chrome with an ERR_INVALID_RESPONSE. Please is there anything am supposed to add or remove??

@LordRahl90
Copy link

it works on windows but not after i uploaded it to my linux server. please check out this url. http://jabu-portal.com.ng/test/excel it downloaded once and then it starts giving file not found error.
Thanks

@LordRahl90
Copy link

LordRahl90 commented Sep 24, 2016

This is the code for the test page
` Excel::create('Laravel Excel', function($excel) {

        $excel->sheet('Excel sheet', function($sheet) {

            $sheet->setOrientation('landscape');

        });

    })->export('xlsx');`

@PoojaKothe
Copy link

i am facing same problem in my current application. But the problem is i wrote the same code in two different methods, but its working in one method and not in the other one.

Not working method:
public function exportReport($reportName,$reportData,$reportType){
$data=json_encode($reportData);
$data=json_decode($data,true);
Log::info('Download generated file into local disk');
return (Excel::create($reportName, function($excel) use ($data,$reportName){
$excel->sheet($reportName, function($sheet) use ($data){
//Inserting the data in the excel sheet
$sheet->fromArray($data);
// Freeze first row
$sheet->freezeFirstRow();
});
})->download($reportType));
}

@topegret
Copy link

have the same issue---basically download csv works fine on my local development enviroment[ LAMP on windows+php5.6+Laravel 5.3 ]but got ERR_INVALID_RESPONSE on my remote production server. ubuntu, nginx+php7,0+laravel5.3... any ideas?

@LordRahl90
Copy link

@topegret download php7.0-zip to download the zip archive or change 'xslx' to 'xls' that's what I did an it worked.

@khalilst
Copy link

@LordRahl90 I had the same issue in exporting xlsx only, but installing php7.0-zip solved my problem. Well done brother

@topegret
Copy link

I checked mine, it was php 7.0 zip and I only need csv file...

@edanao
Copy link

edanao commented Mar 30, 2017

do we have a solution for this?
I am also experiencing the issue, i have php7 in my nginx server and i want to download xls file.
@topegret where you able to solve your issue?

Thank you for any help!

@vitaliyb
Copy link

vitaliyb commented Apr 5, 2017

@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(); }

@topegret
Copy link

topegret commented Apr 6, 2017 via email

@giangmd
Copy link

giangmd commented Jun 27, 2017

@topegret What is the way you solved?
I have same issue on centos + nginx

@edanao
Copy link

edanao commented Jun 28, 2017

Thank you @vitaliyb and @topegret !
It turns out that whenever I use composer update in Laravel, the PHPExcel module doesn't get updated so I am not getting the version correct version. I removed the extra break as suggested here PHPOffice/PHPExcel#642 and clear the config cache just to be sure and it worked.
Hope this helps.

@giangmd
Copy link

giangmd commented Jun 28, 2017

Finally, I have found the issue by config to /tmp folder of project.
It's working for me now.
Thanks.

@ashisgupta
Copy link

Hi, i had the similar error, in the following code :

return Excel::create($name, function($excel) use($records) {
            $excel->sheet('Sheet 1', function($sheet) use($records) {
                $sheet->fromArray($records);
            });
  })->export($type);

The File name($name) and sheet name(Sheet 1) should Not contain any space. After removing space, everything worked like a charm. Cheers.

@solomonhagos
Copy link

Thank you @vitaliyb

@sb53rm
Copy link

sb53rm commented Oct 23, 2018

@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

@szabizs
Copy link

szabizs commented Feb 4, 2019

if (ob_get_level() > 0) { ob_end_clean(); }

Great tip, thanks a lot!

@MisaelMa
Copy link

if (ob_get_level() > 0) { ob_end_clean(); }

Great tip, thanks a lot!

@codingstudio38
Copy link

//if your exported exec file not opening or showing not valid extension
function index(Request $req){
$users = DB::table('tbl_users')->select('id','name','email','address')->get(); //my data colection
ob_end_clean(); // use this
ob_start(); // and this
return Excel::download(new NewUserExport($users),'usersList.xlsx',\Maatwebsite\Excel\Excel::XLSX);
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests