http://www.perlmonks.org?node_id=381508

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

Hello Friends, I wanted to name on the fly excelsheet
created in Perl file. Pls. do remember that I don't
want to you use any Module for the same.
I will first list down the code and then the problem
which I am facing.Pls. take a look at the code.
print "Content-type: application/vnd.ms-excel; CHARSET=x-sjis\n\n"; print "<HTML xmlns:x='urn:schemas-microsoft-com:office:excel' xmln +s:ss='urn:schemas-microsoft-com:office:spreadsheet'>";

After this I print the values in the excel sheet... The code is in say "abc.cgi" file.

Now the problem which I am facing is that by default the worksheet name appears as the "abc" where "abc" is the name of the cgi file which actually processes the request.

I wanted to dynamically name the worksheet without using any of the perl modules.

Would appreciate your inputs..
Thanks in advance..

20040810 Janitored by Corion: Added formatting, code tags

Replies are listed 'Best First'.
Re: Naming a Worksheet in Perl
by bgreenlee (Friar) on Aug 10, 2004 at 10:59 UTC

    You need to add a Content-disposition header:

    print "Content-type: application/vnd.ms-excel; CHARSET=x-sjis\n"; print "Content-disposition: attachment; filename=yourfilename.xls\n\n" +;

    (note that I moved the double newline to after the content-disposition header; two newlines tells the browser that you're done sending headers)

    brad

      bgreenlee's solution is the one that I use and has been recommened to me by others. It saves the pain of the redirect.

      --
      Clayton

        This is a good option, if you clearly know its limits:

        • dixit RFC 2616 (HTTP/1.1):

          Content-Disposition is not part of the HTTP standard, but since it is widely implemented, we are documenting its use and risks for implementors.
        • dixit Java Enterprise Best Practices:

          The bad news is that [...] many browsers second-guess the server's directives and do what they think is best rather than what they're told. These browsers--including Microsoft Internet Explorer and Opera--look at the file extension and "sniff" the incoming content. If they see HTML or image content, they inline-display the file contents instead of offering a Save As dialog.

        I just wanted to be rigorous. BTW last reference explains how to circumvent browser oddities with Content-disposition.

        ____
        HTH, Dominique
        My two favorites:
        If the only tool you have is a hammer, you will see every problem as a nail. --Abraham Maslow
        Bien faire, et le faire savoir...

Re: Naming a Worksheet in Perl
by dfaure (Chaplain) on Aug 10, 2004 at 11:16 UTC
    I wanted to dynamically name the worksheet without using any of the perl modules.

    You may play with HTTP protocol and use a "two passes" download, making your cgi act as a filter:

    1. When solicited, make your cgi reply with an HTTP 302 response (object moved) and give a new location attribute such as: <url_to_cgi>/<path_to_filename>.
    2. According to HTTP response, the client browser will requests again the datas from your cgi giving you back the filename from 1st pass. You'll know which one is the current pass via $ENV{'REQUEST_URI'}. Don't forget to set the mime/type.

    ____
    HTH, Dominique
    My two favorites:
    If the only tool you have is a hammer, you will see every problem as a nail. --Abraham Maslow
    Bien faire, et le faire savoir...