Now coded as a sub

This commit is contained in:
C.GRUSZKA 2017-03-28 08:53:35 +02:00
parent c4fbb4abcb
commit 1956ed7b4b
1 changed files with 46 additions and 20 deletions

View File

@ -33,14 +33,8 @@ my $host = 'localhost';
my $port = '5432';
my $dbname = 'TRIDIM_TEST01';
my $dbh = DBI->connect("dbi:Pg:dbname=$dbname;host=$host;port=$port",
$user,
$password,
{AutoCommit => 1, RaiseError => 1, PrintError => 0}
);
#-------------------------------------------------------------------------------
# Requête SQL (faudra la mettre en argument de la fonction mais pour le moment on teste)
# Requête SQL
#-------------------------------------------------------------------------------
my $prog = 'ATO053';
my $datedebut = '20000101000000';
@ -69,37 +63,69 @@ my $datefin = '20191231235959';
};
#-------------------------------------------------------------------------------
# Requêtage
#-------------------------------------------------------------------------------
# APPEL SUB
#-------------------------------------------------------------------------------
SQL2XLS($user, $password, $host, $port, $dbname, $sql, 'toto.xls', 'Yo!!!');
#-------------------------------------------------------------------------------
# SUB SQL2XLS
# Arguments $user, $password, $host, $port, $dbname, $sql, $filename, $tab
#-------------------------------------------------------------------------------
sub SQL2XLS{
my $user = shift;
my $password = shift;
my $host = shift;
my $port = shift;
my $dbname = shift;
my $sql = shift;
my $filename = shift;
my $tab = shift;
# connexion base
my $dbh = DBI->connect("dbi:Pg:dbname=$dbname;host=$host;port=$port",
$user,
$password,
{AutoCommit => 1, RaiseError => 1, PrintError => 0}
);
#requête
my $sth = $dbh->prepare($sql);
$sth->execute;
my $list = $sth->fetchall_arrayref;
my $colonnes = $sth->{NAME};
my $nb_col = scalar @$colonnes;
say "\nNombre de colonnes : $nb_col";
my $nb_items = scalar @$list;
say "\nNombre de lignes : $nb_items";
#-------------------------------------------------------------------------------
# Excel
#-------------------------------------------------------------------------------
my $wb = Spreadsheet::WriteExcel->new("toto.xls");
#Excel
my $wb = Spreadsheet::WriteExcel->new($filename);
$wb->set_properties(
title => 'héhé',
title => 'SQL2XLS',
author => 'C.GRUSZKA',
comments => $sql,
);
my $ws = $wb->add_worksheet('Yo man!');
my $ws = $wb->add_worksheet($tab);
# entêtes de colonnes
my $col = 0;
foreach my $token (@{$colonnes}) {
$ws->write(0, $col, $token);
$col++;
}
# données
my $row = 0;
foreach my $token (@{$list}) {
my $col = 0;
foreach my $token (@{$list}[$row]) {
$ws->write($row, $col, $token);
$ws->write($row+1, $col, $token);
$col++;
}
$row++;
}
}