[cgiapp] Output an excel file
This is my (coworkers) problem: My web app runs on Apache mod_perl using CGI::Application. I want to provide a download of a generated file. In the past (before we were using mod_perland CGI::App) I just spooled out a csv file to STDOUT as it was generated. Now I'm shooting for a little more refinement - creating an Excel spreadsheet using Spreadsheet::WriteExcel - and I can't seem it to get to print it out from the file handle. sub export_list { my $self = shift; binmode(STDOUT); my $str; open my $fh, '', \$str; my $workbook = Spreadsheet::WriteExcel-new($fh); my $worksheet = $workbook-add_worksheet(); $worksheet-write_col(0,0, ['some','data','here']); warn $str; return $str; } The output is just a blank response, and the warn is blank as well. The method I'm using to write the spreadsheet to a filehandle is pretty much straight out of the documentationhttp://search.cpan.org/dist/Spreadsheet-WriteExcel/lib/Spreadsheet/WriteExcel.pm#new%28%29, so I assume the problem is due to some CGI::App noobery on my part. The documentation's suggested methods for filehandles and mod_perl proved pretty fruitless as well. I guess I should mention I'm running on Windows, and that my current workaround is to create a file and provide the user with a link to it. That poses more problems, however, in regards to clearing out the directory and when to do so, and also authentication for access to the generated files. Suggestions? Scathing criticism? ( http://stackoverflow.com/questions/1349132/how-can-i-provide-a-temp-file-download-using-modperl-and-cgiapplication ) -- fREW Schmidt http://blog.afoolishmanifesto.com # CGI::Application community mailing list #### ## To unsubscribe, or change your message delivery options, ## ## visit: http://www.erlbaum.net/mailman/listinfo/cgiapp## #### ## Web archive: http://www.erlbaum.net/pipermail/cgiapp/ ## ## Wiki: http://cgiapp.erlbaum.net/ ## ####
Re: [cgiapp] Output an excel file
fREW Schmidt wrote: Now I'm shooting for a little more refinement - creating an Excel spreadsheet using Spreadsheet::WriteExcel - and I can't seem it to get to print it out from the file handle. In that case, I'd change somethings about your setup. Instead of creating the whole spreadsheet in memory, you should either write it out to a file and them stream it when finished (using CGI::Application::Plugin::Stream helps here, but you'd still need to clean it up afterwards, but really every web app should have a temp directory that periodically gets cleaned up) or print it as you create it (which means making the FH STDIN instead which might be trickier under mod_perl or maybe not). And then remember to close your workbook when it's done. -- Michael Peters Plus Three, LP # CGI::Application community mailing list #### ## To unsubscribe, or change your message delivery options, ## ## visit: http://www.erlbaum.net/mailman/listinfo/cgiapp## #### ## Web archive: http://www.erlbaum.net/pipermail/cgiapp/ ## ## Wiki: http://cgiapp.erlbaum.net/ ## ####
Re: [cgiapp] Output an excel file
Michael Peters wrote: And then remember to close your workbook when it's done. Also remember to send the right content type and other headers: $self-header_add('-type' = 'application/vnd.ms-excel'); And if you're not going with CAP::Stream, then also mime-type, content-length, and content-disposition/attachment. But really CAP::Stream makes this whole thing so much easier. And you can probably do the cleanup in your teardown method. -- Michael Peters Plus Three, LP # CGI::Application community mailing list #### ## To unsubscribe, or change your message delivery options, ## ## visit: http://www.erlbaum.net/mailman/listinfo/cgiapp## #### ## Web archive: http://www.erlbaum.net/pipermail/cgiapp/ ## ## Wiki: http://cgiapp.erlbaum.net/ ## ####
Re: [cgiapp] Output an excel file
First, have you tried CAP::Output::XSV yet? It's nice and simple if you don't need any of the fancy Spreadsheet::WriteExcel features. fREW Schmidt wrote: This is my (coworkers) problem: My web app runs on Apache mod_perl using CGI::Application. I want to provide a download of a generated file. In the past (before we were using mod_perland CGI::App) I just spooled out a csv file to STDOUT as it was generated. Now I'm shooting for a little more refinement - creating an Excel spreadsheet using Spreadsheet::WriteExcel - and I can't seem it to get to print it out from the file handle. sub export_list { my $self = shift; binmode(STDOUT); my $str; open my $fh, '', \$str; my $workbook = Spreadsheet::WriteExcel-new($fh); my $worksheet = $workbook-add_worksheet(); $worksheet-write_col(0,0, ['some','data','here']); warn $str; return $str; } The output is just a blank response, and the warn is blank as well. I'm missing the call to $worksheet-close in that code. I bet that'll fix your problem. The method I'm using to write the spreadsheet to a filehandle is pretty much straight out of the documentationhttp://search.cpan.org/dist/Spreadsheet-WriteExcel/lib/Spreadsheet/WriteExcel.pm#new%28%29, so I assume the problem is due to some CGI::App noobery on my part. The documentation's suggested methods for filehandles and mod_perl proved pretty fruitless as well. I guess I should mention I'm running on Windows, and that my current workaround is to create a file and provide the user with a link to it. That poses more problems, however, in regards to clearing out the directory and when to do so, and also authentication for access to the generated files. You could always use IO::File-new_tmpfile to pass into new(), and afterwards seek + print. There are more complex suggestions I could give, but let's see where this gets you first :-) Rhesa # CGI::Application community mailing list #### ## To unsubscribe, or change your message delivery options, ## ## visit: http://www.erlbaum.net/mailman/listinfo/cgiapp## #### ## Web archive: http://www.erlbaum.net/pipermail/cgiapp/ ## ## Wiki: http://cgiapp.erlbaum.net/ ## ####