http://qs321.pair.com?node_id=575111

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.

    --
    <http://dave.org.uk>

    "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. ;-)

    <radiant.matrix>
    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.