使用POI 导出word模板文件
maven依赖
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi</artifactId>
<version>3.16</version>
</dependency>
自定义XWPFDocument 类,重写方法插入图片:
/**
* @BelongsProject: exchange
* @BelongsPackage: com.elens.util
* @Author: xuweichao
* @CreateTime: 2019-03-20 12:34
* @Description: 重写XWPFDocument的方法,插入图片
*/
public class CustomXWPFDocument extends XWPFDocument {
public CustomXWPFDocument() {
super();
}
public CustomXWPFDocument(OPCPackage opcPackage) throws IOException {
super(opcPackage);
}
public CustomXWPFDocument(InputStream in) throws IOException {
super(in);
}
public void createPicture(XWPFRun run,String blipId, int id, int width, int height) {
final int EMU = 9525;
width *= EMU;
height *= EMU;
//旧版本方法 .getPackageRelationship() 在该依赖包下已被删除
//String blipId = getAllPictures().get(id).getPackageRelationship().getId();
//在docment下创建XWPFRun 图片会被添加到文档末尾
// CTInline inline = createParagraph().createRun().getCTR().addNewDrawing().addNewInline();
CTInline inline =run.getCTR().addNewDrawing().addNewInline();
String picXml = "" +
"<a:graphic xmlns:a=\"http://schemas.openxmlformats.org/drawingml/2006/main\">" +
" <a:graphicData uri=\"http://schemas.openxmlformats.org/drawingml/2006/picture\">" +
" <pic:pic xmlns:pic=\"http://schemas.openxmlformats.org/drawingml/2006/picture\">" +
" <pic:nvPicPr>" +
" <pic:cNvPr id=\"" + id + "\" name=\"Generated\"/>" +
" <pic:cNvPicPr/>" +
" </pic:nvPicPr>" +
" <pic:blipFill>" +
" <a:blip r:embed=\"" + blipId + "\" xmlns:r=\"http://schemas.openxmlformats.org/officeDocument/2006/relationships\"/>" +
" <a:stretch>" +
" <a:fillRect/>" +
" </a:stretch>" +
" </pic:blipFill>" +
" <pic:spPr>" +
" <a:xfrm>" +
" <a:off x=\"0\" y=\"0\"/>" +
" <a:ext cx=\"" + width + "\" cy=\"" + height + "\"/>" +
" </a:xfrm>" +
" <a:prstGeom prst=\"rect\">" +
" <a:avLst/>" +
" </a:prstGeom>" +
" </pic:spPr>" +
" </pic:pic>" +
" </a:graphicData>" +
"</a:graphic>";
//CTGraphicalObjectData graphicData = inline.addNewGraphic().addNewGraphicData();
XmlToken xmlToken = null;
try {
xmlToken = XmlToken.Factory.parse(picXml);
} catch (XmlException xe) {
xe.printStackTrace();
}
inline.set(xmlToken);
//graphicData.set(xmlToken);
inline.setDistT(0);
inline.setDistB(0);
inline.setDistL(0);
inline.setDistR(0);
CTPositiveSize2D extent = inline.addNewExtent();
extent.setCx(width);
extent.setCy(height);
CTNonVisualDrawingProps docPr = inline.addNewDocPr();
docPr.setId(id);
docPr.setName("Picture " + id);
docPr.setDescr("Generated");
}
}
文件导出工具类:
public class POIWordUtil {
/**
* 根据模板生成新word文档
* 判断表格是需要替换还是需要插入,判断逻辑有$为替换,表格无$为插入
*
* @param inputUrl 模板存放地址
* @param outputUrl 新文档存放地址
* @param textMap 需要替换的信息集合
* @param tableList 需要插入的表格信息集合
* @return 成功返回true, 失败返回false
*/
public static boolean changWord(String inputUrl, String outputUrl,
Map<String, Object> textMap, List<String[]> tableList) {
//模板转换默认成功
boolean changeFlag = true;
try {
//获取docx解析对象
CustomXWPFDocument document = new CustomXWPFDocument(POIXMLDocument.openPackage(inputUrl));
//解析替换文本段落对象
POIWordUtil.changeText(document, textMap);
//解析替换表格对象
POIWordUtil.changeTable(document, textMap, tableList);
//生成新的word
File file = new File(outputUrl);
if (!file.getParentFile().exists()) {
file.getParentFile().mkdirs();
}
FileOutputStream stream = new FileOutputStream(file);
document.write(stream);
stream.close();
} catch (IOException e) {
e.printStackTrace();
changeFlag = false;
}
return changeFlag;
}
/**
* 替换段落文本
*
* @param document docx解析对象
* @param textMap 需要替换的信息集合
*/
public static void changeText(XWPFDocument document, Map<String, Object> textMap) {
//获取段落集合
List<XWPFParagraph> paragraphs = document.getParagraphs();
for (XWPFParagraph paragraph : paragraphs) {
//判断此段落时候需要进行替换
String text = paragraph.getText();
if (checkText(text)) {
List<XWPFRun> runs = paragraph.getRuns();
for (XWPFRun run : runs) {
//替换模板原来位置
run.setText((String) changeValue(run.toString(), textMap), 0);
}
}
}
}
/**
* 替换表格对象方法
*
* @param document docx解析对象
* @param textMap 需要替换的信息集合
* @param tableList 需要插入的表格信息集合
*/
public static void changeTable(CustomXWPFDocument document, Map<String, Object> textMap,
List<String[]> tableList) {
//获取表格对象集合
List<XWPFTable> tables = document.getTables();
for (int i = 0; i < tables.size(); i++) {
//只处理行数大于等于2的表格,且不循环表头
XWPFTable table = tables.get(i);
if (table.getRows().size() > 1) {
//判断表格是需要替换还是需要插入,判断逻辑有$为替换,表格无$为插入
if (checkText(table.getText())) {
List<XWPFTableRow> rows = table.getRows();
//遍历表格,并替换模板
eachTable(document, rows, textMap);
} else {
// System.out.println("插入"+table.getText());
insertTable(table, tableList);
}
}
}
}
/**
* 遍历表格,包含对图片内容的替换
*
* @param rows 表格行对象
* @param textMap 需要替换的信息集合
*/
public static void eachTable(CustomXWPFDocument document, List<XWPFTableRow> rows, Map<String, Object> textMap) {
for (XWPFTableRow row : rows) {
List<XWPFTableCell> cells = row.getTableCells();
for (XWPFTableCell cell : cells) {
//判断单元格是否需要替换
if (checkText(cell.getText())) {
List<XWPFParagraph> paragraphs = cell.getParagraphs();
for (XWPFParagraph paragraph : paragraphs) {
List<XWPFRun> runs = paragraph.getRuns();
for (XWPFRun run : runs) {
// run.setText(changeValue(run.toString(), textMap),0);
Object ob = changeValue(run.toString(), textMap);
if (ob instanceof String) {
run.setText((String) ob, 0);
} else if (ob instanceof Map) {
run.setText("", 0);
Map pic = (Map) ob;
int width = Integer.parseInt(pic.get("width").toString());
int height = Integer.parseInt(pic.get("height").toString());
int picType = getPictureType(pic.get("type").toString());
byte[] byteArray = (byte[]) pic.get("content");
ByteArrayInputStream byteInputStream = new ByteArrayInputStream(byteArray);
try {
String ind = document.addPictureData(byteInputStream, picType);
document.createPicture(run, ind, document.getNextPicNameNumber(picType), width, height);
} catch (Exception e) {
e.printStackTrace();
}
}
}
}
}
}
}
}
/**
* 为表格插入数据,行数不够添加新行
*
* @param table 需要插入数据的表格
* @param tableList 插入数据集合
*/
public static void insertTable(XWPFTable table, List<String[]> tableList) {
//创建行,根据需要插入的数据添加新行,不处理表头
for (int i = 1; i < tableList.size(); i++) {
XWPFTableRow row = table.createRow();
}
//遍历表格插入数据
List<XWPFTableRow> rows = table.getRows();
for (int i = 1; i < rows.size(); i++) {
XWPFTableRow newRow = table.getRow(i);
List<XWPFTableCell> cells = newRow.getTableCells();
for (int j = 0; j < cells.size(); j++) {
XWPFTableCell cell = cells.get(j);
cell.setText(tableList.get(i - 1)[j]);
}
}
}
/**
* 判断文本中时候包含$
*
* @param text 文本
* @return 包含返回true, 不包含返回false
*/
public static boolean checkText(String text) {
boolean check = false;
if (text.indexOf("$") != -1) {
check = true;
}
return check;
}
/**
* 匹配传入信息集合与模板
*
* @param value 模板需要替换的区域
* @param textMap 传入信息集合
* @return 模板需要替换区域信息集合对应值
*/
public static Object changeValue(String value, Map<String, Object> textMap) {
Object obj = null;
Set<Map.Entry<String, Object>> textSets = textMap.entrySet();
for (Map.Entry<String, Object> textSet : textSets) {
//匹配模板与替换值 格式${key}
String key = "${" + textSet.getKey() + "}";
if (value.indexOf(key) != -1) {
obj = textSet.getValue();
}
}
if (!(obj instanceof Map)) {
//模板未匹配到区域替换为空
if (obj != null && checkText(obj.toString())) {
obj = "";
}
}
return obj;
}
/**
* 根据图片类型,取得对应的图片类型代码
*
* @param picType
* @return int
*/
private static int getPictureType(String picType) {
int res = CustomXWPFDocument.PICTURE_TYPE_PICT;
if (picType != null) {
if (picType.equalsIgnoreCase("png")) {
res = CustomXWPFDocument.PICTURE_TYPE_PNG;
} else if (picType.equalsIgnoreCase("dib")) {
res = CustomXWPFDocument.PICTURE_TYPE_DIB;
} else if (picType.equalsIgnoreCase("emf")) {
res = CustomXWPFDocument.PICTURE_TYPE_EMF;
} else if (picType.equalsIgnoreCase("jpg") || picType.equalsIgnoreCase("jpeg")) {
res = CustomXWPFDocument.PICTURE_TYPE_JPEG;
} else if (picType.equalsIgnoreCase("wmf")) {
res = CustomXWPFDocument.PICTURE_TYPE_WMF;
}
}
return res;
}
/**
* 将输入流中的数据写入字节数组
*
* @param in
* @return
*/
public static byte[] inputStream2ByteArray(InputStream in, boolean isClose) {
byte[] byteArray = null;
try {
int total = in.available();
byteArray = new byte[total];
in.read(byteArray);
} catch (IOException e) {
e.printStackTrace();
} finally {
if (isClose) {
try {
in.close();
} catch (Exception e2) {
System.out.println("关闭流失败");
}
}
}
return byteArray;
}
}
功能测试:
String file = "H:/export_template.docx";//doc 模板文件位置
String outputUrl = "H:/doc/test.doc";//文件输出地址
Map<String, Object> testMap = new HashMap<>();
testMap.put("country", data.get("country").toString());
testMap.put("birthday", data.get("birthday").toString());
testMap.put("en_name", data.get("en_name").toString());
testMap.put("enName", data.get("en_name").toString());
testMap.put("gender", data.get("gender").toString());
testMap.put("cn_name", data.get("cn_name").toString());
testMap.put("introduce", data.get("introduce").toString());
testMap.put("tags", data.get("tags").toString());
testMap.put("main_related_china", data.get("main_related_china").toString());
testMap.put("recent_related_china", data.get("recent_related_china").toString());
testMap.put("type1", data.get("type1").toString());
testMap.put("type2", data.get("type2").toString());
testMap.put("recommend_org", data.get("recommend_org").toString());
testMap.put("url_baike", data.get("url_baike").toString());
testMap.put("url_facebook", data.get("url_facebook").toString());
testMap.put("url_twitter", data.get("url_twitter").toString());
testMap.put("url_wiki", data.get("url_wiki").toString());
testMap.put("url_linkedin", data.get("url_linkedin").toString());
testMap.put("url_blog", (String) data.get("url_blog");
String photo = data.get("photo").toString();
Map<String, Object> header = new HashMap<>();
header.put("width", 150);
header.put("height", 200);
header.put("type", photo.substring(photo.indexOf(".") + 1));
FileInputStream in = new FileInputStream("H:/img/test.png");
header.put("content", POIWordUtil.inputStream2ByteArray(in, true));
testMap.put("photo", header);
POIWordUtil.changWord(file, outputUrl, testMap, null);
我这里数据data 是从es 取得,测试的时候搞点假数据就好,路径修改成自己的。
这里${en_name} 是提前占位,具体逻辑实现可以看一下工具类。
最终效果是这样的
这里参考了网上的一些资料,但是没有达到预期的效果,通过对代码的解读做出修改,已在项目中应用,效果良好。