问题
Like many others out there i have had my fair share of issues trying to download an Excel file output by PHPExcel. What happened in my case was whenever I wanted to download a file using
$obj->save('php://output')
i always used to get garbled text in my excel file with a warning saying my file was corrupt. Eventually i resolved the issue. The problem being i had a
require('dbcon.php')
at the top of my php script. I just replaced that with whatever was there inside dbcon.php
and it worked fine again.
Though the problem is solved i would really like to know what caused the problem. It would be great if anyone out there could help me out with this one.
Thanks.
回答1:
If you get that error - you should follow the advice we always give in that situation: you use a text editor to look in the generated file for leading or trailing whitespace, or plaintext error messages - and then in your own scripts for anything that might generate that such as echo statements, blank lines outside ?>
<?php
, etc.
Another way of testing for this is to save to the filesystem rather than php://output and see if you get the same problem: if that works, then the problem is always something that your own script is sending to php://output as well.
Clearly you had a problem along those lines in your dbcon.php file. This can be as simple as a trailing newline after a closing ?>
in the file...
回答2:
Tanmay.
In situations like your's, there can be couple of reasons for broken output:
- in file dbcon.php can be a whitespace before opening or ending php tag, so that produce some chars to output and can make file broken (this is reason for using only opening tag in php 5.3+);
- maybe file dbcon.php wasn't found by require, so you got error message in otput;
- any other errors or notices or warnings in dbcon.php, because presence of global vars from current file..
来源:https://stackoverflow.com/questions/17853285/phpexcel-outputs-garbled-text