Bonjour,
Avant toute chose, je dois dire que c'est mon premier essai de Apache POI et je me suis inspiré de quelques exemples trouvés sur le net.
J'essaie de créer un tableau de deux colonnes. Cela ne marche pas trop mal, mais j'ai un problème:
Lorsque je remplis les cellules en utilisant un paragraphe, le texte va bien dans la cellule désignée, mais est également dupliqué sous le tableau.
Merci d'avance
Gégé
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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95 package test; import gegeutil.Gegetools; import java.io.ByteArrayOutputStream; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import java.io.PrintStream; import java.math.BigInteger; import java.nio.charset.StandardCharsets; import java.util.List; import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTTblLayoutType; import org.openxmlformats.schemas.wordprocessingml.x2006.main.STTblLayoutType; import org.apache.poi.xwpf.usermodel.XWPFDocument; import org.apache.poi.xwpf.usermodel.XWPFParagraph; import org.apache.poi.xwpf.usermodel.XWPFRun; import org.apache.poi.xwpf.usermodel.XWPFTable; import org.apache.poi.xwpf.usermodel.XWPFTableCell; import org.apache.poi.xwpf.usermodel.XWPFTableRow; public class EcritureWORDTable { protected static String répertoire = "D:\\DONNEES\\DEF19\\000-Saisie-DEF19\\"; protected String fichier = ""; public EcritureWORDTable () { super(); créerTable(); } private static XWPFParagraph getTableParagraph(XWPFDocument document, XWPFTableCell cell, String text, boolean bold) { XWPFParagraph paragraph = document.createParagraph(); XWPFRun prun = paragraph.createRun(); prun.setText(text); prun.setFontFamily("Times New Roman"); prun.setFontSize(9); prun.setBold(bold); return paragraph; } private static void créerTable() { XWPFParagraph para = null; try { XWPFDocument document = new XWPFDocument(); FileOutputStream out = new FileOutputStream(new File(répertoire, "fichier Test.docx")); XWPFTable table = document.createTable(); CTTblLayoutType type = table.getCTTbl().getTblPr().addNewTblLayout(); type.setType(STTblLayoutType.FIXED); table.getCTTbl().addNewTblGrid().addNewGridCol().setW(BigInteger.valueOf(2000)); table.getCTTbl().getTblGrid().addNewGridCol().setW(BigInteger.valueOf(7000)); WPFTableRow tableRow = table.getRow(0); para = getTableParagraph(document, tableRow.getCell(0), "Que j'aime a faire connaitre ce nombre utile aux sages, mon cher Archimède", false); tableRow .getCell(0).setParagraph(para); tableRow.addNewTableCell().setText( "Il est 14 heures" ); tableRow = table.createRow(); tableRow.getCell(0).setText("col 1, row 2"); tableRow.getCell(1).setText("col 2, row 2"); document.write(out); document.close(); out.close(); System.out.println("Traitement terminé"); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } catch (Exception e) { e.printStackTrace(); } } } public static void main(String[] args) { new EcritureWORDTable() ; } }
Partager