Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much
 
PerlMonks  

Write to an excel file

by MikeGerard (Initiate)
on Sep 25, 2024 at 17:07 UTC ( [id://11161941]=perlquestion: print w/replies, xml ) Need Help??

MikeGerard has asked for the wisdom of the Perl Monks concerning the following question:

I run a program on a server, where I want to send results to me (as an Excel file). My code somehow sent them to me via my downloads folder (don't ask me ow it did it!) This stopped working a while back, though my perl code has not changed. </p) <code> print $cgi->header(-type=>"application/vnd-ms-excel", -attachment=>"$attachment_name.xls"); print $cgi->start_html(-title=>"Exported Excel results"); <code/>

What I see on the data that I want to get starts with Content-Disposition: attachment; filename="17_1_25_September_25_September_2024_All Types_Geneva.xls" Content-Type: application/vnd.ms-excel; charset=ISO-8859-1 but nothing in my downloads folder!

Replies are listed 'Best First'.
Re: Write to an excel file
by hippo (Archbishop) on Sep 25, 2024 at 17:11 UTC

    This sounds very similar to the last question you asked. Perhaps you could take advantage of the suggestions in the replies to that node? Or else explain in more detail how they have failed to help you diagnose the problem.


    🦛

Re: Write to an excel file
by bliako (Abbot) on Sep 26, 2024 at 09:38 UTC

    From diagonally googling, it seems the "correct" mime type is "application/vnd.ms-excel" which you mention in the last paragraph but you have it wrong in the first. BTW, the above is for XLS files, there is a different mime type if your file is XLSX.

    I am quite certain that Excel::Writer::XLSX you mentioned in your other post (Excel file in perl program) has nothing to do with this. Perhaps it is your browser or some antivirus or you got the mime type wrong.

    The following script was taken from CGI's doc and works for me when accessing it with Firefox, albeit it does not ask me to save the file (specified with -attachment) but it opens the file with Libre Office. Note that CGI's doc mentions this:

    Note that the default being ISO-8859-1 may not make sense for all content types, e.g. Content-Type: image/gif; charset=ISO-8859-1 In the above case you need to pass -charset => '' to prevent the default being used.

    The headers you mentioned you are receiving show that you have a charset after the mime type. Perhaps that confuses your browser.

    Here is the script:

    # mycgi.pl use CGI; my $cgi = CGI->new; print $cgi->header( -type => 'application/vnd.ms-excel', #-nph => 1, -expires => '+3d', #-charset => 'utf-8', -charset => '', -attachment => 'foo.xls', ); # you need a foo.xls in your local dir open (IMAGE, '<', 'foo.xls'); print <IMAGE>; close IMAGE;

    And for those happy creatures who use Linux, here is a oneliner web server to check the above at http://localhost:8080:

    while true; do { echo -ne "HTTP/1.1 200 OK\r\n"; perl mycgi.pl; } | nc + -l -k 8080; done

    I guess you know this but you can open Firefox's Developer Tools and see what is received when you click the Network tab, including headers, cookies, etc. Check that first.

    Update: if you want your browser to just save the file, then why don't you use the binary file mime type: application/octet-stream

    bw, bliako

      if you want your browser to just save the file, then why don't you use the binary file mime type: application/octet-stream

      To save, use "Content-Disposition: attachment" with the proper MIME type. The browser will save that (unless you have a very, very broken browser). Using the CGI module, this can be archived with the -attachment argument passed to header().

      Alexander

      --
      Today I will gladly share my knowledge and experience, for there are no sweeter words than "I told you so". ;-)

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://11161941]
Front-paged by Corion
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others goofing around in the Monastery: (3)
As of 2024-10-08 06:11 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    The PerlMonks site front end has:





    Results (44 votes). Check out past polls.

    Notices?
    erzuuli‥ 🛈The London Perl and Raku Workshop takes place on 26th Oct 2024. If your company depends on Perl, please consider sponsoring and/or attending.