1、安装引用Apache POI
下载地址:https://poi.apache.org/download.html
Maven中pom.xml中添加依赖如下:
org.apache.poi poi-ooxml 3.14 org.apache.poi poi-excelant 3.14 org.apache.poi poi-examples 3.14 org.apache.xmlbeans xmlbeans 2.6.0
2、需要import导入的包
import com.auth0.jwt.internal.org.apache.commons.lang3.StringUtils;
import org.apache.poi.util.Units;
import org.apache.poi.xwpf.model.XWPFHeaderFooterPolicy;
import org.apache.poi.xwpf.usermodel.*;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.*;
import java.io.*;
import java.math.BigInteger;
import java.util.*;
3、添加不带图片的页眉
public void createHeader(XWPFDocument doc, String orgFullName) throws Exception {
/*
* 对页眉段落作处理
* */
CTSectPr sectPr = doc.getDocument().getBody().addNewSectPr();
XWPFHeaderFooterPolicy headerFooterPolicy = new XWPFHeaderFooterPolicy(doc,sectPr);
XWPFHeader header = headerFooterPolicy.createHeader(XWPFHeaderFooterPolicy.DEFAULT);
XWPFParagraph paragraph = header.getParagraphArray(0);
paragraph.setBorderBottom(Borders.THICK);
/*
* 添加字体页眉,公司全称
* */
if (StringUtils.isNotEmpty(orgFullName)) {
XWPFRun run = paragraph.createRun();
run.setText(orgFullName);
//setXWPFRunStyle(run,"新宋体",10);
}
}
4、创建带图片页眉
/** * 对页眉处理 页眉包含公司名称 二维码图片 * @param doc * @param orgFullName 公司全称 * @param logoFilePath 图片的地址 * @throws Exception */ public void createHeader(XWPFDocument doc, String orgFullName, String logoFilePath) throws Exception { /* * 对页眉段落作处理,使图片在页眉右边,公司全称在页眉左边 * */ CTSectPr sectPr = doc.getDocument().getBody().addNewSectPr(); XWPFHeaderFooterPolicy headerFooterPolicy = new XWPFHeaderFooterPolicy(doc, sectPr); XWPFHeader header = headerFooterPolicy.createHeader(XWPFHeaderFooterPolicy.DEFAULT); XWPFParagraph paragraph = header.getParagraphArray(0); paragraph.setAlignment(ParagraphAlignment.BOTH); //设置段落左对齐 paragraph.setBorderBottom(Borders.THICK); //设置下划线 XWPFRun run = paragraph.createRun(); setXWPFRunStyle(run,"新宋体",10); /* * 添加字体页眉,公司全称 * 公司全称在右边 * */ if (StringUtils.isNotEmpty(orgFullName)) { run = paragraph.createRun(); run.setText(orgFullName); setXWPFRunStyle(run,"新宋体",10); } //处理两个段落之间的制表符宽度 run.addTab(); CTTabStop tabStop = paragraph.getCTP().getPPr().addNewTabs().addNewTab(); tabStop.setVal(STTabJc.CENTER); int twipsPerInch = 1450; tabStop.setPos(BigInteger.valueOf(6 * twipsPerInch)); /* * 取到图片的字节流 * */ if (StringUtils.isNotEmpty(logoFilePath)) { File file=new File(logoFilePath); InputStream is = new FileInputStream(file); XWPFPicture picture = run.addPicture(is, XWPFDocument.PICTURE_TYPE_JPEG, logoFilePath, Units.toEMU(40), Units.toEMU(40)); String blipID = ""; for(XWPFPictureData picturedata : header.getAllPackagePictures()) { //这段必须有,不然打开的logo图片不显示 blipID = header.getRelationId(picturedata); } picture.getCTPicture().getBlipFill().getBlip().setEmbed(blipID); run.addTab(); is.close(); } }
5、设置页脚的样式
/** * 设置页脚的字体样式 * * @param r1 段落元素 */ private void setXWPFRunStyle(XWPFRun r1,String font,int fontSize) { r1.setFontSize(fontSize); CTRPr rpr = r1.getCTR().isSetRPr() ? r1.getCTR().getRPr() : r1.getCTR().addNewRPr(); CTFonts fonts = rpr.isSetRFonts() ? rpr.getRFonts() : rpr.addNewRFonts(); fonts.setAscii(font); fonts.setEastAsia(font); fonts.setHAnsi(font); }
6、设置页眉和页脚样式
//获取word文档解析对象XWPFDocument document = new XWPFDocument(POIXMLDocument.openPackage(sourceUrl));//设置文本中表格内容样式//---------------------------------------------------------List < XWPFTable > tbls = document.getTables();for (int i = 0; i < tbls.size(); i++) { String tbl = tbls.get(i).getText(); tbls.get(i).setStyleID("tbl-text"); //前提是styles.xml 中有styleId为tbl-text}//---------------------------------------------------------//设置页眉页脚样式//---------------------------------------------------------List < XWPFHeader > pageHeaders = document.getHeaderList();for (int i = 0; i < pageHeaders.size(); i++) { List < XWPFParagraph > headerPara = pageHeaders.get(i).getParagraphs(); for (int j = 0; j < headerPara.size(); j++) { headerPara.get(j).setStyle("page-header"); //前提是styles.xml 中有styleId为 page-header }}List < XWPFFooter > pageFooters = document.getFooterList();for (int i = 0; i < pageFooters.size(); i++) { List < XWPFParagraph > footerPara = pageFooters.get(i).getParagraphs(); for (int j = 0; j < footerPara.size(); j++) { footerPara.get(j).setStyle("page-footer"); //前提是styles.xml 中有styleId为 page-footer }}
相关文档: