百度真是越来越难用了,***!
还是推荐大家用必应吧
// POI方案为word添加页眉public static void main(String[] args) throws IOException {File is = new File("C:\\upload\\20190510_141809278_Test.docx");//文件路径FileInputStream fis = new FileInputStream(is);XWPFDocument docx = new XWPFDocument(fis);//文档对象CTP ctp = CTP.Factory.newInstance();XWPFParagraph paragraph = new XWPFParagraph(ctp, docx);//段落对象ctp.addNewR().addNewT().setStringValue("华丽的测试页眉2019051488888888");//设置页眉参数ctp.addNewR().addNewT().setSpace(SpaceAttribute.Space.PRESERVE);CTSectPr sectPr = docx.getDocument().getBody().isSetSectPr() ? docx.getDocument().getBody().getSectPr() : docx.getDocument().getBody().addNewSectPr();XWPFHeaderFooterPolicy policy = new XWPFHeaderFooterPolicy(docx, sectPr);XWPFHeader header = policy.createHeader(STHdrFtr.DEFAULT, new XWPFParagraph[] { paragraph });header.setXWPFDocument(docx);OutputStream os = new FileOutputStream("d:\\Test.docx");docx.write(os);//输出到本地}
再贴一个获取word页眉的方案
// POI 方案获取word页眉public static void main(String[] args) throws IOException {File is = new File("d:\\Test.docx");//文件路径FileInputStream fis = new FileInputStream(is);XWPFDocument docx = new XWPFDocument(fis);List<XWPFHeader> headerList = docx.getHeaderList();for (XWPFHeader xwpfHeader: headerList){System.out.println(xwpfHeader.getText());//页眉}}
转自:https://blog.csdn.net/made_in_zhong/article/details/90476112
我的其他文章
亲身分享 一次 字节跳动 真实面试经历和面试题