一、获取添加页眉doc文件
我的项目文件路径:
String reportSavePath= "****************";
DOCX文件一:
changer.setAccTemplateReturnDoc(templatePath);(经过我其他操作返回的docx文件)
DOCX文件二:
String path="D:\\abc.docx";(已知路径docx文件)
二、开始获取doc对象
个人:
XWPFDocument docx=changer.setAccTemplateReturnDoc(reportTemplatePath);
样板:
File file= new File(path);
FileInputStream Fs= new FileInputStream(file);
XWPFDocument docx=changer.setAccTemplateReturnDoc(Fs);
三、进行docx设置页眉处理
接下来为模板通用操作:
CTP ctp = CTP.Factory.newInstance();
XWPFParagraph paragraph = new XWPFParagraph(ctp, docx);
XWPFRun r = paragraph.createRun();
r.setText(CaseNo+" 附件一");//设置简单页眉内容
r.setFontSize(10);//设置页眉字号
r.setFontFamily("黑体");//设置页眉字体颜色
paragraph.setBorderBottom(Borders.THICK);//设置页眉下框线
XWPFParagraph[] paragraph1 = new XWPFParagraph[1];
paragraph1[0]=paragraph;
CTSectPr sectPr = docx.getDocument().getBody().isSetSectPr() ? docx.getDocument().getBody().getSectPr() : docx.getDocument().getBody().addNewSectPr();
XWPFHeaderFooterPolicy policy = new XWPFHeaderFooterPolicy(docx, sectPr);
XWPFHeader header =
XWPFHeader header = policy.createHeader(STHdrFtr.DEFAULT, paragraph1);
header.setXWPFDocument(docx);
OutputStream os = new FileOutputStream("D:\\newabc.docx");
docx.write(os);
解决了自己的问题,我还只是个小白,文章仅供参考,如有不当,谢谢指教!!!谢谢
本人稍会一点java poi操作WORD、EXCEL,有兴趣可以一起讨论