当前位置:   article > 正文

SpringBoot实现Excel导入导出,简单好用_springboot 导出excel

springboot 导出excel

EasyPoi简介

POI是Java操作MicroOffice(如对Excel的导入导出)的一个插件。POI的全称是(Poor Obfuscation Implementation),POI官网地址是 http://poi.achache.org/index.html 。

EasyPoi对POI进行了优化,更加设计精巧,使用简单,接口丰富,扩展简单。EasyPOI的同类产品有Execel4J,Hutools等。EasyPoi官网地址是 https://gitee.com/lemur/easypoi

用惯了SpringBoot的朋友估计会想到,有没有什么办法可以直接定义好需要导出的数据对象,然后添加几个注解,直接自动实现Excel导入导出功能?

EasyPoi正是这么一款工具,如果你不太熟悉POI,想简单地实现Excel操作,用它就对了!

EasyPoi的目标不是替代POI,而是让一个不懂导入导出的人也能快速使用POI完成Excel的各种操作,而不是看很多API才可以完成这样的工作。

Springboot集成EasyPoi

在SpringBoot中集成EasyPoi非常简单,只需添加一个依赖即可,springboot会通过自己的自动装配功能将这个第三方组件加载到spring容器进行管理。

这里贴出完整的pom.xml,读者直接拷贝过去使用即可。 

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  3. xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
  4. <modelVersion>4.0.0</modelVersion>
  5. <parent>
  6. <groupId>org.springframework.boot</groupId>
  7. <artifactId>spring-boot-starter-parent</artifactId>
  8. <version>2.2.5.RELEASE</version>
  9. <relativePath/> <!-- lookup parent from repository -->
  10. </parent>
  11. <groupId>com.example</groupId>
  12. <artifactId>springboot-export</artifactId>
  13. <version>0.0.1-SNAPSHOT</version>
  14. <name>springboot-export</name>
  15. <description>springboot-export</description>
  16. <properties>
  17. <java.version>1.8</java.version>
  18. </properties>
  19. <dependencies>
  20. <dependency>
  21. <groupId>org.springframework.boot</groupId>
  22. <artifactId>spring-boot-starter-web</artifactId>
  23. </dependency>
  24. <dependency>
  25. <groupId>org.springframework.boot</groupId>
  26. <artifactId>spring-boot-starter-test</artifactId>
  27. <scope>test</scope>
  28. </dependency>
  29. <dependency>
  30. <groupId>cn.afterturn</groupId>
  31. <artifactId>easypoi-spring-boot-starter</artifactId>
  32. <version>4.4.0</version>
  33. </dependency>
  34. <dependency>
  35. <groupId>org.projectlombok</groupId>
  36. <artifactId>lombok</artifactId>
  37. <version>RELEASE</version>
  38. <scope>compile</scope>
  39. </dependency>
  40. </dependencies>
  41. <build>
  42. <plugins>
  43. <plugin>
  44. <groupId>org.springframework.boot</groupId>
  45. <artifactId>spring-boot-maven-plugin</artifactId>
  46. </plugin>
  47. </plugins>
  48. </build>
  49. </project>

创建一个用户对象User,封装用户信息:

  1. User中不加@Excel注解的字段,不会被导出到excel表格中
  2. BigDecimal, Date等类型的属性,可以直接导出到excel表格中
  3. 对于以Integer类型表达的性别,可以将其转为汉字,有对应的处理方式
  1. package com.example.springbootexport.model;
  2. import cn.afterturn.easypoi.excel.annotation.Excel;
  3. import lombok.Data;
  4. import lombok.EqualsAndHashCode;
  5. import java.math.BigDecimal;
  6. import java.util.Date;
  7. @Data
  8. @EqualsAndHashCode(callSuper = false)
  9. public class User {
  10. @Excel(name = "ID", width = 10)
  11. private Long id;
  12. @Excel(name = "用户名", width = 20, needMerge = true)
  13. private String username;
  14. private String password;
  15. @Excel(name = "昵称", width = 20, needMerge = true)
  16. private String nickname;
  17. @Excel(name = "出生日期", width = 20, format = "yyyy-MM-dd")
  18. private Date birthday;
  19. @Excel(name = "手机号", width = 20, needMerge = true, desensitizationRule = "3_4")
  20. private String phone;
  21. private String icon;
  22. @Excel(name = "性别", width = 10, replace = {"男_0", "女_1"})
  23. private Integer gender;
  24. @Excel(name = "合同金额")
  25. private BigDecimal totalAmount;
  26. private String notExport;
  27. }
  • 在此我们就可以看到EasyPoi的核心注解@Excel,通过在对象上添加@Excel注解,可以将对象信息直接导出到Excel中去,下面对注解中的属性做个介绍;
    • name:Excel中的列名;
    • width:指定列的宽度;
    • needMerge:是否需要纵向合并单元格;
    • format:当属性为时间类型时,设置时间的导出导出格式;
    • desensitizationRule:数据脱敏处理,3_4表示只显示字符串的前3位和后4位,其他为*号;
    • replace:对属性进行替换;
    • suffix:对数据添加后缀。

