POI根据模板生成新的Excel文件
之前写过一篇根据模板生成新的word文件的文章,生成excel文件也是一样的原理,也记录一下吧。
excel 工具类:
import org.apache.poi.hssf.usermodel.HSSFCell; import org.apache.poi.hssf.usermodel.HSSFRow; import org.apache.poi.hssf.usermodel.HSSFSheet; import org.apache.poi.hssf.usermodel.HSSFWorkbook; import org.apache.poi.xssf.usermodel.XSSFCell; import org.apache.poi.xssf.usermodel.XSSFRow; import org.apache.poi.xssf.usermodel.XSSFSheet; import org.apache.poi.xssf.usermodel.XSSFWorkbook; import org.apache.poi.xwpf.usermodel.*; import java.io.File; import java.io.FileInputStream; 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 PoiTemplateUtils { /** * 根据模板生成新excel文件 * @param inputUrl 模板存放地址 * @param outputUrl 模板文件存放位置(暂时没用) * @param textMap 需要替换的文本信息集合 * @param tableList 需要插入的表格信息集合 * @param changeTextStartRow 文本替换开始行 * @param changeTextEndRow 文本替换结束行 * @param tableInsertStartRow 表格插入开始行 * @return */ public static HSSFWorkbook changeExcel(String inputUrl, String outputUrl, Map<String, String> textMap, List<String[]> tableList, Integer changeTextStartRow, Integer changeTextEndRow, Integer tableInsertStartRow) { try { FileInputStream inStream = new FileInputStream(new File(inputUrl)); HSSFWorkbook wb = new HSSFWorkbook(inStream); HSSFSheet sheet = wb.getSheetAt(0); //解析替换文本段落对象 PoiTemplateUtils.changeText(sheet, textMap, changeTextStartRow, changeTextEndRow); //解析替换表格对象 insertTable(sheet, tableList, tableInsertStartRow); //生成新的Excel //File file = new File(outputUrl); //FileOutputStream stream = new FileOutputStream(file); //wb.write(stream); //stream.close(); return wb; } catch (IOException e) { e.printStackTrace(); } return null; } /** * 替换段落文本 * @param textMap 需要替换的信息集合 */ public static void changeText(HSSFSheet sheet, Map<String, String> textMap, Integer startRow, Integer endRow){ for(int i = startRow; i < endRow; i++){ HSSFRow row = sheet.getRow(i); for(int j = 1; j < row.getLastCellNum(); j++){ // fmea模板特殊要求,都从第二列开始 HSSFCell cell = row.getCell(j); cell.setCellValue(changeValue(cell.getStringCellValue(), textMap)); } } } /** * 为表格插入数据 * @param sheet * @param tableList 数据 * @param startRow 开始插入的行数 */ public static void insertTable(HSSFSheet sheet, List<String[]> tableList, Integer startRow){ //遍历表格插入数据 for(int i = startRow; i < tableList.size()+startRow; i++){ HSSFRow newRow = sheet.createRow(i); for(int j = 0; j < tableList.get(i-startRow).length; j++){ HSSFCell cell = newRow.createCell(j+1); // fmea模板特殊要求,都从第二列开始 cell.setCellValue(tableList.get(i-startRow)[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; } }
测试类:
/** * 根据模板生成新的word文件 * @return */ @RequestMapping("exportWord") public ResponseMessage exportWord() { //模板文件地址 String inputUrl = "src/main/resources/excelTemplate.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"}); testList.add(new String[]{"5","4AA","4BB","4CC","4AA","4BB"}); HSSFWorkbook wb = PoiTemplateUtils.changeExcel(inputUrl, outputUrl, testMap, testList, 3, 7, 11); OssResultMsg ossResultMsg = ossServiceHandler.uploadWord(document, UUID.randomUUID() + "/excel模板测试"); if (!ossResultMsg.isSuccess()) { return ResponseMessage.failure("导出失败"); } return ResponseMessage.success((String) ossResultMsg.getData()); }
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· 单线程的Redis速度为什么快?
· 展开说说关于C#中ORM框架的用法!
· Pantheons:用 TypeScript 打造主流大模型对话的一站式集成库
· SQL Server 2025 AI相关能力初探