Merge branch 'excelwriter' of grouch/SQL2XLS into master

This commit is contained in:
djez smith 2017-03-28 20:24:27 +02:00 committed by Gogs
commit 33d50ba72d
2 changed files with 96 additions and 111 deletions

View File

@ -1,19 +1,19 @@
#!/usr/bin/env perl #!/usr/bin/env perl
#=============================================================================== #===============================================================================
# #
# FILE: SQL2XLS.pl # 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: --- # OPTIONS: ---
# REQUIREMENTS: --- # REQUIREMENTS: ---
# BUGS: --- # BUGS: ---
# NOTES: --- # NOTES: ---
# AUTHOR: grouch' # AUTHOR: grouch'
# ORGANIZATION: # ORGANIZATION:
# VERSION: 1.0 # VERSION: 1.0
# CREATED: 27/03/17 # CREATED: 27/03/17
# CHANGE_LOG: # CHANGE_LOG:
# #
#=============================================================================== #===============================================================================
@ -28,58 +28,55 @@ use Spreadsheet::WriteExcel;
#------------------------------------------------------------------------------- #-------------------------------------------------------------------------------
# DATABASE # DATABASE
#------------------------------------------------------------------------------- #-------------------------------------------------------------------------------
my $user = 'postgres'; my $db = {
my $password = 'postgres'; user => 'postgres'
my $host = 'localhost'; ,password => 'postgres'
my $port = '5432'; ,host => 'localhost'
my $dbname = 'TRIDIM_TEST01'; ,port => '5432'
,dbname => 'TRIDIM_TEST01'
};
#------------------------------------------------------------------------------- #-------------------------------------------------------------------------------
# Requête SQL # Requête SQL
#------------------------------------------------------------------------------- #-------------------------------------------------------------------------------
my $prog = 'ATO053'; my $prog = 'ATO045';
my $datedebut = '20000101000000'; my $datedebut = '20000101000000';
#my $datedebut = '20170201000000'; #my $datedebut = '20170201000000';
my $datefin = '20191231235959'; my $datefin = '20191231235959';
# une première requête
my $sql1 = qq{SELECT
MPOS
,NOM
,MINI
,MAXI
,MES_BAR_BAR
,Pp
,Ppk
,NbPieces
,nb_total_occurences
my $sql1 = qq{SELECT ,CRITICAL
MPOS ,MAJOR
,NOM ,ICD
,MINI ,COMMENTAIRE_DCM
,MAXI
,MES_BAR_BAR
,Pp FROM calcul_capa($datedebut, $datefin, '$prog')
,Ppk
,NbPieces WHERE MINI notNULL AND MAXI notNULL
,nb_total_occurences --AND Pp > 1.33 AND Ppk > 1
};
,CRITICAL
,MAJOR #une deuxième requête
,ICD my $sql2 = qq{SELECT * FROM DATA WHERE PROG = '$prog'};
,COMMENTAIRE_DCM
#la sub prend une ref de hash en argument
# nom_de_l'onglet => requete_sql
FROM calcul_capa($datedebut, $datefin, '$prog') my $requete = {"capa" => $sql1, "data" => $sql2};
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);
#------------------------------------------------------------------------------- #-------------------------------------------------------------------------------
# APPEL SUB # APPEL SUB
#------------------------------------------------------------------------------- #-------------------------------------------------------------------------------
SQL2XLS::SQL2XLS($user, $password, $host, $port, $dbname, \%requete, 'toto.xls');
SQL2XLS::SQL2XLS($db, $requete, 'toto.xlsx');

View File

@ -1,4 +1,4 @@
#!/usr/bin/env perl package SQL2XLS;
#=============================================================================== #===============================================================================
# #
# FILE: SQL2XLS.pm # FILE: SQL2XLS.pm
@ -10,90 +10,78 @@
# BUGS: --- # BUGS: ---
# NOTES: --- # NOTES: ---
# AUTHOR: grouch' # AUTHOR: grouch'
# ORGANIZATION: # ORGANIZATION: non
# VERSION: 1.0 # VERSION: 1.0
# CREATED: 28/03/17 # CREATED: 28/03/17
# CHANGE_LOG: # CHANGE_LOG:
# #
#=============================================================================== #===============================================================================
package SQL2XLS;
use Carp; use Carp;
use DBI; use DBI;
use Modern::Perl; use Modern::Perl;
use Spreadsheet::WriteExcel; use Excel::Writer::XLSX;
use Exporter; use Exporter;
use Data::Dumper;
#trucs que je comprends pas tout
our @ISA = qw( Exporter ); our @ISA = qw( Exporter );
our @EXPORT = (); our @EXPORT = ();
our @EXPORT_OK = qw(SQL2XLS); our @EXPORT_OK = qw(SQL2XLS);
use subs qw(SQL2XLS); use subs qw(SQL2XLS);
#------------------------------------------------------------------------------- #-------------------------------------------------------------------------------
# SUB SQL2XLS # SUB SQL2XLS
# Arguments $user, $password, $host, $port, $dbname, \%requete, $filename # arguments : \%db \%requete, $filename
#------------------------------------------------------------------------------- #-------------------------------------------------------------------------------
sub SQL2XLS{ sub SQL2XLS{
my $ref_db = shift;
my $ref_requete = shift;
my $filename = shift;
my $user = shift; # connexion base
my $password = shift; say "dbi:Pg:dbname=${$ref_db}{dbname};host=${$ref_db}{host};port=${$ref_db}{port}";
my $host = shift; my $dbh = DBI->connect("dbi:Pg:dbname=$ref_db->{dbname};host=$ref_db->{host};port=$ref_db->{port}",
my $port = shift; $ref_db->{user},
my $dbname = shift; $ref_db->{password},
my $ref_requete = shift; {AutoCommit => 1, RaiseError => 1, PrintError => 0}
my $filename = shift; );
my $tab = shift;
# connexion base #création du fichier Excel
my $dbh = DBI->connect("dbi:Pg:dbname=$dbname;host=$host;port=$port", my $wb = Excel::Writer::XLSX->new($filename);
$user,
$password,
{AutoCommit => 1, RaiseError => 1, PrintError => 0}
);
my $wb = Spreadsheet::WriteExcel->new($filename);
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
$wb->set_properties( $wb->set_properties(
title => 'SQL2XLS', title => 'SQL2XLS',
author => 'C.GRUSZKA', author => 'C.GRUSZKA',
comments => $sql, );
);
#on boucle pour créer tous les onglets du workbook
my $ws = $wb->add_worksheet($tab); foreach my $tab (keys %{$ref_requete}){
#création de l'onglet
# entêtes de colonnes say "\n\nOnglet $tab";
my $col = 0; my $ws = $wb->add_worksheet($tab);
foreach my $token (@{$colonnes}) { $ws->set_zoom(85);
$ws->write(0, $col, $token);
$col++; #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 #table EXCEL
my $row = 0; $ws->add_table(0,0,$nb_items,$nb_col-1, { data => $list, header_row => 1, autofilter => 1,style => 'Table Style Medium 5', columns => \@arraycol });
foreach my $token (@{$list}) {
my $col = 0;
foreach my $token (@{$list}[$row]) {
$ws->write($row+1, $col, $token);
$col++;
}
$row++;
} }
} }
} 1;
1;