DelphiFAQ Home Search:

Perl script on web server sends Excel file to browser

 

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

Question:

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?

Answer:

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/exportcsv.pl then the downloaded file will by default be named

exportcsv.pl.xls (using alternative mime type application/vnd.ms-excel)

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

Note:
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/vnd.ms-excel\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";


Comments:

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+'testCSV.pl',
{
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')
{
    createCSV();
}
else
{
    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
2020-01-10, 21:34:26
anonymous from France  
rating
[url= https://scanaremmaamu.tk]how to quote picture book title in essay [/url]
[url= https://karmindmacma..nd.gq]best dating website now [/url]
[url= https://viltinigit.tk]writing prompts for persuasive essay [/url]
[url= https://escrinlessda..a.cf[/url]
[url= https://ereccheby.tk..y.tk[/url]
[url= https://ataturvorhe.tk]asian man dating tips [/url]
[url= https://ereccheby.tk..umentative speech topics funny argumentative essay topics [/url]
[url= https://ahinelrearoo..o.ml[/url]
[url= https://borwestnipot..k]examples of attention grabbing introduction for essays [/url]
[url= https://tralgentnire..e.tk[/url]
[url= https://flavdisfligh...ml]sample research essay topics [/url]
[url= https://maacompmerpe.tk]top 10 international dating sites [/url]
[url= https://flavdisfligh..oka.ml]how to include a webiste in an essay apa format [/url]
[url= https://dieplucarelb...gq]dating a conservative christian girl [/url]
[url= https://dawootaroral..l.ga[/url]
[url= https://riewithsaddc..gq]italian engineer online dating scam phjotos [/url]
2020-01-11, 04:35:46
anonymous from France  
rating
[url= https://firconfclassun.tk]how to write a title of a show in an essay [/url]
[url= https://alinatinen.c..n.cf[/url]
2020-01-11, 09:52:11
anonymous from France  
rating
[url= https://viltinigit.t..]introduce myself essay writing [/url]
[url= https://beituphotita.cf]widow and widower dating site [/url]
[url= https://scanaremmaamu.tk]essay mla format annotated bibliography [/url]
[url= https://rickleaguto...o.ga[/url]
[url= https://gridfibsyndd..umentative images argumentative essay [/url]
[url= https://stumfascuro.tk]is caitlyn jenner dating a black person [/url]
[url= https://ereccheby.tk]what does the p stand for in tape what types of essays are there [/url]
[url= https://tralgentnire.tk]list of top russian dating site for computers [/url]
[url= https://scanaremmaamu.tk]how to write a essay 5th grade [/url]
[url= https://tralgentnire.tk]pay online dating sites [/url]
[url= https://ereccheby.tk..y.tk[/url]
[url= https://uncenruigai...inabellina placerville ca dating and sex [/url]
[url= https://conhacalboyg..d.cf[/url]
[url= https://laigrousathw..mu.gq]best dating site for people in open relationships [/url]
[url= https://ereccheby.tk]example outline for essay about caregiving [/url]
[url= https://scorelentu.tk]best gay dating app for relationship [/url]
2020-01-11, 21:41:36
anonymous from France  
rating
[url= https://emomenyg.tk]..g.tk[/url]
[url= https://fighribali.t..i.tk[/url]
[url= https://conhacalboyg..d.cf[/url]
[url= https://stabpisfolks..h.gq[/url]
[url= https://emomenyg.tk]essays of comparing two systems of racial classification [/url]
[url= https://newsvothyski..g.gq[/url]
[url= https://conhacalboyg..cf]writing the perfect high school essay [/url]
[url= https://giwalzaci.tk]non asian women dating asian men website [/url]
[url= https://gridfibsyndd..o.gq[/url]
[url= https://conttazinebe.ml]best dating sims games [/url]
[url= https://conhacalboyg..d.cf[/url]
[url= https://uslusbavono.ml]first online dating telegraph telegram [/url]
[url= https://emomenyg.tk]how do you write a website title in an essay [/url]
[url= https://symtyfurbei.tk]best dating site for guys in their 20s [/url]
[url= https://flavdisfligh..ka.ml]lift off essay sample [/url]
[url= https://cephoticyc.t..c.tk[/url]
2020-01-12, 03:45:17
anonymous from France  
rating
[url= https://romnorojoban.ml]what is art essays [/url]
[url= https://cirneukarta.tk]online single dating site for women [/url]
2020-01-12, 09:02:58
anonymous from France  
rating
2020-01-13, 00:58:08
anonymous from France  
rating
2020-01-14, 19:36:10
anonymous from France  
rating
2020-09-06, 04:09:14
anonymous from France  
rating
<a href=' https://doctormedweb..>viagra without a doctor prescription</a>
[url= https://doctormedweb.com/# ]sildenafil viagra [/url]

 

 

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: