使用POI api向ms字添加页脚

我搜索了很多并获得了一些结果,其中有一些示例代码,但没有人工作。 所有要么得到空指针exception,要么生成文档然后在打开文件(.docx)时给出错误并显示消息文本/ xml声明可能只在innput的最开始发生。

我想可能是我正在添加一些内容,然后添加页脚是一个问题所以我粘贴我的页脚代码在这一次我现在得到了

索引超出范围的exception

这是我的完整代码

String fileName ="Book.docx"; String folderPath=SystemProperties.get(SystemProperties.TMP_DIR)+File.separator+"liferay" + File.separator + "document_preview"; String filePath=folderPath+File.separator+fileName; File file=new File(filePath); XWPFDocument document = new XWPFDocument(); XWPFParagraph paragraphOne = document.createParagraph(); paragraphOne.setAlignment(ParagraphAlignment.CENTER); XWPFRun paragraphOneRunOne = paragraphOne.createRun(); paragraphOneRunOne.setText("Training Report"); paragraphOneRunOne.addBreak(); XWPFTable table = document.createTable(); XWPFTableRow tableRowOne = table.getRow(0); tableRowOne.getCell(0).setText("No"); tableRowOne.createCell().setText("Name"); XWPFHeaderFooterPolicy headerFooterPolicy = document.getHeaderFooterPolicy(); if (headerFooterPolicy == null) { CTBody body = document.getDocument().getBody(); CTSectPr sectPr = body.getSectPr(); if (sectPr == null) { sectPr = body.addNewSectPr(); } headerFooterPolicy = new XWPFHeaderFooterPolicy(document, sectPr); } CTP ctP1 = CTP.Factory.newInstance(); CTR ctR1 = ctP1.addNewR(); CTText t = ctR1.addNewT(); t.setStringValue("first footer"); XWPFParagraph codePara = new XWPFParagraph(ctP1); XWPFParagraph[] newparagraphs = new XWPFParagraph[1]; newparagraphs[0] = codePara; XWPFFooter xwpfFooter = null; xwpfFooter = headerFooterPolicy.createFooter(XWPFHeaderFooterPolicy.DEFAULT); FileOutputStream fileoutOfTraining = new FileOutputStream(file); document.write(fileoutOfTraining); fileoutOfTraining.flush(); fileoutOfTraining.close(); downloadOperation(file, fileName, resourceResponse); 

downloadOperation方法中的代码

 HttpServletResponse httpServletResponse =PortalUtil.getHttpServletResponse(resourceResponse); BufferedInputStream input = null; BufferedOutputStream output = null; httpServletResponse.setHeader("Content-Disposition", "attachment; filename=\""+fileName+"\"; filename*=UTF-8''"+fileName); int DEFAULT_BUFFER_SIZE=1024; try { input = new BufferedInputStream(new FileInputStream(file), DEFAULT_BUFFER_SIZE); } catch (FileNotFoundException e) { e.printStackTrace(); } try { resourceResponse.flushBuffer(); output = new BufferedOutputStream(httpServletResponse.getOutputStream(), DEFAULT_BUFFER_SIZE); } catch (IOException e) { e.printStackTrace(); } byte[] buffer = new byte[2*DEFAULT_BUFFER_SIZE]; int length; try { while ((length = input.read(buffer)) > 0) { output.write(buffer, 0, length); } output.flush(); } catch (IOException e) { e.printStackTrace(); } finally { try { output.close(); } catch (IOException e) { e.printStackTrace(); } } 

请帮我生成页脚,这是我的代码,如果我在我的段落和表后添加页脚代码然后没有运行时错误但是在打开生成的文件时出错,如果我在页面中放置了我要在文档中添加的内容然后我收到错误“索引超出范围的exception”。 如果任何人有任何代码片段或至少有一些指向解决方案的指针,请帮助我。 谢谢

我遇到了这个问题,解决方案是我们必须使用3.10最终的poi jar。 3.9有这个问题。 请删除以前版本的jar子并添加3.10最终版本的jar子,其中修复了此错误。 jars要求是1. poi-3.10-FINAL.jar 2. poi-ooxml-3.10-FINAL.jar 3. poi-ooxml-schemas-3.10-FINAL.jar容易在网上获得。 http://mvnrepository.com/artifact/org.apache.poi/poi/3.10-FINAL

 XWPFDocument document = new XWPFDocument(); CTP ctp = CTP.Factory.newInstance(); CTR ctr = ctp.addNewR(); CTRPr rpr = ctr.addNewRPr(); CTText textt = ctr.addNewT(); textt.setStringValue( " Page 1" ); XWPFParagraph codePara = new XWPFParagraph( ctp, document ); XWPFParagraph[] newparagraphs = new XWPFParagraph[1]; newparagraphs[0] = codePara; CTSectPr sectPr = document.getDocument().getBody().addNewSectPr(); XWPFHeaderFooterPolicy headerFooterPolicy = new XWPFHeaderFooterPolicy( document, sectPr ); headerFooterPolicy.createFooter( STHdrFtr.DEFAULT, newparagraphs ); 

上面的代码工作正常,请使用我上面提到的3.10战争。

我不知道从头开始向头部添加图像是否适用于新版本,但我知道“模板”的工作原理非常完美。 我用word创建了模板文档,按照我需要的方式调整了标题(在我的例子中,logo-image在右边,左边是虚拟文本的段落,另一个是将上面两个对象分开的图像)内容,在标题底部,所有页面上都重复这些内容,并将文档的其余部分留空。 在一开始,我没有再通过调用...new XWPFDocument()创建...new XWPFDocument() ,我是这样创建的:

 XWPFDocument doc = new XWPFDocument(new FileInputStream("pathTo/template.docx")); 

不只是用您的内容填充您的文档,如果您需要像我一样更新不同导出的标题文本,请调用更新标题函数,在我的情况下看起来像这样:

 public void updateHeader() throws InvalidFormatException, IOException { // load the header policy from template and update the paragraph text XWPFHeaderFooterPolicy headerFooterPolicy = document .getHeaderFooterPolicy(); XWPFHeader defaultHeader = headerFooterPolicy.getDefaultHeader(); defaultHeader.getParagraphs().get(0).getRuns().get(0) .setText(profileName, 0); // this is only to put some space between the content in the header and the real content defaultHeader.getParagraphs() .get(defaultHeader.getParagraphs().size() - 1) .setSpacingAfter(300); } 

据我所知,这可以从3.10开始,如果您偶然发现一些Java安全问题,请尝试当前的夜间版本,其中许多安全问题已得到解决。 对我来说,它甚至可以在Google App Engine上运行。