Unreadable content generated in Excel file

Topics: Developer Forum, User Forum
Mar 6, 2013 at 12:20 PM
Edited Mar 6, 2013 at 12:27 PM
Hi,

I'm currently working with PHPExcel to generate Excel files (filled with data retrieved from a database).

However, I've got a problem when generating the Excel file and letting the user download it through the web browser.

When opening the Excel document after its download, I can't read anything else than special characters...


Could you help to understand why ?
Here is a few configurations of my web server :
root@web:~# php -v
PHP 5.3.3-7+squeeze14 with Suhosin-Patch (cli) (built: Aug  6 2012 14:18:06)
Copyright (c) 1997-2009 The PHP Group
Zend Engine v2.3.0, Copyright (c) 1998-2010 Zend Technologies
    with Suhosin v0.9.32.1, Copyright (c) 2007-2010, by SektionEins GmbH
root@web:~# php -m
[PHP Modules]
bcmath
bz2
calendar
Core
ctype
date
dba
dom
ereg
exif
fileinfo
filter
ftp
gd
gettext
hash
iconv
imap
json
ldap
libxml
mbstring
mcrypt
mhash
mssql
mysql
mysqli
openssl
pcntl
pcre
PDO
pdo_dblib
pdo_mysql
Phar
posix
Reflection
session
shmop
SimpleXML
soap
sockets
SPL
standard
suhosin
sysvmsg
sysvsem
sysvshm
tokenizer
wddx
xml
xmlreader
xmlrpc
xmlwriter
zip
zlib

[Zend Modules]
Suhosin
Note that the gd version installed is the 2.0



Here is the code I'm testing and that generates unreadable characters.
include_once('Excel/PHPExcel.php');

$phpExcel = new PHPExcel();
$phpExcel->getActiveSheet()->setTitle("My Sheet");

$phpExcel->setActiveSheetIndex(0);
header("Content-Type: application/vnd.ms-excel");
header("Content-Disposition: attachment; filename=\"filename.xls\"");
header("Cache-Control: max-age=0");

$objWriter = PHPExcel_IOFactory::createWriter($phpExcel, "Excel5");
$objWriter->save("php://output");
exit;
Thanks a lot for your help!


Edit : This is what I get when I open the file
http://www.hostingpics.net/viewer.php?id=133098excelerror.png
http://www.hostingpics.net/viewer.php?id=350258excelerror2.png
Mar 6, 2013 at 1:32 PM
Edited Mar 6, 2013 at 1:33 PM
Well... I've found my mistake...
It was a stupid one (like every mistake we could say :] )

I forgot to comment one line before the generation of the document :
echo '<pre>';
These few characters were enough to corrupt the whole excel file !
Mar 7, 2013 at 8:58 AM
Edited Mar 7, 2013 at 10:03 AM
Hi again,

Since the last time, I tried to include this code into the CakePHP framework (which I'm currently using) but the Excel file generated is corrupted as if there was unexpected character before the encoding of the XLS.

After some researchs, I found that the framework insert a space before printing the view I want. That space corrupts my excel file.
Actually, when I delete this first space with notepad, I'm then able to open and read the Excel file I just downloaded...

I didn't find a way to prevent CakePHP print this space, thus I try to delete it manually before sending out the XLS file.

Is there a way to display the string generated by the
$objWriter = PHPExcel_IOFactory::createWriter($phpExcel, "Excel5");
If yes, I could delete the first character and then send the file without any issue !


Thanks a lot for your help :]


Edit : I just realized my method is not good... The XLS encoding generated by PHPExcel is good. It is what CakePHP adds that generates my issue... I'll try to find a solution on CakePHP's forums.
Mar 7, 2013 at 11:33 AM
FYI

There was a space after my final "?>" in a class I wrote in my framework. This space was interpreted in HTML and printed !