• 欢迎访问搞代码网站,推荐使用最新版火狐浏览器和Chrome浏览器访问本网站!
  • 如果您觉得本站非常有看点,那么赶紧使用Ctrl+D 收藏搞代码吧

SpringBoot+EasyPoi实现excel导出功能

springboot 搞代码 4年前 (2022-01-05) 30次浏览 已收录 0个评论
文章目录[隐藏]

最新小编遇到这样一个需求,根据检索条件查询列表并将结果导出到excel,实现过程也非常简单,感兴趣的朋友跟随小编一起看看吧

在实际项目开发中,对于Excel的导入导出还是很常见的需求,比如说将数据根据模板批量导入到数据库中,以及将数据库中的数据批量导出陈Excel的形式

现有需求: 根据检索条件查询列表并将结果导出到excel

Easypoi文档:https://easypoi.mydoc.io/#text_186900

EasyPoi的主要特点

1.设计精巧,使用简单
2.接口丰富,扩展简单
3.默认值多,write less do more
4.spring mvc支持,web导出可以简单明了

实现过程

1.创建一个Spring Boot项目

快速生成链接:start.spring.io

2.引入EasyPoi的pom依赖

 <!--EasyPoi导入导出--> cn.afterturneasypoi-base4.3.0 cn.afterturneasypoi-web4.3.0 cn.afterturneasypoi-annotation4.3.0
  • easypoi-base 导入导出的工具包,可以完成Excel导出,导入,Word的导出,Excel的导出功能
  • easypoi-web 耦合了spring-mvc 基于AbstractView,极大的简化spring-mvc下的导出功能
  • easypoi-annotation 基础注解包,作用与实体对象上,拆分后方便maven多工程的依赖管理

sax 导入使用xercesImpl这个包(这个包可能造成奇怪的问题哈),word导出使用poi-scratchpad,都作为可选包了

pom.xml中的所有依赖:

  <!-- 导入web支持:SpringMVC开发支持,Servlet相关的程序 --> org.springframework.bootspring-boot-starter-web org.springframework.bootspring-boot-devtoolsruntimetrue org.projectlomboklomboktrue org.springframework.bootspring-boot-starter-testtest<!-- mybatis相关的依赖 --><!--mybatis-plus自动的维护了mybatis以及mybatis-spring的依赖, 在springboot中这三者不能同时的出现,避免版本的冲突,表示:跳进过这个坑--><!--mybatis-plus--> com.baomidoumybatis-plus-boot-starter3.4.3<!--mysql驱动--> mysqlmysql-connector-javaruntime<!-- alibaba的druid数据库连接池 --> com.alibabadruid1.1.20<!-- alibaba的druid数据库连接池 --> com.alibabadruid-spring-boot-starter1.1.20<!--swagger2依赖--> io.springfoxspringfox-swagger22.9.2<!--排除自身的依赖1.5.20版本-->  io.swaggerswagger-models io.springfoxspringfox-swagger-ui2.9.2<!--此版本解决 example="" 造成的 空字符串""无法转成Number 问题--> io.swaggerswagger-models1.5.21<!--EasyPoi导入导出--> cn.afterturneasypoi-base4.3.0 cn.afterturneasypoi-web4.3.0 cn.afterturneasypoi-annotation4.3.0<!--fastjson--> com.alibabafastjson1.2.71

