记录使用jxls 模板导出excel

记录使用jxls 模板导出excel

jxls官方文档

导入依赖

        <!-- jxls导出导入读取excel报表 -->
        <!-- https://mvnrepository.com/artifact/org.jxls/jxls -->
        <dependency>
            <groupId>org.jxls</groupId>
            <artifactId>jxls</artifactId>
            <version>2.8.1</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/org.jxls/jxls-poi -->
        <dependency>
            <groupId>org.jxls</groupId>
            <artifactId>jxls-poi</artifactId>
            <version>2.8.1</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/org.jxls/jxls-jexcel -->
        <dependency>
            <groupId>org.jxls</groupId>
            <artifactId>jxls-jexcel</artifactId>
            <version>1.0.9</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/org.jxls/jxls-reader -->
        <dependency>
            <groupId>org.jxls</groupId>
            <artifactId>jxls-reader</artifactId>
            <version>2.0.6</version>
        </dependency>

其实官方文档写的很清楚,贴一下自己的核心代码

        InputStream inputStream = null;
        ServletOutputStream outputStream = null;
        try {
            List<T> list = this.list();
            if (CollectionUtils.isEmpty(list)) {
                throw new CheckException("无数据");
            }
            if (excelTemplate.contains(".xlsx")) {
                inputStream = Thread.currentThread().getContextClassLoader().getResourceAsStream("excelTemplate" + "/" + excelTemplate);//这一块主要是为了获取resources下的excelTemplate下的文件
            }else{
                inputStream = Thread.currentThread().getContextClassLoader().getResourceAsStream("excelTemplate" + "/" + excelTemplate + ".xlsx");
            }
            String fileName = URLEncoder.encode(aclass.getSimpleName(), "UTF-8");
            RequestAttributes requestAttributes = RequestContextHolder.getRequestAttributes();
            HttpServletResponse response = ((ServletRequestAttributes) requestAttributes).getResponse();
            response.setContentType("application/vnd.ms-excel");
            response.setCharacterEncoding("utf8");
            response.setHeader("Content-Disposition", "attachment; filename="
                    + fileName + "_" + LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd")) + ".xlsx");
            response.setHeader("Pragma", "public");
            response.setHeader("Cache-Control", "no-store");
            response.addHeader("Cache-Control", "max-age=0");
            Context context = new Context();
            context.putVar("data", list);
            JxlsHelper.getInstance().processTemplate(inputStream, response.getOutputStream(), context);
            outputStream = response.getOutputStream();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (inputStream != null) {
                try {
                    inputStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (outputStream != null) {
                try {
                    outputStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }

excel模板有几个坑需要注意一下 这个lastCell如果不想查看字段,ZZ2就可以包括全部
在这里插入图片描述
调用方法,就可以导出excel

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值