当前位置: 首页 > 图灵资讯 > 技术篇> SpringBoot动态导出word文档POI-TL

SpringBoot动态导出word文档POI-TL

来源:图灵教育
时间:2023-06-18 09:21:34

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();// Stringg核心替换逻辑 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(“yyyyy年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、替换模板

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