You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@poi.apache.org by 尘 <44...@qq.com> on 2014/09/06 18:28:54 UTC

How to copy a table and modify it correctly in the same docx file using POI?

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 !

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

Posted by "massimo.malvestio" <ma...@gmail.com>.
Hi, usign apache-poi-3.7 I think I found a shorter way to do it

/CTTbl tbl = template.getDocument().getBody().insertNewTbl(tableIndex);

tbl.set(original.getCTTbl());

XWPFTable table2 = new XWPFTable(tbl, template);

table2.getRows().get(0).getCell(0).setText("test");/

where

*template*: XWPFDocument
*original*: original table to be duplicated
*tableIndex*: no javadoc found, I suppose is the position of the table, if i
put 0, the "position" of the original table, the duplicated one is created
but completely attached to the original





--
View this message in context: http://apache-poi.1045710.n5.nabble.com/How-to-copy-a-table-and-modify-it-correctly-in-the-same-docx-file-using-POI-tp5716551p5725580.html
Sent from the POI - User mailing list archive at Nabble.com.

---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@poi.apache.org
For additional commands, e-mail: user-help@poi.apache.org


回复: How to copy a table and modify it correctly in the same docx fileusing POI?

Posted by 尘 <44...@qq.com>.
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";<aw...@mbcli.com>;
发送时间: 2014年9月7日(星期天) 上午9:05
收件人: "POI Users List"<us...@poi.apache.org>; 

主题: 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, 尘 <44...@qq.com> 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 !

Re: How to copy a table and modify it correctly in the same docx file using POI?

Posted by Aram Mirzadeh <aw...@mbcli.com>.
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, 尘 <44...@qq.com> 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 !