Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"
 
PerlMonks  

How to write to a excel file while downloading in cgi perl

by ghosh123 (Monk)
on Aug 02, 2013 at 06:45 UTC ( #1047555=perlquestion: print w/ replies, xml ) Need Help??
ghosh123 has asked for the wisdom of the Perl Monks concerning the following question:

Hi
I am working on extracting some information from a webpage and dumping them into a excel file. The webpage from which I am extracting the report has link on it as 'Create report'. Upon clicking that link it goes to a script in the server and does the following things :

1. Creates the Excel using Spreadsheet::WriteExcel
2. Extracts the page content using LWP::UserAgent, HTML::TableExtract qw(tree)
3. Writes to the excel and closes it .
4. Lets the user download it.

Now the question is, how can I write something to that generated excel file in time of downloading it. Say for example I want to write some heading and title at the begining of the excel file while downloading.
All the above 4 functionalities are working fine. For your ease of understanding I am just putting the last part of the code below :

## please assume that extracting the page content and ## writing to the excel is already done before this ## following code : $workbook->close; open(FILE, "<$filename") || die "Unable to download file \n"; my @fileholder; @fileholder = <FILE>; close (FILE); unlink $filename; print "Content-Type:application/x-download\n"; print "Content Disposition:attachment;filename=report.xls\n\n"; print @fileholder;

Thanks

Comment on How to write to a excel file while downloading in cgi perl
Download Code
Re: How to write to a excel file while downloading in cgi perl
by Corion (Pope) on Aug 02, 2013 at 07:06 UTC

    If the code writing to the Excel file is already written, why don't you apply your changes there, before calling $workbook->close?

    Alternatively, (re)open the workbook, make your changes, and then save it to a tempfile, and send that tempfile to the user.

    Also see Spreadsheet::ParseExcel::SaveParser.

Re: How to write to a excel file while downloading in cgi perl
by glasswalk3r (Pilgrim) on Aug 02, 2013 at 21:22 UTC

    I think you should change the order of operations:

    1. Extracts the page content using LWP::UserAgent, HTML::TableExtract
    2. Creates the Excel using Spreadsheet::WriteExcel
    3. Writes to the excel and closes it
    4. Lets the user download it

    This way you would avoid creating the Excel file if the content recovered is not correct (think getting a 404 from LWP).

    I would also consider two things:

    • Use XML instead of creating a Excel file: unless your formating and using formulas in the Excel, XML is more portable and probably faster to write using XML::Writer. Newer versions of Excel can work pretty well with XML (unless the XML content is really large).
    • If you really need Excel, then assign the content of the file to a scalar instead of an array. If you are at a Windows OS, maybe you should use binmode() before printing it.
    Alceu Rodrigues de Freitas Junior
    ---------------------------------
    "You have enemies? Good. That means you've stood up for something, sometime in your life." - Sir Winston Churchill

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://1047555]
Approved by ww
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (10)
As of 2014-07-10 23:08 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    When choosing user names for websites, I prefer to use:








    Results (217 votes), past polls