From 1a902df1b87dd6b3055b542ce985d75211bd2cba Mon Sep 17 00:00:00 2001 From: "C.GRUSZKA" Date: Tue, 28 Mar 2017 16:54:12 +0200 Subject: [PATCH 1/2] Implementation of Excel::Writer::XLSX and many other improvements --- SQL2XLS.pl | 72 +++++++++++++++++++----------------- SQL2XLS.pm | 106 ++++++++++++++++++++++++----------------------------- 2 files changed, 85 insertions(+), 93 deletions(-) diff --git a/SQL2XLS.pl b/SQL2XLS.pl index 7fa261b..b0e4a9b 100644 --- a/SQL2XLS.pl +++ b/SQL2XLS.pl @@ -3,7 +3,7 @@ # # FILE: SQL2XLS.pl # -# DESCRIPTION: truc qui prend une requête SQL et renvoie un tableau Excel +# DESCRIPTION: script de démo du module SQL2XLS.pm # # OPTIONS: --- # REQUIREMENTS: --- @@ -28,54 +28,58 @@ use Spreadsheet::WriteExcel; #------------------------------------------------------------------------------- # DATABASE #------------------------------------------------------------------------------- -my $user = 'postgres'; -my $password = 'postgres'; -my $host = 'localhost'; -my $port = '5432'; -my $dbname = 'TRIDIM_TEST01'; +my $db = { + user => 'postgres' + ,password => 'postgres' + ,host => 'localhost' + ,port => '5432' + ,dbname => 'TRIDIM_TEST01' +}; #------------------------------------------------------------------------------- # Requête SQL #------------------------------------------------------------------------------- -my $prog = 'ATO053'; +my $prog = 'ATO045'; my $datedebut = '20000101000000'; #my $datedebut = '20170201000000'; my $datefin = '20191231235959'; - - my $sql1 = qq{SELECT - MPOS - ,NOM - ,MINI - ,MAXI - ,MES_BAR_BAR - ,Pp - ,Ppk - ,NbPieces - ,nb_total_occurences - - ,CRITICAL - ,MAJOR - ,ICD - ,COMMENTAIRE_DCM +# une première requête +my $sql1 = qq{SELECT + MPOS + ,NOM + ,MINI + ,MAXI + ,MES_BAR_BAR + ,Pp + ,Ppk + ,NbPieces + ,nb_total_occurences - - FROM calcul_capa($datedebut, $datefin, '$prog') + ,CRITICAL + ,MAJOR + ,ICD + ,COMMENTAIRE_DCM + + + FROM calcul_capa($datedebut, $datefin, '$prog') - WHERE MINI notNULL AND MAXI notNULL - --AND Pp > 1.33 AND Ppk > 1 - --LIMIT 12 - }; - - my $sql2 = qq{SELECT * FROM DATA WHERE PROG = '$prog'}; - - my %requete = ("capa" => $sql1, "data" => $sql2); + WHERE MINI notNULL AND MAXI notNULL + --AND Pp > 1.33 AND Ppk > 1 + }; + +#une deuxième requête +my $sql2 = qq{SELECT * FROM DATA WHERE PROG = '$prog'}; + +#la sub prend une ref de hash en argument +# nom_de_l'onglet => requete_sql +my $requete = {"capa" => $sql1, "data" => $sql2}; #------------------------------------------------------------------------------- # APPEL SUB #------------------------------------------------------------------------------- -SQL2XLS::SQL2XLS($user, $password, $host, $port, $dbname, \%requete, 'toto.xls'); +SQL2XLS::SQL2XLS($db, $requete, 'toto.xlsx'); diff --git a/SQL2XLS.pm b/SQL2XLS.pm index f57d4a4..42bc1b6 100644 --- a/SQL2XLS.pm +++ b/SQL2XLS.pm @@ -1,4 +1,4 @@ -#!/usr/bin/env perl +package SQL2XLS; #=============================================================================== # # FILE: SQL2XLS.pm @@ -10,90 +10,78 @@ # BUGS: --- # NOTES: --- # AUTHOR: grouch' -# ORGANIZATION: +# ORGANIZATION: non # VERSION: 1.0 # CREATED: 28/03/17 # CHANGE_LOG: # #=============================================================================== -package SQL2XLS; use Carp; use DBI; use Modern::Perl; -use Spreadsheet::WriteExcel; +use Excel::Writer::XLSX; use Exporter; +use Data::Dumper; +#trucs que je comprends pas tout our @ISA = qw( Exporter ); our @EXPORT = (); our @EXPORT_OK = qw(SQL2XLS); use subs qw(SQL2XLS); + #------------------------------------------------------------------------------- # SUB SQL2XLS -# Arguments $user, $password, $host, $port, $dbname, \%requete, $filename +# arguments : \%db \%requete, $filename #------------------------------------------------------------------------------- sub SQL2XLS{ - -my $user = shift; -my $password = shift; -my $host = shift; -my $port = shift; -my $dbname = shift; -my $ref_requete = 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} - ); - -my $wb = Spreadsheet::WriteExcel->new($filename); + my $ref_db = shift; + my $ref_requete = shift; + my $filename = shift; -foreach $tab (keys %{$ref_requete}){ - - my $sql = %{$ref_requete}{$tab}; - - say "\n\nOnglet $tab"; - #requête - my $sth = $dbh->prepare($sql); - $sth->execute; - my $list = $sth->fetchall_arrayref; - my $colonnes = $sth->{NAME}; - my $nb_col = scalar @$colonnes; - say "Nombre de colonnes : $nb_col"; - my $nb_items = scalar @$list; - say "Nombre de lignes : $nb_items"; - - #Excel + # connexion base + say "dbi:Pg:dbname=${$ref_db}{dbname};host=${$ref_db}{host};port=${$ref_db}{port}"; + my $dbh = DBI->connect("dbi:Pg:dbname=$ref_db->{dbname};host=$ref_db->{host};port=$ref_db->{port}", + $ref_db->{user}, + $ref_db->{password}, + {AutoCommit => 1, RaiseError => 1, PrintError => 0} + ); + #création du fichier Excel + my $wb = Excel::Writer::XLSX->new($filename); $wb->set_properties( title => 'SQL2XLS', author => 'C.GRUSZKA', - comments => $sql, - ); + ); - my $ws = $wb->add_worksheet($tab); - - # entêtes de colonnes - my $col = 0; - foreach my $token (@{$colonnes}) { - $ws->write(0, $col, $token); - $col++; + #on boucle pour créer tous les onglets du workbook + foreach my $tab (keys %{$ref_requete}){ + #création de l'onglet + say "\n\nOnglet $tab"; + my $ws = $wb->add_worksheet($tab); + $ws->set_zoom(85); + + #récupèration de la requête SQL associée à l'onglet + my $sql = $ref_requete->{$tab}; + + #requêtage ! + my $sth = $dbh->prepare($sql); + $sth->execute; + my $list = $sth->fetchall_arrayref; + my $colonnes = $sth->{NAME}; + my $nb_col = scalar @$colonnes; + say "Nombre de colonnes : $nb_col"; + my $nb_items = scalar @$list; + say "Nombre de lignes : $nb_items"; + + #construction de l'array de hash des entêtes de colonnes (à tes souhaits) + my @arraycol; + foreach my $col (@{$colonnes}){ + push @arraycol, { header => "$col" }; } - - # données - my $row = 0; - foreach my $token (@{$list}) { - my $col = 0; - foreach my $token (@{$list}[$row]) { - $ws->write($row+1, $col, $token); - $col++; - } - $row++; + + #table EXCEL + $ws->add_table(0,0,$nb_items,$nb_col-1, { data => $list, header_row => 1, autofilter => 1,style => 'Table Style Medium 5', columns => \@arraycol }); } } -} 1; \ No newline at end of file From 7afc4a58a8e94a0299f961a6312a211c350e3197 Mon Sep 17 00:00:00 2001 From: mrdjez Date: Tue, 28 Mar 2017 20:22:07 +0200 Subject: [PATCH 2/2] Verif... --- SQL2XLS.pl | 33 +++++++++++++-------------------- SQL2XLS.pm | 24 ++++++++++++------------ 2 files changed, 25 insertions(+), 32 deletions(-) diff --git a/SQL2XLS.pl b/SQL2XLS.pl index b0e4a9b..ed7fe4a 100644 --- a/SQL2XLS.pl +++ b/SQL2XLS.pl @@ -1,4 +1,4 @@ -#!/usr/bin/env perl +#!/usr/bin/env perl #=============================================================================== # # FILE: SQL2XLS.pl @@ -10,10 +10,10 @@ # BUGS: --- # NOTES: --- # AUTHOR: grouch' -# ORGANIZATION: +# ORGANIZATION: # VERSION: 1.0 # CREATED: 27/03/17 -# CHANGE_LOG: +# CHANGE_LOG: # #=============================================================================== @@ -37,15 +37,15 @@ my $db = { }; #------------------------------------------------------------------------------- -# Requête SQL -#------------------------------------------------------------------------------- +# Requête SQL +#------------------------------------------------------------------------------- my $prog = 'ATO045'; my $datedebut = '20000101000000'; #my $datedebut = '20170201000000'; my $datefin = '20191231235959'; # une première requête -my $sql1 = qq{SELECT +my $sql1 = qq{SELECT MPOS ,NOM ,MINI @@ -55,35 +55,28 @@ my $sql1 = qq{SELECT ,Ppk ,NbPieces ,nb_total_occurences - + ,CRITICAL ,MAJOR ,ICD ,COMMENTAIRE_DCM - - + + FROM calcul_capa($datedebut, $datefin, '$prog') WHERE MINI notNULL AND MAXI notNULL --AND Pp > 1.33 AND Ppk > 1 }; - + #une deuxième requête my $sql2 = qq{SELECT * FROM DATA WHERE PROG = '$prog'}; - + #la sub prend une ref de hash en argument # nom_de_l'onglet => requete_sql my $requete = {"capa" => $sql1, "data" => $sql2}; - + #------------------------------------------------------------------------------- # APPEL SUB -#------------------------------------------------------------------------------- +#------------------------------------------------------------------------------- SQL2XLS::SQL2XLS($db, $requete, 'toto.xlsx'); - - - - - - - diff --git a/SQL2XLS.pm b/SQL2XLS.pm index 42bc1b6..4c0c41c 100644 --- a/SQL2XLS.pm +++ b/SQL2XLS.pm @@ -13,7 +13,7 @@ package SQL2XLS; # ORGANIZATION: non # VERSION: 1.0 # CREATED: 28/03/17 -# CHANGE_LOG: +# CHANGE_LOG: # #=============================================================================== use Carp; @@ -32,13 +32,13 @@ use subs qw(SQL2XLS); #------------------------------------------------------------------------------- # SUB SQL2XLS # arguments : \%db \%requete, $filename -#------------------------------------------------------------------------------- +#------------------------------------------------------------------------------- sub SQL2XLS{ my $ref_db = shift; my $ref_requete = shift; my $filename = shift; - + # connexion base say "dbi:Pg:dbname=${$ref_db}{dbname};host=${$ref_db}{host};port=${$ref_db}{port}"; my $dbh = DBI->connect("dbi:Pg:dbname=$ref_db->{dbname};host=$ref_db->{host};port=$ref_db->{port}", @@ -46,24 +46,24 @@ sub SQL2XLS{ $ref_db->{password}, {AutoCommit => 1, RaiseError => 1, PrintError => 0} ); - + #création du fichier Excel my $wb = Excel::Writer::XLSX->new($filename); $wb->set_properties( title => 'SQL2XLS', author => 'C.GRUSZKA', ); - + #on boucle pour créer tous les onglets du workbook - foreach my $tab (keys %{$ref_requete}){ + foreach my $tab (keys %{$ref_requete}){ #création de l'onglet say "\n\nOnglet $tab"; my $ws = $wb->add_worksheet($tab); $ws->set_zoom(85); - + #récupèration de la requête SQL associée à l'onglet - my $sql = $ref_requete->{$tab}; - + my $sql = $ref_requete->{$tab}; + #requêtage ! my $sth = $dbh->prepare($sql); $sth->execute; @@ -73,15 +73,15 @@ sub SQL2XLS{ say "Nombre de colonnes : $nb_col"; my $nb_items = scalar @$list; say "Nombre de lignes : $nb_items"; - + #construction de l'array de hash des entêtes de colonnes (à tes souhaits) my @arraycol; foreach my $col (@{$colonnes}){ push @arraycol, { header => "$col" }; } - + #table EXCEL $ws->add_table(0,0,$nb_items,$nb_col-1, { data => $list, header_row => 1, autofilter => 1,style => 'Table Style Medium 5', columns => \@arraycol }); } } -1; \ No newline at end of file +1;