java如何使用POI api在word文档中添加页眉和页脚?
我试着用下面的代码在我的文档中添加页脚,但效果不好
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 sectPr1 = document.getDocument().getBody().addNewSectPr();
XWPFHeaderFooterPolicy headerFooterPolicy = new XWPFHeaderFooterPolicy( document,sectPr1 );
headerFooterPolicy.createFooter( STHdrFtr.DEFAULT, newparagraphs );
我正在使用以下JAR生成我的文档。请帮忙
- poi-3.10-FINAL-20140208。罐子
- poi-ooxml-3.10-FINAL-20140208。罐子
- poi-ooxml-schemas-3.10-FINAL-20140208。罐子
- poi-scratchpad-3.10-FINAL-20140208
# 1 楼答案
Adding footer to ms word using POI api
# 2 楼答案
将评论升级为回答
问题不在于页眉/页脚代码。你问题中的代码看起来适合做你想做的事情。不过,如果您确实想做更多的工作,那么想到的最佳参考是unit tests for headers and footers in Apache POI,它涵盖了更多的用例
你的代码没有做的是在你做了修改后把文件写出来
只需在代码末尾添加对write(OutputStream)的调用,就可以了。差不多