Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
375 views
in Technique[技术] by (71.8m points)

php - Excel export: browser hangs up

I use this http://phpexcel.codeplex.com/ to export to excel.

need to export 11000 - 65000 rows... but when trying to do this browser hangs up. it do not answer with errors or other messages. just show that request in progress.

top shows that apache finished work. even apache log says that all done:

x.x.x.x - - [28/Jul/2011:12:28:38 +0300] "POST /doctor/lab/statistic/ HTTP/1.1" 200 2773504 "http://x.x.x.x:82/doctor/lab/statistic/" "Mozilla/5.0 (X11; Linux i686; rv:5.0) Gecko/20100101 Firefox/5.0"

but still no results.

system:

Linux Eearth 2.6.34-12-desktop #1 SMP PREEMPT 2010-06-29 02:39:08 +0200 i686
PHP Version 5.3.5
Apache/2.2.15 (Linux/SUSE) 

so the question

EDIT 1

header('Content-Type: application/vnd.ms-excel');
header('Content-Disposition: attachment;filename="'.$filename.'.xls"');
header('Cache-Control: max-age=0');
$writer = PHPExcel_IOFactory::createWriter($xls, 'Excel5');
$writer->save('php://output');
exit;

EDIT 2

i tried to save data to file like this $writer->save('test.xls'); filesize 2.7MB then if i do this:

header('Content-Type: application/vnd.ms-excel');
header('Content-Disposition: attachment;filename="'.$filename.'.xls"');
header('Cache-Control: max-age=0');
include 'test.xls';
exit;

download starts immediate...

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

It's possible that your web server is not emitting error messages, so I'd say check your error.log (should be hanging around in /var/log/apache2 or similar.) It might well be something trivial like not being able to write to a temporary file.

If that turns out as a dead-end, consider programmatically creating CSV files with echo statements - Excel will open those just fine.

EDIT

To produce CSV, do your headers

header('Content-Type: text/comma-separated-values);
header('Content-Disposition:attachment;filename="'.$filename.'.csv"');
header('Cache-Control: max-age=0');

Then output your column headers:

echo "col1,col2,col3,col4,..." 

followed by a new line. Then output your data:

while(/*Still got some rows*/) {
   echo $row[0] . "," .$row[1] "," + ...
}

Have a look at: http://en.wikipedia.org/wiki/Comma-separated_values, it's a really simple file format and Excel will open it without difficulty.

The other thing, which I hadn't thought of, is if you've got access to a Windows Server, you can produce Excel files directly via COM interop.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...