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();
    }
}
相关推荐
fire-flyer37 分钟前
Spring Boot 源码解析之 Logging
java·spring boot·spring·log4j·logging
CodeCraft Studio39 分钟前
Excel处理控件Aspose.Cells教程:使用 Python 在 Excel 中创建甘特图
python·excel·项目管理·甘特图·aspose·aspose.cells
KoiHeng2 小时前
部分排序算法的Java模拟实现(复习向,非0基础)
java·算法·排序算法
cui_hao_nan5 小时前
JVM——如何对java的垃圾回收机制调优?
java·jvm
熟悉的新风景7 小时前
springboot项目或其他项目使用@Test测试项目接口配置-spring-boot-starter-test
java·spring boot·后端
心平愈三千疾7 小时前
学习秒杀系统-实现秒杀功能(商品列表,商品详情,基本秒杀功能实现,订单详情)
java·分布式·学习
玩代码7 小时前
备忘录设计模式
java·开发语言·设计模式·备忘录设计模式
BUTCHER58 小时前
Docker镜像使用
java·docker·容器
岁忧8 小时前
(nice!!!)(LeetCode 面试经典 150 题 ) 30. 串联所有单词的子串 (哈希表+字符串+滑动窗口)
java·c++·leetcode·面试·go·散列表
LJianK19 小时前
Java和JavaScript的&&和||
java·javascript·python