使用POI API将页脚添加到MS Word
我进行了很多搜索,并得到一些结果,这些结果中包含一些示例代码,但是没有人在工作。所有人都将获得空指针异常,或者如果生成了文档,则在打开文件(.docx)时出现错误并显示消息
我以为可能是我要添加一些内容,然后添加页脚会带来一些问题,所以这次我一开始就粘贴了页脚代码
索引超出范围的异常
这是我的完整代码
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();
}
}
请帮助我生成页脚,这是我的代码,如果我在段落和表后添加页脚代码,则没有运行时错误,但打开生成的文件时出错,如果我将页脚放在要添加到文档中的内容之前我收到错误“索引超出绑定异常”。如果有任何代码片段或至少有一些指向该解决方案的指针,请帮助我。谢谢
回答:
我遇到了这个问题,解决方法是我们必须使用3.10
最终的poi jar。 3.9
有这个问题。
请删除以前版本的jar,然后添加3.10
已修复此错误的最终版本的jar 。
罐子需要:
poi-3.10-FINAL.jar
poi-ooxml-3.10-FINAL.jar
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 wars。
以上是 使用POI API将页脚添加到MS Word 的全部内容, 来源链接: utcz.com/qa/422226.html