POI根据word模块生成新的word
工具类
package com.otoc.quality.spc.util;
import org.apache.poi.ooxml.POIXMLDocument; import org.apache.poi.xwpf.usermodel.*; import java.io.File; import java.io.FileOutputStream; import java.io.IOException; import java.util.List; import java.util.Map; import java.util.Set; /** * 通过word模板生成新的word工具类 * Created by GaoYuman on 2022/2/15 9:34 */ public class PoiWordUtils { /** * 根据模板生成新word文档 * 判断表格是需要替换还是需要插入,判断逻辑有$为替换,表格无$为插入 * @param inputUrl 模板存放地址 * @param outputUrl 新文档存放地址 * @param textMap 需要替换的信息集合 * @param tableList 需要插入的表格信息集合 * @return 成功返回true,失败返回false */ public static boolean changWord(String inputUrl, String outputUrl, Map<String, String> textMap, List<String[]> tableList) {
boolean resultFlag = true; try { //获取docx解析对象 XWPFDocument document = new XWPFDocument(POIXMLDocument.openPackage(inputUrl)); //解析替换文本段落对象 PoiWordUtils.changeText(document, textMap); //解析替换表格对象 PoiWordUtils.changeTable(document, textMap, tableList); //生成新的word File file = new File(outputUrl); FileOutputStream stream = new FileOutputStream(file); document.write(stream); stream.close();
return resultFlag; } catch (IOException e) { e.printStackTrace();
resultFlag = false } return resultFlag; } /** * 替换段落文本 * @param document docx解析对象 * @param textMap 需要替换的信息集合 */ public static void changeText(XWPFDocument document, Map<String, String> 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(changeValue(run.toString(), textMap),0); } } } } /** * 替换表格对象方法 * @param document docx解析对象 * @param textMap 需要替换的信息集合 * @param tableList 需要插入的表格信息集合 */ public static void changeTable(XWPFDocument document, Map<String, String> 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(rows, textMap); }else{ // System.out.println("插入"+table.getText()); insertTable(table, tableList); } } } } /** * 遍历表格 * @param rows 表格行对象 * @param textMap 需要替换的信息集合 */ public static void eachTable(List<XWPFTableRow> rows ,Map<String, String> 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); } } } } } } /** * 为表格插入数据,行数不够添加新行 * @param table 需要插入数据的表格 * @param tableList 插入数据集合 */ public static void insertTable(XWPFTable table, List<String[]> tableList){ //创建行,根据需要插入的数据添加新行,不处理表头 int rowsCount = table.getRows().size(); for(int i = rowsCount; i < tableList.size(); i++){ XWPFTableRow row =table.createRow(); } //遍历表格插入数据 List<XWPFTableRow> rows = table.getRows(); for(int i = 0; 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)[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 String changeValue(String value, Map<String, String> textMap){ Set<Map.Entry<String, String>> textSets = textMap.entrySet(); for (Map.Entry<String, String> textSet : textSets) { //匹配模板与替换值 格式${key} String key = "${"+textSet.getKey()+"}"; if(value.indexOf(key)!= -1){ value = value.replace(key, textSet.getValue()); } } //模板未匹配到区域替换为空 //if(checkText(value)){ // value = ""; //} return value; } }
测试类:
@RequestMapping("exportWord") public void exportWord() { //模板文件地址 String inputUrl = "src/main/resources/temporaryWord.docx"; //新生成的模板文件 String outputUrl = "C:\\Users\\frank\\Desktop\\test.docx"; Map<String, String> testMap = new HashMap<String, String>(); testMap.put("name", "小明"); testMap.put("sex", "男"); testMap.put("address", "软件园"); testMap.put("phone", "88888888"); List<String[]> testList = new ArrayList<String[]>(); testList.add(new String[]{"1"}); testList.add(new String[]{"2","2AA","2BB"}); testList.add(new String[]{"3","3AA","3BB","3CC"}); testList.add(new String[]{"4","4AA","4BB","4CC","4AA","4BB","4CC"}); PoiWordUtils.changWord(inputUrl, outputUrl, testMap, testList); }
效果:
模板word:
生成新word:
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】博客园社区专享云产品让利特惠,阿里云新客6.5折上折
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 微软正式发布.NET 10 Preview 1:开启下一代开发框架新篇章
· 没有源码,如何修改代码逻辑?
· NetPad:一个.NET开源、跨平台的C#编辑器
· PowerShell开发游戏 · 打蜜蜂
· 凌晨三点救火实录:Java内存泄漏的七个神坑,你至少踩过三个!