Very first functionnal code

This commit is contained in:
C.GRUSZKA 2017-03-27 16:07:16 +02:00
commit c4fbb4abcb
1 changed files with 105 additions and 0 deletions

105
SQL2XLS.pl Normal file
View File

@ -0,0 +1,105 @@
#!/usr/bin/env perl
#===============================================================================
#
# FILE: SQL2XLS.pl
#
# DESCRIPTION: truc qui prend une requête SQL et renvoie un tableau Excel
#
# OPTIONS: ---
# REQUIREMENTS: ---
# BUGS: ---
# NOTES: ---
# AUTHOR: grouch'
# ORGANIZATION:
# VERSION: 1.0
# CREATED: 27/03/17
# CHANGE_LOG:
#
#===============================================================================
use Carp;
use DBI;
use Data::Dumper;
use Modern::Perl;
use Spreadsheet::WriteExcel;
#-------------------------------------------------------------------------------
# DATABASE
#-------------------------------------------------------------------------------
my $user = 'postgres';
my $password = 'postgres';
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)
#-------------------------------------------------------------------------------
my $prog = 'ATO053';
my $datedebut = '20000101000000';
#my $datedebut = '20170201000000';
my $datefin = '20191231235959';
my $sql = qq{SELECT
MPOS
,OPr
,MES_BAR_BAR
,CRITICAL
,MAJOR
,ICD
,Pp
,Ppk
,COMMENTAIRE_DCM
,MINI
,MAXI
FROM calcul_capa($datedebut, $datefin, '$prog')
WHERE MINI notNULL AND MAXI notNULL
--AND Pp > 1.33 AND Ppk > 1
--LIMIT 12
};
#-------------------------------------------------------------------------------
# Requêtage
#-------------------------------------------------------------------------------
my $sth = $dbh->prepare($sql);
$sth->execute;
my $list = $sth->fetchall_arrayref;
my $nb_items = scalar @$list;
say "\nNombre de lignes : $nb_items";
#-------------------------------------------------------------------------------
# Excel
#-------------------------------------------------------------------------------
my $wb = Spreadsheet::WriteExcel->new("toto.xls");
$wb->set_properties(
title => 'héhé',
author => 'C.GRUSZKA',
comments => $sql,
);
my $ws = $wb->add_worksheet('Yo man!');
my $row = 0;
foreach my $token (@{$list}) {
my $col = 0;
foreach my $token (@{$list}[$row]) {
$ws->write($row, $col, $token);
$col++;
}
$row++;
}