依赖版本

Apache poi 4.1.1
poi-ooxml-4.1.1.jar
poi-ooxml-schemas-4.1.1.jar
poi-excelant-4.1.1.jar
commons-codec-1.13.jar
commons-collections4-4.4.jar
commons-compress-1.19.jar
commons-logging-1.2.jar
commons-math3-3.6.1.jar
poi-4.1.1.jar
xmlbeans-3.1.0.jar
JDK 1.8
# 由于POI官方提供的example为使用模版的方法,并且文档并不全面,所以本文所产生的代码并不是准确解决方案,仅作参考

页眉页脚实现相对简单

/**
     * 设置页头
     * 参考示例  https://stackoverflow.com/questions/43252994/how-to-set-header-and-footer-position-in-poi-word-xwpf
     * @param document
     * @param headerInfo
     */
    static void setHeaderInfo(XWPFDocument document, String text ) {

        XWPFHeaderFooterPolicy headerFooterPolicy = document.getHeaderFooterPolicy();
        if (headerFooterPolicy == null) headerFooterPolicy = document.createHeaderFooterPolicy();
        XWPFHeader header = headerFooterPolicy.createHeader(XWPFHeaderFooterPolicy.DEFAULT);
        XWPFParagraph paragraph = header.createParagraph();
        paragraph.setAlignment(ParagraphAlignment.LEFT);
        XWPFRun run = paragraph.createRun();
        setHeaderAndFooterXWPFRunStyle(run, "新宋体", 10);
        if (StringUtils.isNotEmpty(text) {
            run = paragraph.createRun();
            run.setText(text);
            setHeaderAndFooterXWPFRunStyle(run, "新宋体", 10);
        }


    }

    /**
     * 创建页脚
     * 
     * 参考示例  https://stackoverflow.com/questions/43252994/how-to-set-header-and-footer-position-in-poi-word-xwpf
     * @param document
     * @param footerInfo
     */
    static void setFooterInfo(XWPFDocument document, FooterInfo footerInfo) {

        XWPFHeaderFooterPolicy headerFooterPolicy = document.getHeaderFooterPolicy();
        if (headerFooterPolicy == null) headerFooterPolicy = document.createHeaderFooterPolicy();
        XWPFFooter footer = headerFooterPolicy.createFooter(XWPFHeaderFooterPolicy.DEFAULT);

        XWPFParagraph  paragraph = footer.createParagraph();
        paragraph.setAlignment(ParagraphAlignment.CENTER);

        paragraph.setVerticalAlignment(TextAlignment.CENTER);
        XWPFRun run = paragraph.createRun();

        run.setText("第");
        setHeaderAndFooterXWPFRunStyle(run, "仿宋", 10);

        run = paragraph.createRun();
        CTFldChar fldChar = run.getCTR().addNewFldChar();
        fldChar.setFldCharType(STFldCharType.Enum.forString("begin"));

        run = paragraph.createRun();
        CTText ctText = run.getCTR().addNewInstrText();
        ctText.setStringValue("PAGE  \\* MERGEFORMAT");
        ctText.setSpace(SpaceAttribute.Space.Enum.forString("preserve"));
        setHeaderAndFooterXWPFRunStyle(run, "仿宋", 10);

        fldChar = run.getCTR().addNewFldChar();
        fldChar.setFldCharType(STFldCharType.Enum.forString("end"));

        run = paragraph.createRun();
        run.setText("页 总共");
        setHeaderAndFooterXWPFRunStyle(run, "仿宋", 10);

        run = paragraph.createRun();
        fldChar = run.getCTR().addNewFldChar();
        fldChar.setFldCharType(STFldCharType.Enum.forString("begin"));

        run = paragraph.createRun();
        ctText = run.getCTR().addNewInstrText();
        ctText.setStringValue("NUMPAGES  \\* MERGEFORMAT ");
        ctText.setSpace(SpaceAttribute.Space.Enum.forString("preserve"));
        setHeaderAndFooterXWPFRunStyle(run, "仿宋", 10);

        fldChar = run.getCTR().addNewFldChar();
        fldChar.setFldCharType(STFldCharType.Enum.forString("end"));

        run = paragraph.createRun();
        run.setText("页");

        setHeaderAndFooterXWPFRunStyle(run, "仿宋", 10);



    }

只要有树叶飞舞的地方,火就会燃烧。