Merge branch 'hash' of grouch/SQL2XLS into master

This commit is contained in:
djez smith 2017-03-28 19:53:34 +02:00 committed by Gogs
commit 56bd8fb7e3
2 changed files with 116 additions and 63 deletions

View File

@ -20,7 +20,8 @@
use Carp;
use DBI;
use Data::Dumper;
use lib ('Z:/GRUSZKA/SQL2XLS');
use SQL2XLS;
use Modern::Perl;
use Spreadsheet::WriteExcel;
@ -42,18 +43,22 @@ my $datedebut = '20000101000000';
my $datefin = '20191231235959';
my $sql = qq{SELECT
my $sql1 = qq{SELECT
MPOS
,OPr
,NOM
,MINI
,MAXI
,MES_BAR_BAR
,Pp
,Ppk
,NbPieces
,nb_total_occurences
,CRITICAL
,MAJOR
,ICD
,Pp
,Ppk
,COMMENTAIRE_DCM
,MINI
,MAXI
FROM calcul_capa($datedebut, $datefin, '$prog')
@ -62,70 +67,19 @@ my $datefin = '20191231235959';
--LIMIT 12
};
my $sql2 = qq{SELECT * FROM DATA WHERE PROG = '$prog'};
my %requete = ("capa" => $sql1, "data" => $sql2);
#-------------------------------------------------------------------------------
# APPEL SUB
#-------------------------------------------------------------------------------
SQL2XLS($user, $password, $host, $port, $dbname, $sql, 'toto.xls', 'Yo!!!');
SQL2XLS::SQL2XLS($user, $password, $host, $port, $dbname, \%requete, 'toto.xls');
#-------------------------------------------------------------------------------
# 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($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++;
}
# 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++;
}
}

99
SQL2XLS.pm Normal file
View File

@ -0,0 +1,99 @@
#!/usr/bin/env perl
#===============================================================================
#
# FILE: SQL2XLS.pm
#
# DESCRIPTION: truc qui prend une requête SQL et renvoie un tableau Excel
#
# OPTIONS: ---
# REQUIREMENTS: ---
# BUGS: ---
# NOTES: ---
# AUTHOR: grouch'
# ORGANIZATION:
# VERSION: 1.0
# CREATED: 28/03/17
# CHANGE_LOG:
#
#===============================================================================
package SQL2XLS;
use Carp;
use DBI;
use Modern::Perl;
use Spreadsheet::WriteExcel;
use Exporter;
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
#-------------------------------------------------------------------------------
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);
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(
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++;
}
# 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++;
}
}
}
1;