Apache POI: modify row after it was already added to the table

651 Views Asked by At

Consider .docx with 1 row, 1 column table. I want to copy the row, and fill the copied row with some text.

The following works fine:

XWPFDocument document = new XWPFDocument(fis);
XWPFTable table = document.getTables().get(0);
XWPFTableRow templateRow = table.getRow(0);
XWPFTableRow copiedTemplateRow = new XWPFTableRow((CTRow) templateRow.getCtRow().copy(), table);
copiedTemplateRow.getTableCells().get(0).setText("SOME TEXT");
table.addRow(copiedTemplateRow); 

However, I'd prefer to first addRow, and then modify it, like so:

table.addRow(copiedTemplateRow);
copiedTemplateRow.getTableCells().get(0).setText("SOME TEXT");

However in this second version, when saving to docx, the row is not modified like I expected.

0

There are 0 best solutions below