首页 > 其他分享 >SpringBoot动态导出word文档POI-TL

SpringBoot动态导出word文档POI-TL

时间:2023-06-16 16:01:09浏览次数:44  
标签:word SpringBoot poi dictParam param TL paramMap put String

1、引入依赖,同步使用hutool工具进行开发

<dependency>
            <groupId>com.deepoove</groupId>
            <artifactId>poi-tl</artifactId>
            <version>1.10.0</version>
        </dependency>
        <dependency>
            <groupId>org.apache.poi</groupId>
            <artifactId>poi</artifactId>
            <version>4.1.2</version>
        </dependency>
        <dependency>
            <groupId>org.apache.poi</groupId>
            <artifactId>poi-ooxml</artifactId>
            <version>4.1.2</version>
        </dependency>

        <dependency>
            <groupId>org.apache.poi</groupId>
            <artifactId>poi-ooxml-schemas</artifactId>
            <version>4.1.2</version>
        </dependency>

        <dependency>
            <groupId>commons-io</groupId>
            <artifactId>commons-io</artifactId>
            <version>2.7</version>
        </dependency>


2、替换模板的方法

// 模板文件路径
File tempFile = new File(templatePath);
FileUtils.copyInputStreamToFile(in, tempFile);

// 生成word的路径
String fileDir = getClass().getClassLoader().getResource("").getPath();
// 生成word的文件
String fileName = UUID.randomUUID().toString().replace("-", "") + "_create." + templateAttach.getSuffix();

// 核心替换逻辑
String wordPath = createWord(templatePath, fileDir, fileName, param);
wordPath = wordPath.replace("\\", "/");


3、核心替换逻辑

