Training, Open Source computer languages
PerlPHPPythonMySQLApache / TomcatTclRubyJavaC and C++LinuxCSS 
Search for:
Home Accessibility Courses Diary The Mouth Forum Resources Site Map About Us Contact
For 2023 - we are now fully retired from IT training.
We have made many, many friends over 25 years of teaching about Python, Tcl, Perl, PHP, Lua, Java, C and C++ - and MySQL, Linux and Solaris/SunOS too. Our training notes are now very much out of date, but due to upward compatability most of our examples remain operational and even relevant ad you are welcome to make us if them "as seen" and at your own risk.

Lisa and I (Graham) now live in what was our training centre in Melksham - happy to meet with former delegates here - but do check ahead before coming round. We are far from inactive - rather, enjoying the times that we are retired but still healthy enough in mind and body to be active!

I am also active in many other area and still look after a lot of web sites - you can find an index ((here))
Excel Download Perl CGI

Posted by gmeyer (gmeyer), 23 March 2004
Hi Guys,

I am having difficulty getting my 'Download to Excel' button to actually pop up a Save As window and download the excel spreadsheet.

I am using CGI module to generate the web page and also Spreadhseet::WriteExcel to create the xls file.

I need help on getting the following code working please:
my $query = new CGI;
print $query->header('application/,-attachment=>'path to excel file');

Am I barking up the wrong tree??

Any help appreciated.


Posted by admin (Graham Ellis), 23 March 2004
Something odd with the quotes there, and (I think) a mixture of the two syntaxes for header.   Have you tried something like
header(-type => "application/",
          -attachment => "path to excel file");

Posted by gmeyer (gmeyer), 23 March 2004
Hi Graham,

Sorry mistyped in code looks eactly as u have suggested, but Im afraid it doesnt work

If I just have the 'application/' part of the code then it does open excel in the browser. However there is nothing to put in it of course because there is no attachment!


Posted by admin (Graham Ellis), 24 March 2004
What's the rest of your code doing / generating?    I think the -attachment option simply sets the name that the file is to be given when it's saved at your local machine and that you need to actually send the content too.

Posted by gmeyer (gmeyer), 24 March 2004
Short history of problem:
originally there was simply a hyperlink 'Download to Excel' which pointed to report.xls This worked if right clicked and save as..but left click sometimes opens straight in the browser. My idea was to write Perl CGI prog instead so that the user can click a 'Download to Excel' button.

Ive tried different code including following:
use strict;
use CGI qw /:standard /;
my $query = new CGI;
print $query->header(-type=>"application/",-attachment=>"path/file.xls",-Content_length=>" -s path/file.xls");
open (FH, "path/file.xls");
print $_ while <FH>;
close (FH);

Posted by admin (Graham Ellis), 25 March 2004
I would tend to use read rather than < ... > to read the file as it will contain binary information, but that's probably just a niceity;  it will loop in very odd blocks the way you have it, but I think it will work.

Have you tried seting the MIME type for .xls on your server to application/ and simply sending a link?   This would cut out the Perl program (oops - shouldn't suggest that on the Perl board!).   An experiment worth trying anyway as it will help you see if you're looking at server or client side isssues with those "sometimes" issues.

I would also try the script out with "application/octet-stream" which should force download to file - again as a tool to help research what you're seeing.

Posted by gmeyer (gmeyer), 25 March 2004
Hi Graham,

Thanks for the advice. I originally did have a hyperlink to the excel file (not pl prog). I have now found a way of always opening excel when the link is clicked. Unfortunately it involves changing a setting on each of the clients. Oh well they can live with that



This page is a thread posted to the opentalk forum at and archived here for reference. To jump to the archive index please follow this link.

1 unpublished comment pending on this page

edit your own (not yet published) comments

© WELL HOUSE CONSULTANTS LTD., 2023: Well House Manor • 48 Spa Road • Melksham, Wiltshire • United Kingdom • SN12 7NY
PH: 01144 1225 708225 • FAX: 01144 1225 793803 • EMAIL: • WEB: • SKYPE: wellho