You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@poi.apache.org by massimo malvestio <ma...@gmail.com> on 2015/11/20 12:44:12 UTC

XWPFTable insertNewTableRow trouble

Hi everybody!
I'm trying to manipulate a docx using apache poi, but I got a problem with
the library.

Versions of apache poi tested: 3.13, 3.7
Word: 2013

I need to add a variable number of rows into a table and I use
insertNewTableRow, here the snippet of code I wrote to test the
functionality

//template
InputStream templateStream =
getClass().getResourceAsStream("./templates/test.docx");
XWPFDocument template = new XWPFDocument(OPCPackage.open(templateStream));
XWPFTable table = template.getTables().get(0);
//0 header, 1 empty existing row
table.insertNewTableRow(2);

File destFile = File.createTempFile("docx_test", ".docx");
BufferedOutputStream outputStream = new BufferedOutputStream(new
FileOutputStream(destFile));
template.write(outputStream);
outputStream.flush();
outputStream.close();

The docx is correctly generated, but when I try to open it, Word claims
it's not possible, a problem with its content has been found.
If I click on details section, an unspecified error is shown, related to
document.xml.

If I use createRow() everything goes fine, but I cannot use it because I
can need to insert a row between two already existing.

I tried to use addNewRowBetween(), but I found  is an empty method,
watching the source code.

I tried  createRow() then inserting the returned object in the correct
position, but doing so rows are duplicated. (Yes, I tried to use
removeRow() giving the position of the last row but it didn't work)

I don't know if I can attach docx, anyway the structure of the document is
quite simple, a table with header and one row.

Thanks people in advance for the support