public String createWord(String templatePath, String fileDir, String fileName, ContractWordTplReplaceParam param) {
        Assert.notNull(templatePath, "word模板文件路径不能为空");
        Assert.notNull(fileDir, "生成的文件存放地址不能为空");
        Assert.notNull(fileName, "生成的文件名不能为空");
        File dir = new File(fileDir);
        if (!dir.exists()) {
            log.info("目录不存在,创建文件夹{}!", fileDir);
            dir.mkdirs();
        }
        String filePath = fileDir +"\\"+ fileName;

        Map<String, Object> paramMap = new HashMap<>();
        // 基础信息
        paramMap.put("title", param.getTitle());

        DateFormat dateFormat = new SimpleDateFormat("yyyy年MM月dd日");
        String nowDate = dateFormat.format(new Date());
        paramMap.put("createTime", nowDate);

        DictDataItemQueryParam dictParam = new DictDataItemQueryParam();
        dictParam.setDictType("mg_contract_type");
        dictParam.setDictValue(param.getType());
        DictDataDto type = dictDataFeign.queryByTypeValue(dictParam).getData();
        paramMap.put("type", type.getDictLabel());

        dictParam.setDictType("mg_range_type");
        dictParam.setDictValue(String.valueOf(param.getRangeType()));
        DictDataDto rangeType = dictDataFeign.queryByTypeValue(dictParam).getData();
        paramMap.put("rangeType", rangeType.getDictLabel());

        paramMap.put("startTime", dateFormat.format(param.getStartTime()));
        paramMap.put("endTime", dateFormat.format(param.getStartTime()));

        paramMap.put("summary", param.getSummary());

        paramMap.put("amount", param.getAmount());

        // 列表循环
        paramMap.put("signList", param.getSignList());
        paramMap.put("productList", param.getProductList());
        paramMap.put("costList", param.getCostList());
        paramMap.put("expenseList", param.getExpenseList());

        // 读取模板渲染参数
        LoopRowTableRenderPolicy policy = new LoopRowTableRenderPolicy();
        Configure config = Configure.builder()
                .bind("signList", policy)
                .bind("productList", policy)
                .bind("costList", policy)
                .bind("expenseList", policy)
                .build();
        XWPFTemplate template = XWPFTemplate.compile(templatePath, config)
                .render(paramMap);
        try {
            // 将模板参数写入路径
            template.writeToFile(filePath);
            template.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return filePath;
    }

4、替换模板

文件没有办法上传,具体的可以参考百度,支持文本、列表、图片(实际上是特殊文本)的替换。



标签:word,SpringBoot,poi,dictParam,param,TL,paramMap,put,String
From: https://blog.51cto.com/u_15595167/6500319

相关文章

  • SpringBoot 接口返回响应体修改
    在开发中,有时候会遇到一些统一修改http接口响应体内容的场景,比如返回体Wrapper统一增加一些数据包装处理。解决思路:通过自定义注解+切面 或者自定义注解+ ResponseBodyAdvice处理接口返回体1、创建自定义注解 importjava.lang.annotation.ElementType;importjava......
  • 如何在WORDPRESS中添加CNZZ等统计代码
    1,   首先进入我们的WordPress网站后台,即在浏览器上输入网站域名/wp-login,如我的网站是输入forlong401.com/wp-login,然后输入用户名及密码,进入后台,点击左侧的“外观->主题”,查看一下我们使用的是什么主题,像我的进入后台后,会发现有三个主题可供选择,一个TwentyThirteen、Twenty......
  • 教你如何完美更改wordpress站域名
    最近因为要把博客网站从nas上搬运到阿里云服务器,又重温了一遍如何完美搬迁wordpress整站。其实搬运wordpress博客无非就是以下两种情况:1.更换服务器,不换域名2.更换域名下面我分别介绍一下如何完美搬迁wordpress博客1.更换服务器,不换域名这种情况下相对比较简单,三步即可备......
  • KindEditor从word复制粘贴公式
    ​ 这种方法是servlet,编写好在web.xml里配置servlet-class和servlet-mapping即可使用后台(服务端)java服务代码:(上传至ROOT/lqxcPics文件夹下)<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%><%@     page contentType="text/html;cha......
  • springboot-Quartz定时任务并持久化
    新建项目,添加依赖新建一个springboot项目,勾选下springboot,以及quartz依赖 或者我们可以直接在pom.xml文件中直接添加依赖<dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-quartz</artifactId>......
  • springboot防重复提交
    springboot防重复提交1、场景网页卡顿的时候,用户点击会造成重复操作如果前端不做防重复操作。会导致重复提交,重复下单等意外操作。而且对于系统资源来说也是一种浪费常规的解决方法是让前端把点击后的按钮设置为不可点击,这样基本上能就能解决了。99.999999%能解决。前端这么弄......
  • Matlab正态分布、历史模拟法、加权移动平均线 EWMA估计风险价值VaR和回测标准普尔指数
    原文链接:http://tecdat.cn/?p=24480 最近我们被客户要求撰写关于风险价值的研究报告,包括一些图形和统计输出。此示例说明如何使用三种方法估计风险价值(VaR)并执行VaR回测分析。这三种方法是:正态分布历史模拟指数加权移动平均线(EWMA)风险价值是一种量化与投......
  • eWebEditor从word复制粘贴公式
    ​如何做到ueditor批量上传word图片?1、前端引用代码<!DOCTYPE html PUBLIC "-//W3C//DTDXHTML1.0Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head>......
  • kubectl 转发端口流量
     之前运行了一次错误的命令,查看pid。[[email protected]]#psaux|grepkubectlroot    14819 0.0 0.0112828 2296pts/0   S+  11:30  0:00grep--color=autokubectlroot    19199 0.1 0.576002843384pts/0   Sl  1......
  • TinyMCE从word复制粘贴公式
    ​ 自动导入Word图片,或者粘贴Word内容时自动上传所有的图片,并且最终保留Word样式,这应该是Web编辑器里面最基本的一个需求功能了。一般情况下我们将Word内容粘贴到Web编辑器(富文本编辑器)中时,编辑器都无法自动上传图片。需要用户手动一张张上传Word图片。如果只有一张图片还能够接......