Bonjour,
J'ai une page de statistiques et j'aimerai pouvoir exporter le tableau sous format excel.
J'utilise PHPEXCEL et ça fonctionne plutot bien.
Sauf sur cette statistique car elle est un peu spéciale.
Voici le code qui m'affiche mon tableau sous format HTML
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13 Une parti du formulaire: <SELECT id="" name="annee4"> <OPTION value="0"></OPTION> $req = Cnx::connectCnx()->prepare("SELECT DISTINCT YEAR( date_vente ) an_vente FROM contacts WHERE YEAR(date_vente)<>0000-00-00 ORDER BY an_vente DESC"); $req->execute(array('id' => $Auth->user('id'))); while($user=$req->fetch(PDO::FETCH_OBJ)) { echo "<OPTION value=".$user->an_vente.">".$user->an_vente."</OPTION>"; $annee_min4 = $user->an_vente; } </SELECT>Jusque là aucun soucis, ça fonctionne parfaitement.
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47 Le traitement <?php $now = date('Y'); //année actuelle $annees = range($annee_min4, $now );//génère les années comprises entre l'année la plus petite enregistrée en BDD et l'année actuelle $_SESSION['annees'] = range($annee_min4, $now );//idem mais en session pour PHPEXCEL $total_par_an=''; foreach ( $annees as $annee ) { $total_par_an .=",SUM(CASE WHEN YEAR(date_vente) = '".$annee."' THEN ttc/(1+(tva/100)) ELSE 0 END) '".$annee."' "; } $req1 = Cnx::connectCnx()->prepare("SELECT c.prod_ref, p.categorie_prod ".$total_par_an ." FROM contacts c INNER JOIN produits p ON c.prod_ref=p.ref_prod AND c.prod_ref <> '' AND YEAR(date_vente) NOT LIKE '0' GROUP BY p.categorie_prod ORDER BY YEAR(date_vente)"); if ( $req1->execute(array(':annee' => $_SESSION['annee4'], ':commercial' => $_SESSION['commercial4'])) ) { $ventes = $req1->fetchAll(PDO::FETCH_ASSOC); ?> <table class="tabl_graph"> <thead> <tr> <th></th> <?php foreach ( $annees as $annee ) { echo "<th>".$annee."</th>";}?> </tr> </thead> <tbody> <?php foreach ( $ventes as $annee_v ){ ?> <tr> <th><?php echo $annee_v['categorie_prod']; ?></th> <?php foreach ( $annees as $annee ) { if(number_format($annee_v[$annee], 2, ',', ' ') == 0.00) { $resultat4 = '-'; } else { $resultat4 = number_format($annee_v[$annee], 2, ',', ' '); } echo '<td>'.$resultat4.'</td>'."\n"; } ?> </tr> <?php } ?> </tbody> </table> <?php } ?>
Maintenant, pour l'exporter en format excel, voici ce que j'ai
Le code tel que vous le voyez fonctionne très bien, mais je voudrai que l'affichage des colonnes "Année" s'affiche dynamiquement en fonction des années enregistrées en BDD.(a la place de 2012, 2013, 2014... il y ai quelquechose comme $annee)
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35 <?php $workbook = new PHPExcel; $sheet = $workbook->getActiveSheet(); $query = Graph::connectGraph()->query("SELECT c.prod_ref, p.categorie_prod ".$total_par_an ." FROM contacts c INNER JOIN produits p ON c.prod_ref=p.ref_prod AND c.prod_ref <> '' AND YEAR(date_vente) NOT LIKE '0' GROUP BY p.categorie_prod ORDER BY YEAR(date_vente)"); $nbre=$query->ColumnCount(); for($i=0;$i<$nbre;$i++){//design entete dans excel $j=$i; $sheet->getStyleByColumnAndRow($j++,1)->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER); } for($i=0;$i<=23;$i++){//design entete dans excel $styleA = $sheet->getStyleByColumnAndRow($i,1); $styleA->applyFromArray(array( 'font'=>array( 'bold'=>true), )); } $sheet->setCellValueByColumnAndRow(0,1,'Produit' ); $sheet->setCellValueByColumnAndRow(1,1,'2013' );// A AFFICHER DYNAMIQUEMENT $sheet->setCellValueByColumnAndRow(2,1,'2014' );// A AFFICHER DYNAMIQUEMENT $ligne = 2; while($data = $query->fetch()){ $colonne=0; $sheet->setCellValueByColumnAndRow($colonne, $ligne, $data['categorie_prod']);$colonne++; $sheet->setCellValueByColumnAndRow($colonne, $ligne, $data['2013']);$colonne++;// A AFFICHER DYNAMIQUEMENT $sheet->setCellValueByColumnAndRow($colonne, $ligne, $data['2014']);$colonne++;// A AFFICHER DYNAMIQUEMENT $ligne++;//ligne suivante }
Une idée svp?
Partager