Skip to content
Advertisement

PHPExcel: Download the Excel file on the client side

The problem was solved: For other users that may have this problem – notice the encoding of the PHP file. If you use PHPExcel it must be ANSII encoding and not UTF8, otherwise the EXCEL will be downloaded corruptly. The Headers that were added (answer 1) solved the problem after i changed the encoding of the file itself.

I am using PHPExcel in order to create an EXCEL from a table in MYSQL DB, so the user can download it to his computer.

The code bellow creates a correct Excel file but the problem is that it is downloaded to my server. I read in PHPExcel manual that i need to add headers:

JavaScript

But if i do that, the downloaded file is: 1. Has some jibrish inside 2. Says that Excel needs to fix it because the file is not good. The problem is that this file is saved as UTF8 and if i encode it as ANSI then it is working properly but of course it is a manual change and i need a working properly excel to reach the users.

What is the bug?

My code that works (but download the file to the server):

JavaScript

Advertisement

Answer

Remove the following inclusion:

JavaScript

You declared the object twice. Remove one of them:

JavaScript

Insert the following headers just before creating the Writer:

JavaScript

Instead of the following (which actually saves the file in the server):

JavaScript

Insert the following (which will create the downloadable file):

JavaScript

This should solve the gibberish text. If you still get such text, insert the following code before the last line ($objWriter->save("php://output");):

JavaScript

This worked for me. Hope it helps.

User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement