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

The following test code is intended to put a hello message in a spreadsheet and send that spreadsheet to a browser. ActiveState Perl 5.6.0, build 623. Instead, oddly, it dumps the program source to the browser in a spreadsheet, with one row per program line. I just don't see where it is getting the source.... any suggestions?
use strict; use Spreadsheet::WriteExcel 0.31; use IO::Scalar; use CGI qw(:standard); print header('application/vnd.ms-excel'); my $xls_str; tie *XLS, 'IO::Scalar', \$xls_str; my $wb = Spreadsheet::WriteExcel->new(\*XLS); my $ws = $wb->addworksheet(); $ws->write(0, 0, "Hi Excel!"); $wb->close(); binmode(STDOUT); print $xls_str;

Replies are listed 'Best First'.
(tye)Re: Spreadsheet::WriteExcel to Browser Problem
by tye (Sage) on May 02, 2001 at 00:20 UTC

    If you are seeing source code instead of running source code then the problem is that your web server is misconfigured as far as this script is concerned.

    Your web server is not configured to recognize this as a CGI script.

            - tye (but my friends call me "Tye")
      Hmmm.... I think it is being recognized as perl, as the header is being sent, because the source shows up in an excel file in the browser window. Also, if I change the header to text, I get a browser window of binary gibberish. Hmm... perhaps my install of the module is bad...

        My guess is that the header isn't being sent but that you have named your CGI *.xls and IE is ignoring the header and interpretting the downloaded text as a spreadsheet and silently deciding to convert it to such.

        Update: Try it with a different browser. Or better, use LWP::Simple to see what is really being sent:

        perl -MLWP::Simple -e "print get('http://www.alienfishexchange.com/')" + | more perl -MLWP::Simple -e "print join '|',head('http://www.alienfishexchan +ge.com/')"

                - tye (but my friends call me "Tye")
Re: Spreadsheet::WriteExcel to Browser Problem
by jmcnamara (Monsignor) on May 02, 2001 at 01:46 UTC

    I added a hash-bang line to your code snippet and it ran without problem using Apache and ActivePerl.

    I think tye's analysis is correct.

    John.
    --


Re: Spreadsheet::WriteExcel to Browser Problem
by dws (Chancellor) on May 02, 2001 at 00:00 UTC
    You might have to add   binmode(XLS); before you start creating the spreadsheet. But that's probably not what's causing the problem you're seeing.

    The WriteExcel documentation suggests using "-" to designate STDOUT.

    print header("application/vnd.ms-excel"); binmode(STDOUT); my $wb = SpreadSheet::WriteExcel->new("-"); ...
    If that's not sufficient, you might need to add the header  Content-Disposition: attachment; filename=text.xls (Also suggested by an example in the WriteExcel.pm docs.)

    Update: You've probably already tested this, but does the machine on which you're running the browser have Excel installed, and will the browser invoke Excel for normal .xls files?

    Update 2: Ignore all of this until you've resolved the problem tye identified below.