如何在XWPFTableCell中插入表

时间:2016-03-15 05:53:49

标签: java apache-poi xwpf

我试图使用Apache poi XWPF来创建word文档。在文档中,我试图在特定的表格单元格中创建多个嵌套表。

我使用我的代码并在一个表格单元格中成功创建了一个表格。但是当我试图在这个特定的单元格中创建更多的表时,我的代码中有一个错误指向" java.lang.IndexOutOfBoundsException:"

        XWPFTableRow tableRowFive = table.createRow();
        cell = tableRowFive.getCell(0);

        paragraph = cell.getParagraphs().get(0);
        XWPFRun run1 = paragraph.createRun();
        run1.setText("Brief facts:");

        XWPFParagraph paragraph2 = cell.addParagraph();
        XWPFRun run2 = paragraph2.createRun();
        XWPFRun run3 = paragraph2.createRun();
        run2.setText("COMPLAINANT");
        run3.setText("test");   
        XWPFParagraph paragraph3 = cell.addParagraph();
        run2 = paragraph3.createRun();
        run3 = paragraph3.createRun();
        run2.setText("tst");
        run3.setText("test2");  

        XmlCursor cursor = cell.getParagraphs().get(0).getCTP().newCursor();
        //add the nested Table1
        XWPFTable nestedTable = cell.insertNewTbl(cursor);
        XmlCursor cursor2 =cell.getParagraphs().get(1).getCTP().newCursor();
        //add the nested Table2
        XWPFTable nestedTable2 = cell.insertNewTbl(cursor2);
        XmlCursor cursor3 =cell.getParagraphs().get(2).getCTP().newCursor();
        //add the nested Table3
        XWPFTable nestedTable3 = cell.insertNewTbl(cursor3);

错误发生在最后一个表创建时。对于嵌套的Table1和Table2,创建是可以的。但最后一个表创建" XWPFTable nestedTable3 = cell.insertNewTbl(cursor3);"报告错误

java.lang.IndexOutOfBoundsException: Index: 5, Size: 3
at java.util.ArrayList.add(ArrayList.java:367)
at org.apache.poi.xwpf.usermodel.XWPFTableCell.insertNewTbl(XWPFTableCell.java:319)
at com.ncs.crimes2.briefingnote.command.BriefingNoteCommand.generateWord(BriefingNoteCommand.java:476)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at com.ncs.iframe.base.ejb.command.BaseCommand.execute(Unknown Source)
at com.ncs.iframe.base.ejb.command.server.CommandServer.executeCommand(Unknown Source)
at com.ncs.iframe.base.ejb.command.CommandExecutor.execute(Unknown Source)
at com.ncs.iframe.base.ejb.command.CommandExecutor.execute(Unknown Source)
at com.ncs.iframe.base.ejb.command.action.BaseCommandAction.executeCommand(Unknown Source)
at com.ncs.crimes2.base.Crimes2Action.executeCommandAction(Crimes2Action.java:177)
at com.ncs.crimes2.base.Crimes2Action.executeCommand(Crimes2Action.java:106)
at com.ncs.iframe.extensions.action.BaseFlowCommandAction.execute(BaseFlowCommandAction.java:260)
at com.ncs.crimes2.base.Crimes2Action.execute(Crimes2Action.java:61)
at org.apache.struts.action.RequestProcessor.processActionPerform(RequestProcessor.java:484)
at org.apache.struts.action.RequestProcessor.process(RequestProcessor.java:274)
at com.ncs.crimes2.base.Crimes2RequestProcessor.process(Crimes2RequestProcessor.java:67)
at org.apache.struts.action.ActionServlet.process(ActionServlet.java:1482)
at org.apache.struts.action.ActionServlet.doPost(ActionServlet.java:525)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:727)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:820)
at weblogic.servlet.internal.StubSecurityHelper$ServletServiceAction.run(StubSecurityHelper.java:227)
at weblogic.servlet.internal.StubSecurityHelper.invokeServlet(StubSecurityHelper.java:125)
at weblogic.servlet.internal.ServletStubImpl.execute(ServletStubImpl.java:300)
at weblogic.servlet.internal.TailFilter.doFilter(TailFilter.java:26)
at weblogic.servlet.internal.FilterChainImpl.doFilter(FilterChainImpl.java:56)
at com.ncs.crimes2.base.filter.ParamFilter.doFilter(ParamFilter.java:42)
at weblogic.servlet.internal.FilterChainImpl.doFilter(FilterChainImpl.java:56)
at com.ncs.crimes2.base.filter.SessionValidFilter.doFilter(SessionValidFilter.java:87)
at weblogic.servlet.internal.FilterChainImpl.doFilter(FilterChainImpl.java:56)
at weblogic.servlet.internal.WebAppServletContext$ServletInvocationAction.wrapRun(WebAppServletContext.java:3715)
at weblogic.servlet.internal.WebAppServletContext$ServletInvocationAction.run(WebAppServletContext.java:3681)
at weblogic.security.acl.internal.AuthenticatedSubject.doAs(AuthenticatedSubject.java:321)
at weblogic.security.service.SecurityManager.runAs(SecurityManager.java:120)
at weblogic.servlet.internal.WebAppServletContext.securedExecute(WebAppServletContext.java:2277)
at weblogic.servlet.internal.WebAppServletContext.execute(WebAppServletContext.java:2183)
at weblogic.servlet.internal.ServletRequestImpl.run(ServletRequestImpl.java:1454)
at weblogic.work.ExecuteThread.execute(ExecuteThread.java:209)
at weblogic.work.ExecuteThread.run(ExecuteThread.java:178)

1 个答案:

答案 0 :(得分:1)

也许您应该使用cursor.toNextSibling()而不是创建新段落和新游标:

 public static void main(String[] args) throws Exception {
    FileOutputStream out = new FileOutputStream(new File("word.docx"));
    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).getParagraphs().get(0);
    XmlCursor cursor = paragraph.getCTP().newCursor();
    XWPFTable tableTwo = tablerow.getCell(1).insertNewTbl(cursor);

    tablerow = tableTwo.createRow();
    tablerow.createCell().setText("aaaaa");
    tablerow.createCell().setText("bbbbb");
    tablerow = tableTwo.createRow();
    tablerow.getCell(0).setText("cccc");
    tablerow.getCell(1).setText("dddd");

    tablerow = tableOne.getRow(1);
    cursor.toNextSibling();
    XWPFTable tableThree = tablerow.getCell(1).insertNewTbl(cursor);

    tablerow = tableThree.createRow();
    tablerow.createCell().setText("eeee");
    tablerow.createCell().setText("ffff");
    tablerow = tableThree.createRow();
    tablerow.getCell(0).setText("gggg");
    tablerow.getCell(1).setText("hhhh");

    document.write(out);
    out.close();

}