XWPF: Ajouter un tableau dans une cellule
Bonjour à tous,
je pars d'un modèle de document que je dois remplir. la partie Abstract du document est un tableau d'une seule cellule. J'y peux rien, mon modèle est comme ca.
Dans cet abstract, je souhaite y insérer un tableau , mais la je butte et je ne trouve rien sur internet à ce sujet
j'ai essayé cette solution
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
| private void createAbstractPartOfDocument() {
XWPFTableCell cell = this.getAbstractConclusionCell();
XmlCursor cursor = cell.getCTTc().newCursor();
cursor.selectPath("./*");
cursor.toNextSelection();
XWPFParagraph newParagraph = cell.insertNewParagraph(cursor);
newParagraph.setAlignment(ParagraphAlignment.CENTER);
XWPFRun run = newParagraph.createRun();
run.setText("Résumé des résultats");
run.addCarriageReturn();
run.setBold(true);
run.setFontSize(12);
/* Creation d'une table dans la cellule */
XWPFTable table = document.createTable(4,4);
cell.getTables().add(table);
cell.insertTable(1, table);
} |
mais j'obtiens ceci sur la ligne cell.insertTable(1, table);
Code:
1 2 3
|
Exception in thread "main" java.lang.UnsupportedOperationException
at java.util.Collections$UnmodifiableCollection.add(Unknown Source) |
j'ai essayé aussi ceci
Code:
1 2 3 4
|
/* Creation d'une table dans la cellule */
cursor.toNextSelection();
XWPFTable table = cell.insertNewTbl(cursor); |
mais la fonction insertnewTable me renvoie toujours null
Comment doit on faire
Merci
Cordialement
1 pièce(s) jointe(s)
[POI] XWPF: Ajouter un tableau dans une cellule
Bonjour à tous,
j'ai continuer a chercher une solution, et j'ai pu enfin insérer un tableau dans une cellule d'un tableau.
j'ai d'abord créer cette classe
Code:
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
|
import java.math.BigInteger;
import java.util.List;
import org.apache.poi.xwpf.usermodel.XWPFTable;
import org.apache.poi.xwpf.usermodel.XWPFTableCell;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTP;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTRow;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTString;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTTbl;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTTblGrid;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTTblPr;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTTblWidth;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTTc;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.STTblWidth;
public class XWPFExtendedTable {
protected CTTbl ctTbl;
protected int nbRows;
public XWPFExtendedTable(XWPFTableCell cell) {
ctTbl = cell.getCTTc().addNewTbl();
/* definition du style du tableau */
CTTblPr tblPr = ctTbl.addNewTblPr();
CTString style = tblPr.addNewTblStyle();
style.setVal("Grilledutableau");
CTTblWidth tblW = tblPr.addNewTblW();
tblW.setW(new BigInteger("0"));
tblW.setType(STTblWidth.AUTO);
/* Creation d'un paragraph (Obligatoire pour éviter les erreur de lecture) */
cell.getCTTc().addNewP();
}
public void setColumnDefinition(int[] width) {
nbRows = width.length;
CTTblGrid tblGrig = ctTbl.addNewTblGrid();
CTRow tr = ctTbl.addNewTr();
String rsiRid = "00020A65";
tr.setRsidR(rsiRid.getBytes());
for (int i=0; i<nbRows ; i++) {
tblGrig.addNewGridCol().setW(new BigInteger(""+width[i]));
CTTc tc = tr.addNewTc();
tc.addNewP();
}
}
public int getNbRows() {
return nbRows;
}
public CTTbl getCTTbl() {
return ctTbl;
}
} |
et pour l'utilisation je fais ceci
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14
|
XWPFTableCell cell = this.getAbstractConclusionCell();
.....
XWPFExtendedTable table = new XWPFExtendedTable(cell);
/* 4 colonnes dans mon tableau */
table.setColumnDefinition(new int[] {2500,2500,2500,2500});
XWPFTable t = new XWPFTable(table.getCTTbl(),cell);
XWPFTableRow row = t.getRow(0);
row.getCell(0).setText("Ma cellule 0");
row.getCell(1).setText("Ma cellule 1");
row.getCell(2).setText("Ma cellule 2");
row.getCell(3).setText("Ma cellule 3"); |
:yaisse2::yaisse2:
j'espère que cela pourra resservir.
Cordialement