"Excel found unreadable content" warning when opening Excel files made with PHPExcel
As told by questioning user Tiny on his own comment:
Finally, it worked. I just added exit at the end of my PHP script (at the end of the first code snippet in the question). Thanks very much all of you for giving me useful hints.
To give some constructive additional tips on this kind of problems:
- A good tip is that you can omit the closing tag
?>
on all your PHP script files, that way you know that you're not sending any aditional invisible whitespace at the end of it. - Also enconding PHP script files on UTF-8 on wrongfully configured web server can send an undesirable couple bytes at the begining of the script. Open the PHP file on Notepad++, check if it's UTF-8, and in that case, convert it to ANSI. If that makes it work, check your webserver/php configuration.
Just before the
header
calls, you can check if headers has been wrongfully sent with:if ( headers_sent() ) die("**Error: headers sent");
If you can't prevent that some function call sends undesirable strings to the browser, you can "erase" all of it using at the very beginning of your script:
ob_start();
and then, just before the first
headers
call, use:ob_clean();
Be careful that with doing so will prevent you for receiving error feedback.
And lastly, as already said, if nothing has to be executed afterwards some point on the script, call
exit
ordie
.
Well I had this problem, my solution was changing the header Content-type to :
header('Content-Type: application/openxmlformats-officedocument.spreadsheetml.sheet');
I had before:
header('Content-Type: application/vnd.ms-excel');
and I changed the format to Excel2007, I was using Excel5 for excel 2003 xls format.
I tried all the solutions from this page and all the other pages with the same question but not luck. So basically, I just changed the output format of my excels and that solved the problem.
This error can also be triggered by serving the file without the Content-Length headers.
Look at the example given here in php.net