Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/redis/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何使用POI api在word文档中添加页眉和页脚?_Java_Apache Poi - Fatal编程技术网

Java 如何使用POI api在word文档中添加页眉和页脚?

Java 如何使用POI api在word文档中添加页眉和页脚?,java,apache-poi,Java,Apache Poi,我尝试使用以下代码在文档中添加页脚,但效果不佳 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,

我尝试使用以下代码在文档中添加页脚,但效果不佳

  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.jar poi-ooxml-3.10-FINAL-20140208.jar poi-ooxml-schemas-3.10-FINAL-20140208.jar poi-scratchpad-3.10-FINAL-20140208
将评论升级为答案

问题不在于页眉/页脚代码。你问题中的代码看起来适合做你想做的事情。如果您确实想做更多的工作,您会想到的最好的参考是,它涵盖了更多的用例

你的代码没有做的是在你做了更改后写出文件

您只需要在代码末尾添加一个对的调用,就可以了。差不多

FileOutputStream out = new FileOutputStream("WithHeader.docx");
document.write(out);
out.close();

你说工作不好是什么意思?部分有效?给出一个错误?出现错误?另外,保存文件时没有显示任何代码,可以这么简单吗?请您帮助完成代码好吗?请告诉我如何将此保存到文件?要保存文件,无论您使用何种格式,它都是相同的-只需致电感谢Gagravarr获得您的帮助。这个答案应该对这个片段有一些解释。
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 );