ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

springboot整合easyexcel来导出数据

2022-02-28 23:03:07  阅读:409  来源: 互联网

标签:index springboot excel easyexcel ExcelProperty 导出 value private


easyexcel是阿里巴巴出品的,导出官方文档地址:Alibaba Easy Excel - 简单、省内存的Java解析Excel工具 | 写Excel

下面通过实例来演示导出数据demo

首先新建一个springboot项目(这里不再赘述)

然后在pom里面引入easyExcel的依赖:

<dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>easyexcel</artifactId>
            <version>2.1.1</version>
        </dependency>

然后直接展示业务逻辑的关键代码:

    public void exportData(HttpServletResponse response) {

        // 设置下载信息
        response.setContentType("application/vnd.ms-excel");
        response.setCharacterEncoding("utf-8");
        // URLEncoder.encode可以防止中文乱码
        String fileName= null;
        try {
            fileName = URLEncoder.encode("has测试下载","UTF-8");
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        response.setHeader("Content-disposition","attachment;filename="+fileName+".xlsx");
        // 查询数据信息
        List<User> userList=exportMapper.queryAllData();
        //将User映射为导出的实体类
        List<UserExcel> userExcelList=new ArrayList<>();
        for (User user:userList){
            UserExcel userExcel=new UserExcel();
            BeanUtils.copyProperties(user,userExcel);
            userExcelList.add(userExcel);
        }
        //调用方法进行写操作
        try {
            EasyExcel.write(response.getOutputStream(),UserExcel.class).sheet("名单信息(sheet名称)").doWrite(userExcelList);
        } catch (IOException e) {
            e.printStackTrace();
        }

    }

注意:代码中有一点就是将User转为UserExcel,这是因为在使用easyExcel导出数据的时候需要有一个实体类来和excel中的字段进行映射,由于需要在实体类中设置excel的属性,所以另起一个实体类来封装导出的excel的字段比较合适。

UserExcel:

@Data
public class UserExcel {
    /**
     * index指定在excel中列数
     */
    @ExcelProperty(value = "序号" ,index = 0)
    private int id;

    @ExcelProperty(value = "姓名" ,index = 1)
    private String name;

    @ExcelProperty(value = "年龄" ,index = 2)
    private int age;

    @ExcelProperty(value = "省份" ,index = 3)
    private String province;

    @ExcelProperty(value = "城市" ,index = 4)
    private String city;

    @ExcelProperty(value = "地址" ,index = 5)
    private String address;

    @ExcelProperty(value = "爱好" ,index = 6)
    private String hobby;
}

代码写完启动服务通过浏览器访问地址:http://localhost:8888/exportData

导出的excel的信息如下:

而我数据库中的信息如下所示:

 

可以发现导出的数据是正常的,导出功能完成,相关的整个demo的代码已经上传到csdn,有需要的可以下载,下载地址:演示通过easyExcel来导出excel数据-Java文档类资源-CSDN下载

 

标签:index,springboot,excel,easyexcel,ExcelProperty,导出,value,private
来源: https://blog.csdn.net/royal1235/article/details/123169529

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有