Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris

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]: (not a high priority bug because I can use some HTML entities in the second string)
[Corion]: base64 is padded to a multiple of 4 chars (or something)
[LanX]: misunderstanding, I joined them before converting to base64
[Corion]: Also, I would be wary of encodings and try to make really sure that both input strings are UTF-8. Maybe join the input strings from one source together to see whether they decode as bad or not
[Corion]: LanX: Then the problem should persist without encoding to base64 too ;)
[LanX]: I think it's a flag problem ... I'll produce a reprodocable example for SOPW
[Corion]: "flag problem" to me sounds like "contains UTF-8 bytes but was never properly decoded to an UTF-8 string"
[LanX]: not my code ...
[choroba]: yeah, sounds like one of the strings is not flagged as UTF-8
[choroba]: which usually means its input wasn't handled correctly

How do I use this? | Other CB clients
Other Users?
Others avoiding work at the Monastery: (11)
As of 2017-01-16 13:55 GMT
Find Nodes?
    Voting Booth?
    Do you watch meteor showers?

    Results (150 votes). Check out past polls.