以模板为例子
将上面$
转义的内容替换即可
package com.gxuwz.zjh.util;import org.apache.poi.ss.usermodel.*;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import java.io.*;
import java.util.HashMap;
import java.util.Map;
import java.io.IOException;
import org.apache.poi.ss.usermodel.Workbook;
import org.springframework.stereotype.Component;/*** @author FangZenglin* @date 2023年10月12日17:27*/
@Component
public class ExcelZiDongTian {public void xieexcel_qitajia(String exceldizhi,String danwei,String xingming,String date,String zhiwu,String shiyou,String leixing,String baocundizhi) throws IOException {FileInputStream templateFile = new FileInputStream(exceldizhi);Workbook workbook = new XSSFWorkbook(templateFile);templateFile.close();Sheet sheet = workbook.getSheetAt(0);Map<String, String> data = new HashMap<>();data.put("danwei", danwei);data.put("xingming", xingming);data.put("date", date);data.put("zhiwu", zhiwu);data.put("shiyou", shiyou);data.put("leixing", leixing);for (Row row : sheet) {for (Cell cell : row) {String cellValue = cell.getStringCellValue();if (cellValue.startsWith("$")) {String key = cellValue.substring(1);if (data.containsKey(key)) {cell.setCellValue(data.get(key));}}}}FileOutputStream outputStream = new FileOutputStream(baocundizhi);workbook.write(outputStream);workbook.close();}public void xieexcel_nianjia(String exceldizhi,String danwei,String shoucigongzuonianyue,String gongling,String quxiang,String xingming,String date,String zhiwu,String leixing,String baocundizhi) throws IOException {FileInputStream templateFile = new FileInputStream(exceldizhi);Workbook workbook = new XSSFWorkbook(templateFile);templateFile.close();Sheet sheet = workbook.getSheetAt(0);Map<String, String> data = new HashMap<>();data.put("danwei", danwei);data.put("xingming", xingming);data.put("date", date);data.put("zhiwu", zhiwu);data.put("quxiang", quxiang);data.put("shoucigongzuonianyue",shoucigongzuonianyue);data.put("gongling",gongling);data.put("leixing", leixing);for (Row row : sheet) {for (Cell cell : row) {String cellValue = cell.getStringCellValue();if (cellValue.startsWith("$")) {String key = cellValue.substring(1);if (data.containsKey(key)) {cell.setCellValue(data.get(key));}}}}FileOutputStream outputStream = new FileOutputStream(baocundizhi);workbook.write(outputStream);workbook.close();}
}