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

    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);
            tableOneRow1.addNewTableCell();
            
            CTTbl ctTable = CTTbl.Factory.newInstance();
            XWPFTable table = new XWPFTable(ctTable, document, 0, 0);
            XWPFTableRow tr = table.getRow(0);
            XWPFTableCell cell = tr.getCell(0);
            cell.setText("c");
            
           tableOne.getRow(0).getCell(0).setText("a");
            tableOne.getRow(0).getCell(1).setText("b");
            //tableOne.getRow(0).getCell(0).insertTable(0, document.createTable(1, 1));
            //tableOne.getRow(0).getCell(0).insertTable(0, table);
            tableOne.getRow(0).getCell(0).addParagraph();
            tableOne.getRow(0).getCell(0).getBodyElements().get(0).getBody().insertTable(0, table);
            tableOne.getRow(0).getCell(0).getParagraphs().get(0).createRun();
            tableOne.getRow(0).getCell(0).getParagraphs().get(0).getRuns().get(0).setText("d");
            tableOne.getRow(0).getCell(0).getTables().get(0).getRow(0).getCell(0).setText("c");
            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="http://schemas.openxmlformats.org/wordprocessingml/2006/main">
    <w:body>
        <w:tbl>
            <w:tblPr>
                <w:tblW w:w="0" w:type="auto"/>
                <w:tblBorders>
                    <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"/>
                </w:tblBorders>
            </w:tblPr>
            <w:tr>
                <w:tc>
                    <w:p>
                        <w:r>
                            <w:t>a</w:t>
                        </w:r>
                        <w:r>
                            <w:t>d</w:t>
                        </w:r>
                    </w:p>
                    <w:p/>
                </w:tc>
                <w:tc>
                    <w:p>
                        <w:r>
                            <w:t>b</w:t>
                        </w:r>
                    </w:p>
                </w:tc>
            </w:tr>
        </w:tbl>
    </w:body>
    </w:document>

    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.

    Felix

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

    Default Re: How can I create nestedTables XWPF

    Hello Felix.

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

    Holger

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

    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();
         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;
       }
    }
    and

    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

    Holger

Similar Threads

  1. Replies: 1
    Last Post: 11-26-2012, 10:54 AM
  2. Replies: 10
    Last Post: 09-03-2012, 08: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, 04:06 PM
  4. create id
    By daisy in forum JDBC
    Replies: 15
    Last Post: 03-16-2011, 09:34 AM
  5. Create XML From XSD
    By Jack in forum XML
    Replies: 1
    Last Post: 07-09-2007, 12: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
  •