Spring Boot 导出excel表格

Spring boot 导出Excel表格


前言

如何使用Spring Boot 导出excel表格,使用easyexcel


提示:以下是本篇文章正文内容,下面案例可供参考

一、如何使用Spring Boot 将数据导出为Excel

可以使用alibab的EasyExcel 这里主要讲的是简单写.

二、使用步骤

1.添加POM依赖

代码如下:

 <dependency>
        <groupId>com.alibaba</groupId>
        <artifactId>easyexcel</artifactId>
        <version>3.1.0</version>
        <exclusions>
            <exclusion>
                <artifactId>poi-ooxml-schemas</artifactId>
                <groupId>org.apache.poi</groupId>
            </exclusion>
        </exclusions>
</dependency>

2.写Excel

通用数据生成(service层)

    private List<DemoData> data() {
        List<DemoData> list = ListUtils.newArrayList();
        for (int i = 0; i < 10; i++) {
            DemoData data = new DemoData();
            data.setString("字符串" + i);
            data.setDate(new Date());
            data.setDoubleData(0.56);
            list.add(data);
        }
        return list;
    }

3.读入数据

代码如下(controller层代码):

   try {
       response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
        response.setCharacterEncoding("utf-8");
        String fileName = URLEncoder.encode("模板", "UTF-8").replaceAll("\\+", "%20");
        response.setHeader("Content-disposition", "attachment;filename*=utf-8''" + fileName + ".xlsx");
        EasyExcel.write(response.getOutputStream(), AccountVo.class).sheet("模板").doWrite(DemoData);
    } catch (Exception e) {
        System.out.println("*******导出失败,错误信息:" + e.getMessage());
    }

该处使用的是HttpServletResponse response输出文件流


总结

以上就是Spring Boot 实现Excel简单写的功能,如有不足,请多指正!

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值