Reputation: 498
I want to insert a table into a cell,I did a demo but failure, can anyone give some advice?
XWPFDocument document = new XWPFDocument();
XWPFTable tableTwo = document.createTable(1,1);
XWPFTableRow tableTwoRow1 = tableTwo.getRow(0);
tableTwoRow1.getCell(0).setText("aaaaaaaaaa");
XWPFTable tableOne = document.createTable(2,2);
XWPFTableRow tableOneRow1 = tableOne.getRow(0);
XWPFTableRow tableOneRow2 = tableOne.getRow(1);
tableOneRow1.getCell(0).setText("Test");
tableOneRow1.getCell(1).setText("Test");
tableOneRow2.getCell(0).setText("Test");
tableOneRow2.getCell(1).insertTable(0, tableTwo);
FileOutputStream fos = new FileOutputStream("D:\\2.docx");
document.write(fos);
fos.close();
Upvotes: 1
Views: 4867
Reputation: 39
To add a table into a cell, one can use XWPFTableCell's method:
public XWPFTable insertNewTbl(XmlCursor cursor)
so
XWPFTable table = cell.insertNewTbl(cell.getParagraphArray(cell.getParagraphs().size() - 1));
If u want to add the inner table into a paragraph that is not the first paragraph of a cell, you have to add a paragraph to the cell (obviously). In the newer versions of POI (tested on 5.2.3) you can just use the addParagraph() method of XWPFTableCell, but keep in mind that this method is flawed on some prior versions. For example this method on 5.2.3 updates the list of IBodyElements in the document:
public void addParagraph(XWPFParagraph p) {
this.paragraphs.add(p);
this.bodyElements.add(p);
}
but in the prior versions (4.1.2)
public void addParagraph(XWPFParagraph p) {
this.paragraphs.add(p);
}
so, you have to use the XMLCursor for adding a paragraph, because the insertNewParagraph method updates the IBodyElemets list:
void addParagraphToCellUsingCursor(XWPFTableCell cell) {
XmlCursor cursor = getCursorAfterParagraph(cell.getParagraphArray(cell.getParagraphs().size() - 1));
XWPFParagraph cellParagraph = cell.insertNewParagraph(cursor);
}
public static XmlCursor getCursorAfterParagraph(XWPFParagraph previous) {
XmlCursor cursor = previous.getCTP().newCursor();
cursor.toEndToken();
cursor.toNextToken();
return cursor;
}
Upvotes: 0
Reputation: 61852
The XWPFTable
created using XWPFDocument.createTable
will always belongs to the document. So it cannot later be taken from the document and putted into a cell.
For doing this, XWPFTableCell.insertNewTbl is needed.
The following code works using actual apache poi
versions:
import java.io.*;
import org.apache.poi.xwpf.usermodel.*;
public class CreateWordTableInTable {
public static void main(String[] args) throws Exception {
XWPFDocument document = new XWPFDocument();
XWPFTable tableOne = document.createTable(2,2);
XWPFTableRow tablerow = tableOne.getRow(0);
tablerow.getCell(0).setText("Test");
tablerow.getCell(1).setText("Test");
tablerow = tableOne.getRow(1);
tablerow.getCell(0).setText("Test");
XWPFParagraph paragraph = tablerow.getCell(1).getParagraphArray(0);
XWPFTable tableTwo = tablerow.getCell(1).insertNewTbl(paragraph.getCTP().newCursor());
tableTwo.getCTTbl().addNewTblPr().addNewTblBorders().addNewLeft().setVal(
org.openxmlformats.schemas.wordprocessingml.x2006.main.STBorder.SINGLE);
tableTwo.getCTTbl().getTblPr().getTblBorders().addNewRight().setVal(
org.openxmlformats.schemas.wordprocessingml.x2006.main.STBorder.SINGLE);
tableTwo.getCTTbl().getTblPr().getTblBorders().addNewTop().setVal(
org.openxmlformats.schemas.wordprocessingml.x2006.main.STBorder.SINGLE);
tableTwo.getCTTbl().getTblPr().getTblBorders().addNewBottom().setVal(
org.openxmlformats.schemas.wordprocessingml.x2006.main.STBorder.SINGLE);
tableTwo.getCTTbl().getTblPr().getTblBorders().addNewInsideH().setVal(
org.openxmlformats.schemas.wordprocessingml.x2006.main.STBorder.SINGLE);
tableTwo.getCTTbl().getTblPr().getTblBorders().addNewInsideV().setVal(
org.openxmlformats.schemas.wordprocessingml.x2006.main.STBorder.SINGLE);
tablerow = tableTwo.createRow();
tablerow.createCell().setText("aaaaaaaaaa");
tablerow.createCell().setText("jjjjjjjj");
tablerow = tableTwo.createRow();
tablerow.getCell(0).setText("bbbbbbbbbb");
tablerow.getCell(1).setText("gggggggggg");
document.write(new FileOutputStream("CreateWordTableInTable.docx"));
document.close();
}
}
But I would strongly recommend not to use this approach but to use merged cells instead. Tables which are contained in table cells are ugly. This is true for HTML
as well as for Word
as well as for all other text document formats which can contain tables.
Upvotes: 4