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

I'm still writing a Dancer application and was wondering whether or not there is a plugin or module I could use to allow the user to download his results as an excel compatible file.

Usually I used MMM::Sylk to creat SLK files which Excel can open, but maybe there is something better? For example I found Data::Table with Data::Table::Excel, but it seems to have the disadvantage that I need to write to the filesystem in order to get an excel file.


s$$([},&%#}/&/]+}%&{})*;#$&&s&&$^X.($'^"%]=\&(|?*{%
+.+=%;.#_}\&"^"-+%*).}%:##%}={~=~:.")&e&&s""`$''`"e

Replies are listed 'Best First'.
Re: Modules for creating EXCEL compatible files on webserver
by Corion (Patriarch) on Jan 27, 2014 at 15:15 UTC

    I use Spreadsheet::WriteExcel opened to an in-memory filehandle:

    open my $fh, '>', \my $buffer; my $workbook = Spreadsheet::WriteExcel->new($fh); ... close $fh; print $buffer;

      I tried it, but I seem to do something wrong.

      I simply took the example from the documentation but simply get a 0 byte file. I will continue investigating, but maybe someone of you has some information what might be wrong?

      This is what I have in my Dancer route:

      content_type 'application/vnd.ms-excel'; my $buffer; open my $fh, '>', \$buffer; my $workbook = Spreadsheet::WriteExcel->new($fh); my $worksheet = $workbook->add_worksheet(); # Add and define a format my $format = $workbook->add_format(); # Add a format $format->set_bold(); $format->set_color('red'); $format->set_align('center'); # Write a formatted and unformatted string, row and column notation. my $col = 0; my $row = 0; $worksheet->write($row, $col, 'Hi Excel!', $format); $worksheet->write(1, $col, 'Hi Excel!'); # Write a number and a formula using A1 notation $worksheet->write('A3', 1.2345); $worksheet->write('A4', '=SIN(PI()/4)'); close $fh; return $buffer;

      Found the mistake
      I forgot $workbook->close();.


      s$$([},&%#}/&/]+}%&{})*;#$&&s&&$^X.($'^"%]=\&(|?*{%
      +.+=%;.#_}\&"^"-+%*).}%:##%}={~=~:.")&e&&s""`$''`"e
Re: Modules for creating EXCEL compatible files on webserver
by dasgar (Priest) on Jan 27, 2014 at 15:54 UTC

    One option would be to create a CSV file, which Excel can read.

    If the HTML output is a table, I've also seen one approach of saving the page via the browser and give it an .xls file extension. Excel seems to be able to open and read this. Not exactly a "pretty" solution, but it seems to work. (Not advocating this route. Just tossing out ideas.)

    If the web server happens to be on a Windows system that has Excel installed, you could also use Win32::OLE to control Excel to create the Excel file for download. Otherwise, my guess is that Corion's suggestion maybe the best route to go.

      ++ for the idea.

      The disadvantages of CVS are, as far as I can see:

      1. the user needs to specify the column formats when importing/opening the file
      2. columns are most times misinterpreted if you choose "standard"
      3. numbers get mixed up

      That's why I always used MMM::Sylk. I'm trying now out Excel::Writer::XLSX as it is the successor of the module Corion recommended.


      s$$([},&%#}/&/]+}%&{})*;#$&&s&&$^X.($'^"%]=\&(|?*{%
      +.+=%;.#_}\&"^"-+%*).}%:##%}={~=~:.")&e&&s""`$''`"e