Results 1 to 3 of 3
  1. #1
    fjung is offline Member
    Join Date
    Aug 2015
    Rep Power

    Default How can I create nestedTables XWPF

    Hello, I have some trouble with POI.

    It's not possible for me to create nestedTables. I tested some things but no one get the wished result.

    It's possible for me to load a document with nested tables, manipulate it and save it.
    But I can't create a table with another table inside with poi-methods.

    Currently I used follow code.

    Java Code:
            XWPFDocument document = new XWPFDocument();
            XWPFTable tableOne = document.createTable();
            XWPFTableRow tableOneRow1 = tableOne.getRow(0);
            CTTbl ctTable = CTTbl.Factory.newInstance();
            XWPFTable table = new XWPFTable(ctTable, document, 0, 0);
            XWPFTableRow tr = table.getRow(0);
            XWPFTableCell cell = tr.getCell(0);
            //tableOne.getRow(0).getCell(0).insertTable(0, document.createTable(1, 1));
            //tableOne.getRow(0).getCell(0).insertTable(0, table);
            tableOne.getRow(0).getCell(0).getBodyElements().get(0).getBody().insertTable(0, table);
            return document;
    The Document structure looks right.

    How can I create nestedTables XWPF-documentstructure.png

    But when I save the document then the first cell don't have the table in the xml-structure.

    Java Code:
    <?xml version="1.0" encoding="UTF-8"?>
    <w:document xmlns:w="">
                <w:tblW w:w="0" w:type="auto"/>
                    <w:top w:val="single"/>
                    <w:left w:val="single"/>
                    <w:bottom w:val="single"/>
                    <w:right w:val="single"/>
                    <w:insideH w:val="single"/>
                    <w:insideV w:val="single"/>

    When I use the createTable Method to add a new table to a tableCell, then this method add the table to the
    tablecell and to the document body.

    Java Code:
    tableOne.getRow(0).getCell(0).insertTable(0, document.createTable(1, 1));
    tableOne.getRow(0).getCell(0).insertTable(0, table);
    When I save this document again, the nested table is added append the first table.

    Is there a way to create nested Tables with POI? Or is the a workaround?

    Thanks for any help.


  2. #2
    HolgerN is offline Member
    Join Date
    Nov 2015
    Rep Power

    Default Re: How can I create nestedTables XWPF

    Hello Felix.

    did you solve your probelm I am facing a problem with nested tables too.


  3. #3
    HolgerN is offline Member
    Join Date
    Nov 2015
    Rep Power

    Default Re: How can I create nestedTables XWPF

    I found this in the www and oh wonder it works for me

    Java Code:
    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();
         CTTblWidth tblW = tblPr.addNewTblW();
         tblW.setW(new BigInteger("0"));
         /* Creation d'un paragraph (Obligatoire pour éviter les erreur de lecture) */
       public void setColumnDefinition(int[] width) {
         nbRows = width.length; 
         CTTblGrid tblGrig = ctTbl.addNewTblGrid();
         CTRow tr = ctTbl.addNewTr();
         String rsiRid = "00020A65";
         for (int i=0; i<nbRows ; i++) {
           tblGrig.addNewGridCol().setW(new BigInteger(""+width[i]));
           CTTc tc = tr.addNewTc();
       public int getNbRows() {
         return nbRows;
       public CTTbl getCTTbl() {
        return ctTbl;

    Java Code:
    XWPFTableRow tr = tabel1getRow(1);
    XWPFTableCell = tr.getCell(1);
    XWPFExtendedTable table2 = new XWPFExtendedTable(cell);
    /* 4 colonnes dans mon tableau */
    table2.setColumnDefinition(new int[] {2500,2500,2500,2500});
    XWPFTable t = new XWPFTable(table2.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");
    The output is something like this
    How can I create nestedTables XWPF-auswahl_002.png


Similar Threads

  1. Replies: 1
    Last Post: 11-26-2012, 11:54 AM
  2. Replies: 10
    Last Post: 09-03-2012, 09:13 PM
  3. How to create a JFrame window and create a circle.
    By tehsumo in forum AWT / Swing
    Replies: 3
    Last Post: 02-27-2012, 05:06 PM
  4. create id
    By daisy in forum JDBC
    Replies: 15
    Last Post: 03-16-2011, 10:34 AM
  5. Create XML From XSD
    By Jack in forum XML
    Replies: 1
    Last Post: 07-09-2007, 01:56 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts