Java通过模板替换实现excel的传参填写

以模板为例子

将上面$转义的内容替换即可

java 复制代码
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();
    }
}
相关推荐
东小西8 小时前
【SAA实战】第 4 篇 · Agent 短期记忆:saver 让 Agent 跨轮记得住(threadId 隔离)
java·后端·spring
许彰午8 小时前
22-DataCenter报文序列化
java·低代码·架构·状态模式
2601_962065258 小时前
[MySQL] SQL优化之性能分析
java·sql·mysql
小范同学_8 小时前
JDK1.7 与 JDK1.8 HashMap 底层原理对比 + 数组并发扩容死循环详解
java·开发语言
予昊9 小时前
从零实现“在线五子棋对战“:WebSocket 实时通信 + 段位匹配
java·开发语言·网络·websocket
2601_962203519 小时前
【SpringAI入门】初识SpringAI
java
weixin_4614085810 小时前
Mybatis-flex小记
java·开发语言·mybatis
2601_9621771310 小时前
【MySQL篇】聚合查询,联合查询
android·java·mysql
それども10 小时前
IDEA接入Claude完整流程
java·ai·intellij-idea
小鹿的周先生11 小时前
Spring-AI-第2篇-ChatClient 实战:使用 DeepSeek 完成第一次 AI 对话
java·人工智能·spring