3.编写excel工具类

 package com.example.easypoiexceldemo.utils; import cn.afterturn.easypoi.excel.ExcelExportUtil; import cn.afterturn.easypoi.excel.entity.ExportParams; import cn.afterturn.easypoi.excel.entity.enmus.ExcelType; import org.apache.commons.lang3.StringUtils; import org.apache.poi.ss.usermodel.Workbook; import org.springframework.beans.BeanUtils; import javax.servlet.ServletOutputStream; import javax.servlet.http.HttpServletResponse; import java.io.IOException; import java.net.URLEncoder; import java.text.SimpleDateFormat; import java.util.ArrayList; import java.util.Collection; import java.util.Date; import java.util.List; /** * excel工具类 * @author qzz */ public class ExcelUtils { /** * Excel导出 * * @param response      response * @param fileName      文件名 * @param list          数据List * @param pojoClass     对象Class */ public static void exportExcel(HttpServletResponse response, String fileName, Collection list, Class pojoClass) throws IOException { if (StringUtils.isBlank(fileName)) { //当前日期 SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd"); fileName = df.format(new Date()); } Workbook workbook = ExcelExportUtil.exportExcel(new ExportParams(fileName, fileName, ExcelType.HSSF), pojoClass, list); response.setCharacterEncoding("UTF-8"); response.setHeader("content-Type", "application/vnd.ms-excel"<span style="color:transparent">来源gaodai#ma#com搞*代#码网</span>); response.setHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode(fileName, "UTF-8") + ".xls"); ServletOutputStream out = response.getOutputStream(); workbook.write(out); out.flush(); } /** * Excel导出,先sourceList转换成List,再导出 * * @param response      response * @param fileName      文件名 * @param sourceList    原数据List * @param targetClass   目标对象Class */ public static void exportExcelToTarget(HttpServletResponse response, String fileName, Collection sourceList, Class targetClass) throws Exception { List targetList = new ArrayList(sourceList.size()); for (Object source : sourceList) { Object target = targetClass.newInstance(); BeanUtils.copyProperties(source, target); targetList.add(target); } exportExcel(response, fileName, targetList, targetClass); } /** * Excel导出----设置title---sheetName---要求Collection list是Class pojoClass类型的 * * @param response      response * @param fileName      文件名 * @param list          数据List * @param pojoClass     对象Class */ public static void exportExcel(HttpServletResponse response, String title, String sheetName, String fileName, Collection list, Class pojoClass) throws IOException { if (StringUtils.isBlank(fileName)) { //当前日期 SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd"); fileName = df.format(new Date()); } Workbook workbook = ExcelExportUtil.exportExcel(new ExportParams(title, sheetName, ExcelType.HSSF), pojoClass, list); response.setCharacterEncoding("UTF-8"); response.setHeader("content-Type", "application/vnd.ms-excel"); response.setHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode(fileName, "UTF-8") + ".xls"); ServletOutputStream out = response.getOutputStream(); workbook.write(out); out.flush(); } }

4.在实体类上加注解@Excel

我这边使用了lombok。getter setter和构造方法通过注解 @Data @AllArgsConstructor进行添加,不使用lombok也可手动添加。

要导出的数据可在实体类对应属性上方加**@Excel()注解**。可定义导出列的名称、宽度,以及性别可区分化(一般数据库中存储的性别为1和2),日期格式化等等。

 package com.example.easypoiexceldemo.excel; import cn.afterturn.easypoi.excel.annotation.Excel; import lombok.Data; import java.util.Date; /** * 商品 * @author qzz */ @Data public class ProductExcel { /** * 商品id */ @Excel(name = "商品id") private Integer product_id; /** * 商品标题 */ @Excel(name="商品标题") private String title; /** * 商品副标题 */ @Excel(name="商品副标题") private String sub_title; /** * 商品售价 */ @Excel(name="商品售价") private Double sale_price; /** * 创建者 */ @Excel(name="创建者") private Integer create_by; /** * 创建时间 */ @Excel(name="创建时间", format = "yyyy-MM-dd") private Date create_time; /** * 修改时间 */ @Excel(name="修改时间", format = "yyyy-MM-dd") private Date update_time; /** * 修改者id */ @Excel(name="修改者id") private Integer update_by; }

@Excel 作用到filed上面,是对Excel一列的一个描述

@Excel 的属性介绍:

5.Controller

 /** * excel导出 * @param response */ @GetMapping("/excel") @ApiOperation("根据检索条件查询列表,导出excel") public void export( HttpServletResponse response) throws IOException { //根据条件检索列表 QueryWrapper queryWrapper = new QueryWrapper(); //根据条件检索商品列表 List<Map> list = productService.selectList(queryWrapper); //将List<Map>结果集转换成List List productList = MapToEntity.setList(list,ProductExcel.class); //导出excel ExcelUtils.exportExcel(response,null,productList, ProductExcel.class); }

setList方法为工具类,用于将List<Map>结果集转换成List

MapToEntity工具类:

 package com.example.easypoiexceldemo.utils; import org.apache.commons.lang3.StringUtils; import java.lang.reflect.Field; import java.math.BigDecimal; import java.text.ParseException; import java.text.ParsePosition; import java.text.SimpleDateFormat; import java.util.ArrayList; import java.util.Date; import java.util.List; import java.util.Map; /** * List<Map>到List数据转换 * @author qzz */ public class MapToEntity { /** * List<Map> 到 List 数据转换 */ public static  List setList(final List<Map> srcList, Class clazz) { List list = new ArrayList(); for (int i=0;i<srcList.size();i++){ try { T t = clazz.newInstance(); Field[] fields = t.getClass().getDeclaredFields(); for (Field field : fields) { if (!"serialVersionUID".equals(field.getName())) { //设置对象的访问权限,保证对private的属性的访问 field.setAccessible(true); //读取配置转换字段名,并从map中取出数据 Object v = srcList.get(i).get(field.getName()); field.set(t, convert(v, field.getType())); } } list.add(t); } catch (Exception ex) { ex.toString(); } }; return list; } /** * 字段类型转换 */ private static  T convert(Object obj, Class type) throws ParseException { if (obj != null && StringUtils.isNotBlank(obj.toString())) { if (type.equals(String.class)) { return (T) obj.toString(); } else if (type.equals(BigDecimal.class)) { return (T) new BigDecimal(obj.toString()); }else if(type.equals(Double.class)){ return (T) Double.valueOf(obj.toString()); }else if(type.equals(Integer.class)){ return (T) Integer.valueOf(obj.toString()); }else if(type.equals(Date.class)){ if(obj!=null){ String timeStr = String.valueOf(obj); String s[] = timeStr.split("T"); SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); return (T) sdf.parse(s[0]+" "+s[1]); }else{ return null; } } else{ //其他类型转换 return (T) obj.toString(); } } return null; } }

6.启动项目,进行测试

项目启动成功后,在浏览器中输入 http://localhost:8083/product/excel,进行访问:

打开导出的excel文档:

以上就是SpringBoot+EasyPoi实现excel导出功能的详细内容,更多请关注gaodaima搞代码网其它相关文章!


搞代码网(gaodaima.com)提供的所有资源部分来自互联网,如果有侵犯您的版权或其他权益,请说明详细缘由并提供版权或权益证明然后发送到邮箱[email protected],我们会在看到邮件的第一时间内为您处理,或直接联系QQ:872152909。本网站采用BY-NC-SA协议进行授权
转载请注明原文链接:SpringBoot+EasyPoi实现excel导出功能

喜欢 (0)
[搞代码]
分享 (0)
发表我的评论
取消评论

表情 贴图 加粗 删除线 居中 斜体 签到

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址