Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number

CGI Output in an excel file

by valavanp (Curate)
on Sep 27, 2006 at 09:18 UTC ( #575111=perlquestion: print w/replies, xml ) Need Help??
valavanp has asked for the wisdom of the Perl Monks concerning the following question:

Hi monks, When i write the CGI output to an excel file by using the below code it works fine.
#!/usr/bin/perl -w use CGI; use strict; print <<test_header; Content-type: application/msexcel Content-Disposition: attachment; filename= test.xls ID;PWXL;N;E P;PGeneral P;P0 P;P0.0 P;P0.00 test_header
But when i write the above coding by inserting a line between the print statement and the content type as shown i get the file printed directly in the browser, not in the excel format. I don't know the reason for that. So Monks your valuable thoughts in the above. Thanks.
print <<test_header; Content-type: application/msexcel Content-Disposition: attachment; filename= test.xls ID;PWXL;N;E P;PGeneral P;P0 P;P0.0 P;P0.00 test_header

20060927 Janitored by Corion: Added code tags, as per Writeup Formatting Tips

Replies are listed 'Best First'.
Re: CGI Output in an excel file
by davorg (Chancellor) on Sep 27, 2006 at 09:26 UTC

    It's hard to see what you're saying as your question is formatted so badly that the whitespace that you are talking about disappears. Please use <code> tags round your code examples.

    Your problem is that the blank line you are introducing is taken as ending the CGI headers. Therefore your header is seens as being part of the body of the response and the web server adds its own default content-type header (probably saying that the content is text/html). You can't have that blank line there as the first blank line in the response defines the end of the header.

    Update: Now that Corion has added code tags I can see what you're doing a little more clearly. I'm not sure why it's working as it is, as you need you insert a blank line between the end of the headers and the start of the content.


    "The first rule of Perl club is you do not talk about Perl club."
    -- Chip Salzenberg

Re: CGI Output in an excel file
by radiantmatrix (Parson) on Sep 27, 2006 at 14:54 UTC

    That's because the HTTP headers end with the first blank line. If you start with a blank line, the server says "oh, that's the end of the headers: I'm going to assume this is just text, then". When the browser is told "this is text", it just displays it.

    This is sort of like the old doctor joke: "Doctor, it hurts when I do this!" -- stop doing that. ;-)

    A collection of thoughts and links from the minds of geeks
    The Code that can be seen is not the true Code
    I haven't found a problem yet that can't be solved by a well-placed trebuchet
Re: CGI Output in an excel file
by pbeckingham (Parson) on Sep 27, 2006 at 14:35 UTC

    The simple answer is that the protocol is to format the output as:

    name: value name: value content
    Both of your examples violate the protocol, but I guess the browsers tolerate the latter. The code should be corrected.

    pbeckingham - typist, perishable vertebrate.

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://575111]
Approved by Corion
[LanX]: lol
[ambrus]: no, the chess bishop itself is a joke on real bi-shops
[ambrus]: or at least on the stereotype of bi-shops
[choroba]: we call chess bishops "archers"
[Eily]: choroba sorry, I had to get that off my chess
[ambrus]: chess certainly hasn't started those stereotypes, like how kings aren't that powerful but has convinced their whole country to work for them, etc. it's just poured them to a nice clean form that would easily get propagated.

How do I use this? | Other CB clients
Other Users?
Others browsing the Monastery: (13)
As of 2017-09-26 12:19 GMT
Find Nodes?
    Voting Booth?
    During the recent solar eclipse, I:

    Results (294 votes). Check out past polls.