DelphiFAQ Home Search:

Perl script on web server sends Excel file to browser


comments15 comments. Current rating: 3 stars (14 votes). Leave comments and/ or rate it.


I need to have my web site generate an Excel worksheet on the fly (a database export) and send it to the user's webbrowser. How can I do that?


The following assumes that you export a simple sheet containing strings and numbers, no formulas.

In this case, you can generate a CSV file (comma separated values) and set the mime type to applicatioon/octet-stream.

If you perl script is called as /cgi-bin/ then the downloaded file will by default be named (using alternative mime type application/

To suggest a specific file name (other than the script name, use the Content-Disposition header field. This field allows you to provide other information as well, e.g.

- creation-date
- modification-date
- read-date
- size

The size parameter indicates just an approximate size of the file in octets. It does not have to be the exact size. This information can be used by the client software (web browser) to pre-allocate space before attempting to store the file, or to determine whether enough space exists.

For the full syntax, see RFC 2183

# alternative 1:
# sending a file simply as an Excel
print "Content-type: application/\n";
print "Content-Disposition: attachment;filename=users.csv\n\n";

# alternative 2:
# sending a CSV file and suggesting 'users.csv' as the file name
print "Content-type: application/octet-stream\n";
print "Content-Disposition: attachment;filename=users.csv\n\n";

# now dump the data
# all values are enclosed by " in case the 
# strings contain a comma
# this dumps 2 rows with 2 columns each
print '"1","Test, first one"' . "\n";
print '"2","Test, last one"' . "\n";

Content-type: text/html


2009-06-07, 22:55:14
anonymous from Canada  
I'm calling a Perl Script from a Javascript (AJAX) which has to generate CSV on the fly... I tried using the above code. But its not working.

JavaScript Function

function testCSV()
alert('Got the data');    
new Ajax.Request(cgiPath+'',
method: 'get',
parameters: {
action: 'createCSV',
    type: 'sample',
    ts: new Date().getTime()
onComplete: function() {
    alert('Created CSV');

Perl Script

use CGI;
use JSON;

$cgi = new CGI();
$action = $cgi->param('action');

if ($action eq 'createCSV')
    die 'Action not specified';

sub createCSV

    print 'Content-type: application/octet-stream\n';
    print 'Content-Disposition: attachment;filename=users.csv\n\n';
    print ''1','Test, first one'' . '\n';
    print ''2','Test, last one'' . '\n';


Please help
2019-04-08, 15:42:07
anonymous from Russian Federation  
[url=]free adult chat[/url] [url=]hot webcam girls[/url] [url=]adult cam chat[/url] [url=]sex chat rooms[/url] [url=]sex chat free[/url]
2019-04-08, 16:23:34
anonymous from Russian Federation  
[url=]buy generic valtrex online[/url] [url=]cheap tadalafil 20mg[/url] [url= https://celebrex100...m/]generic celebrex[/url] [url= https://cheapprednis../]purchass of prednisolone tablets[/url] [url= https://prednisone91..sone[/url] [url= https://buysildenafi..sildenafil 50mg[/url] [url=]no prescription prednisone[/url] [url= https://buyamoxicill..moxicillin without a prescription[/url] [url= https://prednisolone..ednisolone online[/url] [url=]buy doxycycline[/url]
2019-04-08, 17:03:13
anonymous from Russian Federation  
2019-04-08, 17:19:46
anonymous from Russian Federation  
[url=]buy albuterol online without prescription[/url] [url=]cheap valtrex online[/url] [url= https://femaleviagra..m/]levitra for women[/url] [url=]buy generic doxycycline[/url] [url=]..asix[/url] [url= https://prednisone91..sone[/url] [url=]buy prednisolone 5mg[/url] [url= https://buyacyclovir..]acyclovir buy online[/url] [url=]cheap stromectol[/url] [url= https://metformin100..]metformin 500mg[/url]
2019-04-08, 17:25:39
anonymous from Russian Federation  
2019-04-08, 17:33:35
anonymous from Russian Federation  
2019-04-08, 17:35:26
anonymous from Russian Federation  
2019-04-08, 19:14:41
anonymous from Russian Federation  
[url= https://tadalafilgen..]tadalafil tablets 20 mg[/url]
2019-04-08, 19:33:15
anonymous from Russian Federation  
2019-04-08, 19:41:36
anonymous from Russian Federation  
2019-04-08, 20:30:36
anonymous from Russian Federation  
[url= https://lisinoprilze..pril[/url] [url=]viagra soft[/url] [url=]cost of tadalafil[/url] [url=]buy propecia finasteride[/url] [url= https://amoxicillin9..moxicillin brand name[/url] [url= https://cheapprednis..ednisolone uk[/url] [url= https://tadalafilgen..m/]generic cialis tadalafil[/url] [url= https://lisinopril0...lisinopril 10mg[/url] [url= https://valtrex500mg..m/]valtrex cheap[/url] [url=]buy lasix online no prescription[/url]
2019-04-08, 21:47:35
anonymous from Russian Federation  
2019-04-08, 22:13:47
anonymous from Russian Federation  
[url=]adult videos[/url]
2019-04-08, 23:42:25
anonymous from Russian Federation  



NEW: Optional: Register   Login
Email address (not necessary):

Rate as
Hide my email when showing my comment.
Please notify me once a day about new comments on this topic.
Please provide a valid email address if you select this option, or post under a registered account.

Show city and country
Show country only
Hide my location
You can mark text as 'quoted' by putting [quote] .. [/quote] around it.
Please type in the code:

Please do not post inappropriate pictures. Inappropriate pictures include pictures of minors and nudity.
The owner of this web site reserves the right to delete such material.

photo Add a picture: