我试图使用 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)