apache poi - How to insert a table into a cell in poi word? -
i want insert table cell,i did demo failure, can give advice?
xwpfdocument document = new xwpfdocument(); xwpftable tabletwo = document.createtable(1,1); xwpftablerow tabletworow1 = tabletwo.getrow(0); tabletworow1.getcell(0).settext("aaaaaaaaaa"); xwpftable tableone = document.createtable(2,2); xwpftablerow tableonerow1 = tableone.getrow(0); xwpftablerow tableonerow2 = tableone.getrow(1); tableonerow1.getcell(0).settext("test"); tableonerow1.getcell(1).settext("test"); tableonerow2.getcell(0).settext("test"); tableonerow2.getcell(1).inserttable(0, tabletwo); fileoutputstream fos = new fileoutputstream("d:\\2.docx"); document.write(fos); fos.close();
the xwpftable
created using xwpfdocument.createtable
belongs document. cannot later taken document , putted cell.
for doing this, xwpftablecell.insertnewtbl needed.
the following code works using actual apache poi
versions:
import java.io.*; import org.apache.poi.xwpf.usermodel.*; public class createwordtableintable { public static void main(string[] args) throws exception { xwpfdocument document = new xwpfdocument(); xwpftable tableone = document.createtable(2,2); xwpftablerow tablerow = tableone.getrow(0); tablerow.getcell(0).settext("test"); tablerow.getcell(1).settext("test"); tablerow = tableone.getrow(1); tablerow.getcell(0).settext("test"); xwpfparagraph paragraph = tablerow.getcell(1).getparagrapharray(0); xwpftable tabletwo = tablerow.getcell(1).insertnewtbl(paragraph.getctp().newcursor()); tabletwo.getcttbl().addnewtblpr().addnewtblborders().addnewleft().setval( org.openxmlformats.schemas.wordprocessingml.x2006.main.stborder.single); tabletwo.getcttbl().gettblpr().gettblborders().addnewright().setval( org.openxmlformats.schemas.wordprocessingml.x2006.main.stborder.single); tabletwo.getcttbl().gettblpr().gettblborders().addnewtop().setval( org.openxmlformats.schemas.wordprocessingml.x2006.main.stborder.single); tabletwo.getcttbl().gettblpr().gettblborders().addnewbottom().setval( org.openxmlformats.schemas.wordprocessingml.x2006.main.stborder.single); tabletwo.getcttbl().gettblpr().gettblborders().addnewinsideh().setval( org.openxmlformats.schemas.wordprocessingml.x2006.main.stborder.single); tabletwo.getcttbl().gettblpr().gettblborders().addnewinsidev().setval( org.openxmlformats.schemas.wordprocessingml.x2006.main.stborder.single); tablerow = tabletwo.createrow(); tablerow.createcell().settext("aaaaaaaaaa"); tablerow.createcell().settext("jjjjjjjj"); tablerow = tabletwo.createrow(); tablerow.getcell(0).settext("bbbbbbbbbb"); tablerow.getcell(1).settext("gggggggggg"); document.write(new fileoutputstream("createwordtableintable.docx")); document.close(); } }
but recommend not use approach use merged cells instead. tables contained in table cells ugly. true html
word
other text document formats can contain tables.
Comments
Post a Comment