Laravel-excel: Exported XLS has strange characters and XLSX are not valid and can't be opened

Created on 7 Apr 2014  路  11Comments  路  Source: Maatwebsite/Laravel-Excel

CSV seems to work fine. Using this example code:

    return Excel::create('ExcelTest')
            ->sheet('SheetName')
                ->with(array('data', 'data'))
            ->export('xlsx');

Running Laravel version 4.1.24

Screenshot of garbled xls file:
screen shot 2014-04-07 at 5 15 29 pm

I can upload/send the xls and xlsx files if needed.

Thanks,
Alex Graham

Most helpful comment

Please add the code before your file download code

ob_end_clean();
ob_start();

All 11 comments

Send the files to [email protected], I will have a look tomorrow morning

I confirm I can't open your files, but when I test the code you supplied with xls and xlsx I can open the just created files.

What kind of server are you running the code on? Are you using a different encoding than UTF-8?

I'm running the code on this vagrant server (
https://github.com/bryannielsen/Laravel4-Vagrant)

  • OS - Ubuntu 12.04
  • Apache - 2.4.6
  • PHP - 5.5.4
  • MySQL - 5.5.32
  • PostgreSQL - 9.1
  • Beanstalkd - 1.4.6
  • Redis - 2.2.12
  • Memcached - 1.4.13

I also ran the code on my production server which is an Amazon EC2 server
running PHP 5.5 with the same result.

My files should all be saved as UTF-8, it is the default in my text editor
(Sublime 3). I tried resaving my routes.php (where I am calling the Excel
code from) specifically as UTF-8, problem persists.

On Wed, Apr 9, 2014 at 2:42 AM, Maatwebsite [email protected]:

I confirm I can't open your files, but when I test the code you supplied
with xls and xlsx I can open the just created files.

What kind of server are you running the code on? Are you using a different
encoding than UTF-8?

Reply to this email directly or view it on GitHubhttps://github.com/Maatwebsite/laravel4-PHPExcel/issues/52#issuecomment-39933205
.

I fixed the issue-

One of the files I was autoloading began with an empty line instead of

Thanks again for the help.

Great :)

Having same problem ... but not rectifying .. pls help

I have attached the excel sheet. M using Laravel 5.* and running this code in localhost. Please check and same and tell me the solution to complete this.
laravel excel

Try following the thealexgraham 's suggestion. Every single time I saw this kind of error it had to do with a whitespace in front of <?php of after ?>

Hello my problem with this is I have large pack of files in my laravel. Is there a way how can I find the file with whitespace ?

Please add the code before your file download code

ob_end_clean();
ob_start();

@sachinshelke28 Thank you! This solved the problem.

Was this page helpful?
0 / 5 - 0 ratings