创建一个Controller,用于导出会员列表到Excel,具体代码如下

  1. package com.example.springbootexport.controller;
  2. import cn.afterturn.easypoi.entity.vo.NormalExcelConstants;
  3. import cn.afterturn.easypoi.excel.entity.ExportParams;
  4. import cn.afterturn.easypoi.excel.entity.enmus.ExcelType;
  5. import cn.afterturn.easypoi.view.PoiBaseView;
  6. import com.example.springbootexport.model.User;
  7. import org.springframework.stereotype.Controller;
  8. import org.springframework.ui.ModelMap;
  9. import org.springframework.web.bind.annotation.RequestMapping;
  10. import org.springframework.web.bind.annotation.RequestMethod;
  11. import javax.servlet.http.HttpServletRequest;
  12. import javax.servlet.http.HttpServletResponse;
  13. import java.math.BigDecimal;
  14. import java.util.ArrayList;
  15. import java.util.Date;
  16. import java.util.List;
  17. @Controller
  18. @RequestMapping("/easyPoi")
  19. public class EasyPoiController {
  20. @RequestMapping(value = "/exportMemberList", method = RequestMethod.GET)
  21. public void exportMemberList(ModelMap map,
  22. HttpServletRequest request,
  23. HttpServletResponse response) {
  24. List<User> memberList = new ArrayList<>();
  25. User user1 = new User();
  26. user1.setBirthday(new Date());
  27. user1.setGender(1);
  28. user1.setIcon("png");
  29. user1.setId(1000L);
  30. user1.setNickname("bruce");
  31. user1.setPassword("123456");
  32. user1.setPhone("12365789018");
  33. user1.setUsername("bruce wang");
  34. user1.setNotExport("不导出");
  35. user1.setTotalAmount(BigDecimal.valueOf(10.50));
  36. User user2 = new User();
  37. user2.setBirthday(new Date());
  38. user2.setGender(0);
  39. user2.setIcon("jpf");
  40. user2.setId(2000L);
  41. user2.setNickname("bruce1");
  42. user2.setPassword("1234567890");
  43. user2.setPhone("12365789089");
  44. user2.setUsername("bruce-wang");
  45. user2.setNotExport("不导出");
  46. user2.setTotalAmount(BigDecimal.valueOf(11.50));
  47. memberList.add(user1);
  48. memberList.add(user2);
  49. ExportParams params = new ExportParams("会员列表", "会员列表", ExcelType.XSSF);
  50. map.put(NormalExcelConstants.DATA_LIST, memberList);
  51. map.put(NormalExcelConstants.CLASS, User.class);
  52. map.put(NormalExcelConstants.PARAMS, params);
  53. map.put(NormalExcelConstants.FILE_NAME, "memberList");
  54. PoiBaseView.render(map, request, response, NormalExcelConstants.EASYPOI_EXCEL_VIEW);
  55. }
  56. }

整个代码结构如下:

启动程序,调用接口导出excel表格:

http://localhost:8083/easyPoi/exportMemberList

导出的excel表格如下:

 

参考地址:

SpringBoot实现Excel导入导出,好用到爆,POI可以扔掉了! - 腾讯云开发者社区-腾讯云 (tencent.com) 

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/繁依Fanyi0/article/detail/321479
推荐阅读
相关标签
  

闽ICP备14008679号