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

I have XML files that I'm trying to serve via CGI to browser in such a way that it will launch Excel.

I also have stylesheets that need to be applied that I want to keep resident on the server.

I've been told that there are Java and VB modules that will take XML and a selected stylesheet and transform the XML file into what is essentially an HTML format, but that is also directly supported by Excel.

Anyone know of a clean way to do this in Perl? Thanks!

Replies are listed 'Best First'.
Re: XML to Excel with Stylesheet
by jimX11 (Friar) on Feb 06, 2007 at 18:20 UTC
    A browser may be set to download an excel file instead of launch a spreadsheet application. So you'd need to have fine grain control of your users browsers (and platform choice) in order to launch Excel.
    Spreadsheet::WriteExcel may interest you.
    You need to set the mime type. Here's a header method I use (in mod perl).
    sub print_html_header { my $self = shift; if ($self->style eq "excel") { my $filename = $self->new_excel_filename; $self->{r}->header_out('Content-Disposition'=>"attachm +ent; filename=$filename") } my $content_type = ($self->style eq 'excel') ? 'application/vn +d.ms-excel' : 'text/html'; $self->{r}->send_http_header($content_type); }
      Heh, turns out it isn't a problem. We're doing the same thing with PDFs via a 3rd party program, and I just found out that the same program can produce Excel as well. My lucky day!
Re: XML to Excel with Stylesheet
by clscott (Friar) on Feb 06, 2007 at 19:50 UTC

    I've tackled this task several ways including sending a csv file and building an excel file with SpreadSheet::WriteExcel

    My favourite trick for delivering dynamically generated Excel spreadsheets over the web is this:
    Send an html file to the browser with the Content-type header set to the MIME type for Excel (application/msexcel).

    The browser should prompt the user to open the file in Excel. Excel will parse and display the contents of the file, in cells so if your page is a bunch of tables, Excel will create a 1-to-1 cell mapping for the HTML table cells to Excel cells. You can make the tables as complex as you like, style them and the information and formatting comes through. It's a very fast way to get up and running especially if you are using a templating system like Template-Toolkit, HTML::Template or Petal.

    This has worked since the version of Excel before Office XP (2003)

    --
    Clayton