output on browser

Topics: Developer Forum, User Forum
Oct 14, 2009 at 3:06 PM


I am using Zend Framework and trying to put excel file on the browser using the code below.

$objPHPExcel = new PHPExcel();

header('Content-Type: application/vnd.ms-excel');
header('Content-Disposition: attachment;filename="test.xls"');
$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5');

I am getting corrupted excel file. It worked when I tried to not use Zend Framework. Does anyone know what is the problem.



Oct 14, 2009 at 3:11 PM

Can you upload the corrupt file here: http://phpexcel.codeplex.com/WorkItem/View.aspx?WorkItemId=10749

We will inspect it and try to find the problem.

Oct 14, 2009 at 3:23 PM

Hi I have uploaded the file.



Oct 14, 2009 at 3:54 PM

The problem is that your PHP script (or an included PHP script) is saved as UTF-8 with BOM. It should be saved as UTF-8 without BOM. Otherwise it is effectively echoing 3 bytes at the start which is enough to corrupt the file so it is opened as plain text in Excel.

// three "invisible" bytes being echoed when script is saved as UTF-8 with BOM

You may read some technical explanation about BOM:

Recognize the 3 characteristic bytes when you open the Excel file:

Your solution is to save your scripts as UTF-8 without BOM. Check that your text editor is capable of that.

For example, here is what it should look like in Notepad++ (there they call "UTF-8 without BOM" for "ANSI as UTF-8")

Oct 15, 2009 at 4:42 PM


I have changed encoding and it worked! Thanks!