in reply to Running external apps

I use this code in a script:
#! /usr/bin/perl use strict; use warnings; use diagnostics; use CGI; use CGI::Carp qw(fatalsToBrowser); use DBI; use Data::Dumper; use lib qw(/data/perllib); use Spreadsheet::WriteExcel; use Conf; my $cgi = CGI->new(); my %params = $cgi->Vars(); print $cgi->header(-type => 'application/vnd.ms-excel',); # general settings for database-connection my $USER = $Conf::USER; my $PASSWORD = $Conf::PASSWORD; my $HOST = $Conf::HOST; my $DATABASE = $Conf::DATABASE; my $DBMS = $Conf::DBMS; my $driver = "dbi:$DBMS:$DATABASE:$HOST"; # create connection to database my $dbh = DBI->connect($driver,$USER,$PASSWORD); my $statement = "SELECT * FROM table;"; #---------------------------------------------------# # write EXCEL-file # #---------------------------------------------------# binmode(\*STDOUT); my $EXCEL = new Spreadsheet::WriteExcel(\*STDOUT); my $sheet = $EXCEL->addworksheet("Features"); my $state_h = $dbh->prepare($statement); $state_h->execute(); my $col_h = 1; $sheet->write(0,0,'Header1'); shift(@wanted_cols); foreach my $th(@wanted_cols){ $sheet->write(0,$col_h,$th); $col_h++; } my $row = 1; while(my @res_array = $state_h->fetchrow_array){ foreach my $col(0..(scalar(@res_array)-1)){ $sheet->write($row,$col,$res_array[$col]); } $row++; } $EXCEL->close();


This code creates an Excel-window where you can use the all the Excel-abilities!

Replies are listed 'Best First'.
Re^2: Running external apps
by zdog (Priest) on Sep 13, 2004 at 14:15 UTC
    This code creates an Excel-window where you can use the all the Excel-abilities!

    Along the lines of what dragonchild pointed out, that code does not really create an Excel window at all. It returns Excel data to the client, but it's the client's duty to handle it correctly by, for example, opening it in an Excel window. Under normal circumstances, this task is beyond the scope of the Perl script. The client must be configured correctly ( ie Excel is installed properly ) for this to work as expected.

    Zenon Zabinski | zdog | zdog@perlmonk.org

Re^2: Running external apps
by dragonchild (Archbishop) on Sep 13, 2004 at 14:37 UTC
    To elaborate what zdog has said, this returns data identified as Excel data to the client. What the client does with it is completely outside the control of the CGI, as it should be. For example, if this is returned to IE on WinXP, this may open an Excel window within the browser, but only if configured appropriately. When returned to Mozilla on WinXP, it will ask you if you want to Save or Open, at which point Excel is one of the choices. When returned to Galeon on Linux, it will ask for Save or Open, and OpenOffice (if installed) may be one of the choices.

    In other words, your script returns some data. The client does what it wants with it. That codes does NOT creates an Excel-window where you can use the all the Excel-abilities!.

    ------
    We are the carpenters and bricklayers of the Information Age.

    Then there are Damian modules.... *sigh* ... that's not about being less-lazy -- that's about being on some really good drugs -- you know, there is no spoon. - flyingmoose

    I shouldn't have to say this, but any code, unless otherwise stated, is untested