Because they're passed by reference not by content. You're better off just re-creating the table as needed than copying an existing one.
On Sat, Sep 6, 2014 at 12:28 PM, 尘 <[email protected]> wrote: > Hi, everybody, > I need to generate a complex docx file which mainly contains many > tables. I try to open a template docx file and copy a empty template table > in it, then fill the new table with texts. But it seems that there isn't a > method such as "copyTable()" in XWPFDocument, so I have to create a empty > table then use setTable() to set the new one as the template table. By this > way I can get a new table same as the template table, but when I try to > edit it, I find that the result is very strange, because either > getTables.get(0) or getTables.get(1) will refer to the first table, not the > second one. Here are my codes, > > > public static void copyTable() throws Exception { > XWPFDocument doc = new XWPFDocument(new FileInputStream( > "styledTable.docx")); > > XWPFTable table = doc.getTables().get(0); > doc.createTable(); > doc.createParagraph(); > doc.setTable(1, table); > // table = doc.getTables().get(0); > table = doc.getTables().get(1); > table.getRows().get(0).getCell(0).setText("123"); > // The 1st table always be modified, why ? > > FileOutputStream out = new > FileOutputStream("styledTable2.docx"); > doc.write(out); > out.close(); > } > > > > The same problem happens when I try to copy and modify a table row by > addRow(XWPFTableRow row). If anyone get the correct way to copy a table and > a table row, please write me back here, thanks a lot !
