Perl Spreadsheet::WriteExcel : Output to browser - Always opens empty

nivo_Z
nivo_Z used Ask the Experts™
on
HI,
I'm writing a Perl based WEB application and I want to display Excel spreadsheet with data in the browser.
When I run the below example an Excel spreadsheet opens in the browser (I'm using IE 5.5, Windows 2000) but it is empty.
No matter what I tried, it always opens empty.
What is the problem?
Also, what is the meaning of the last line in the example description ("This can be useful for CGIs which have a Content-type of application/vnd.ms-excel") ???

Thanks.
 
Following is the description and the example:

2.4.6 Spreadsheet::WriteExcel Example: Output to browser
You can redirect the Spreadsheet::WriteExcel output to STDOUT using the special Perl filehandle "-". This can be useful for CGIs which have a Content-type of application/vnd.ms-excel:


    #!/usr/bin/perl -w


    use strict;
    use Spreadsheet::WriteExcel;
   
    print "Content-type: application/vnd.ms-excel\n\n";
   
    my $workbook = Spreadsheet::WriteExcel->new("-");
    $workbook->write(0, 0, "Hi Excel!");
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®

Commented:
The author is simply saying that the filehand "-" is useful when trying to direct output from a perl script that delivers a content type of "application/vnd.ms-excel"  Basically the content type is what tells your browser that the information you're sending it has a purpose, and that it should be displayed in a certain manner.  In this case they're simply pointing out that you can output the result of the SpreadSheet:WriteExcel routine to the browser in this manner.  Why doesn't it work?  I have no idea.  It seems to me that you may not have the required module to make the function run correctly.  Your browser gets the content-type segment, and opens its Excel spreadsheet viewer before your CGI reports the error that it doesn't have the module in place to actually input data into the spreadsheet.  Check with your sys admin about installing perl modules.

Author

Commented:
The thing is that when I don't direct the output to the browser and instead I create a spreadsheet on the local drive it works. I managed to create a spreadsheet and display the data successfully.
But this is a limitation because I want to allow access from any machine.
So, I guess that I have the required module.

Commented:
This is *possibly* due to a permissions issue.

Spreadsheet::WriteExcel needs to open a temporary file to operate. Returning a blank spreadsheet is a known symptom of IO::File not being able to open the temporary file for writing.

How should you proceed? Try writing a perl script which opens a file wherever your %TEMP is set to. Get it to put some text in the file. Run the script under mod_perl exactly as you do this Excel script. See if the file in %TEMP actually exists and contains what it should. If not, you have a permissions issue.

Author

Commented:
Problem found!

When I used:
    $worksheet->write(0, 0, "Hi Excel!");
Instead of:
    $workbook->write(0, 0, "Hi Excel!");
It worked!

Points refunded and placed in PAQ

Computer101
E-E Admin

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial