Bonjour à tous,
J'initialise un tableau avec une boucle for depuis un classeur Excel dans mon contrôleur que j'envoie en retour dans mon twig.
Ok tout marche, mais quand je veux afficher mon tableau j'ai automatiquement l'indice en première colonne. Quelle syntaxe utiliser pour n'avoir que mes données ?
Le code du contrôleur:
Le code de ma vue twig
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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65 public function lireFeuilleAction($feuillA = 0, $feuillB = 0) { $em1 = $this->container->get('doctrine')->getEntityManager(); $fichA = "C:\Documents and Settings\bzn\Bureau\PHPExcel-Tests\FichierA.xls"; $fichB = "C:\Documents and Settings\bzn\Bureau\PHPExcel-Tests\FichierB.xls"; //Création de l'objet reader $xls_service = $this->container->get('xls.load_xls5'); $excelObj_A = $xls_service->load($fichA); $excelObj_B = $xls_service->load($fichB); //Récupération des dernières lignes et colonnes du classeur A $objWorksheet_A = $excelObj_A->getSheet($feuillA); $highestRow_A = $objWorksheet_A ->getHighestRow(); $highestColumn_A = $objWorksheet_A ->getHighestColumn(); //Récupération des dernières lignes et colonnes du classeur B $objWorksheet_B = $excelObj_B->getSheet($feuillB); $highestRow_B = $objWorksheet_B ->getHighestRow(); $highestColumn_B = $objWorksheet_B ->getHighestColumn(); //Création de l'objet writer $obj2 = new \PHPExcel_Writer_Excel5($excelObj_B); $colA = 1; $colB = 1; $colAsort = $colA + 2; $compt = 0; for ($rowA = 3; $rowA <= $highestRow_A; $rowA++) { $valA = $objWorksheet_A->getCellByColumnAndRow($colA, $rowA)->getValue(); for ($rowB = 3; $rowB <= $highestRow_B; $rowB++) { $valB = $objWorksheet_B->getCellByColumnAndRow($colB, $rowB)->getValue(); if($valA == $valB) { $valAsort = $objWorksheet_A->getCellByColumnAndRow($colAsort, $rowA)->getValue(); $compt++;// $compt =+1; $tab[] = array($rowB,3,$valAsort); //J'écris dans le classeur Excel $excelObj_B->setActiveSheetIndex($feuillB); $excelObj_B->getActiveSheet()->setCellValueByColumnAndRow(3, $rowB, $valAsort); } } } //Test d'écriture dans le classeur Excel //$excelObj_B->setActiveSheetIndex(0)->setCellValueByColumnAndRow(0, 1, $valA); //Enregistrement du fichier Excel $obj2->save($fichB); //Retour du tableau return $this->container->get('templating')->renderResponse('MyAppFilmothequeBundle:Excel:lireFeuille.html.twig.twig', array( 'tab' => $tab )); }
Résultat obtenu:
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10 <table border=1> {% for a, b in tab %} <tr> <td>{{ a }}</td> {% for strb in b %} <td>{{ strb }}</td> {% endfor %} </tr> {% endfor %} </table>
0 15 3 AAA
1 3 3 BBB
2 10 3 CCC
3 9 3 DDD
Ce que j'aurais aimé avoir:
15 3 AAA
3 3 BBB
10 3 CCC
9 3 DDD
Merci les gars et @ +++
Kris63
Partager