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


In reply to Re: Write to an excel file by bliako
in thread Write to an excel file by MikeGerard

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.