output on browser

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

Hi

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');
$objWriter->save('php://output');

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

Thanks...

 

Developer
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.

Thanks.

 

Developer
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.

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


You may read some technical explanation about BOM:
http://en.wikipedia.org/wiki/Byte-order_mark

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

Hi

I have changed encoding and it worked! Thanks!