You're right, I thought the codes just replace the content of the new table to 
be the old one, but actually it turns out  they refer to the same table unless 
I save and reopen the document. However, I can't just re-creating a new table 
and fill it because the table I need has a complex style, so copying a existing 
one seems to be the best way. Finally I made it by construct a table using the 
template table's "CTTbl" before using setTable() to avoid referring to the same 
table . Look at the piece of the codes,


        public static void copyTable() throws Exception {
                XWPFDocument doc = new XWPFDocument(new FileInputStream(
                                "styledTable.docx"));
                XWPFTable table = doc.getTables().get(0);


                 // Copying a existing table
                 CTTbl ctTbl = CTTbl.Factory.newInstance(); // Create a new 
CTTbl for the new table
                 ctTbl.set(table.getCTTbl()); // Copy the template table's CTTbl
                 XWPFTable table2 = new XWPFTable(ctTbl, doc); // Create a new 
table using the CTTbl upon
                 table2.getRow(5).getCell(0).setText("123"); // Modify the new 
table
                 doc.createParagraph();
                 doc.createTable(); // Create a empty table in the document
                 doc.setTable(1, table2);  // Replace the empty table to table2


                  // Copying a existing table row‍
                 CTRow ctRow = CTRow.Factory.newInstance();
                 XWPFTableRow row = table.getRow(5);
                 ctRow.set(row.getCtRow());
                 XWPFTableRow row2 = new XWPFTableRow(ctRow, table);
                 row2.getCell(0).setText("123");
                 table.addRow(row2);


                FileOutputStream out = new 
FileOutputStream("styledTable2.docx");
                doc.write(out);
                out.close();
        }‍



By this way I could correctly copy a existing table and modify the two tables 
independently as well as the table row. Maybe there's a better way to solve 
this problem, anyway, it does works.


------------------ 原始邮件 ------------------
发件人: "Aram Mirzadeh";<[email protected]>;
发送时间: 2014年9月7日(星期天) 上午9:05
收件人: "POI Users List"<[email protected]>; 

主题: Re: How to copy a table and modify it correctly in the same docx fileusing 
POI?



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 !

Reply via email to