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();
    }
}
相关推荐
汉拓3D数字化15 分钟前
设计标准化怎么落地?企业设计标准化的5阶段实施路径
科技·excel·系统·软件
Rabitebla42 分钟前
C++11 新特性详解(一):列表初始化、initializer_list 与右值引用
java·开发语言·数据结构·c++·算法·leetcode·list
洋不写bug1 小时前
JavaComparable、Comparator、Cloneable接口解析,深拷贝浅拷贝详细解析
android·java·开发语言
坐吃山猪1 小时前
WebFlux_学习Subscriber总结
java·开发语言·学习·webflux
白露与泡影1 小时前
Java面试题及答案整理(2026年金九银十最新版,持续更新)
java·开发语言
geovindu1 小时前
java: Memento Pattern
java·开发语言·后端·备忘录模式·行为模式
xcLeigh1 小时前
Unity基础:Start与Update方法——Unity脚本生命周期初探
java·unity·游戏引擎·教程
zephyr052 小时前
链表实现O(n log n)时间复杂度的排序——归并排序详解
java·数据结构·算法
Zane19942 小时前
JRE 去哪了?一次讲清楚 JDK、JRE、JVM 到底是什么关系?
java·后端
PPPPickup2 小时前
基础知识差缺补漏-面试版持续更新
java·开发语言