根据word模板,使用poi-tl导出word表格中的数据,将导出的文件在网页下载

poi-tl 版本1.10.5
模板书写方式:
在这里插入图片描述
需要写一个要导出的表格数据实体类:
实体类要和模板中表格字段相对应。

public class OccDiseaseJson {

    /** 起止时间 */
    @ApiModelProperty(value = "起止时间" , position = 1)
    private String startEndDate;

    /** 工作单位 */
    @ApiModelProperty(value = "工作单位" , position = 1)
    private String org;

    /** 工种 */
    @ApiModelProperty(value = "工种" , position = 1)
    private String position;

    /** 接触职业病危害 */
    @ApiModelProperty(value = "接触职业病危害" , position = 1)
    private String harm;

    /** 防护措施 */
    @ApiModelProperty(value = "防护措施" , position = 1)
    private String defense;
}

记得写Set和Get哦。
Service中写的内容,只展示了列表数据如何操作
首先要创建表格行循环策略
再将表格数据中的实体类builder一下
可参照poi-tl文档:
poi-tl文档链接

//创建表格行循环策略
        LoopRowTableRenderPolicy policy = new LoopRowTableRenderPolicy();
        Configure configure = Configure.builder()
        		.bind("occDiseaseJsons",policy)
        		.build();

        String name= commonParam.getName();
        Map<String, Object> fileMap = new HashMap<>();
        fileMap = BeanUtil.beanToMap(commonParam);
        String templateFileName="laborers_custody_file.docx";
        String CustodyFileName="劳动者个人监护档案";
        //构造填充的参数
        File destTemplateFile = null;
        File resultFile = null;
        try {
            // 读取模板流
            InputStream inputStream = POICacheManager.getFile(templateFileName);
            // 创建一个临时模板
            destTemplateFile = FileUtil.writeFromStream(inputStream, FileUtil.file(FileUtil.getTmpDir() +
                    File.separator + templateFileName));
            // 构造填充的参数
            // 导出时间
            fileMap.put("exportDateTime", DateUtil.format(DateTime.now(), DatePattern.CHINESE_DATE_PATTERN));

            XWPFTemplate xwpfTemplate = XWPFTemplate.compile(destTemplateFile, configure).render(fileMap);
            // 生成临时导出文件
            resultFile = FileUtil.file(FileUtil.getTmpDir() + File.separator + name + CustodyFileName + ".docx");
            // 写入
            BufferedOutputStream outputStream = FileUtil.getOutputStream(resultFile);
            xwpfTemplate.write(outputStream);
            outputStream.close();
            // 下载
            CommonDownloadUtil.download(resultFile, response);
        } catch (Exception e) {
            CommonResponseUtil.renderError(response, "导出失败");
        } finally {
            // 删除临时文件
            if (ObjectUtil.isNotEmpty(destTemplateFile)) {
                FileUtil.del(destTemplateFile);
            }
            if (ObjectUtil.isNotEmpty(resultFile)) {
                FileUtil.del(resultFile);
            }
        }
  • 7
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值