Quantcast
Channel: Java Programming Forum - Learn Java Programming - Apache POI
Viewing all articles
Browse latest Browse all 120

How can I create nestedTables XWPF

$
0
0
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.

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.

documentStructure.PNG


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

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.

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
Attached Images

Viewing all articles
Browse latest Browse all 120

Trending Articles