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! |