批量上传数据导入、数据统计分析导出,已经基本是系统必不可缺的一项功能,这里从性能和易用性方面考虑,集成EasyExcel。EasyExcel是一个基于Java的简单、省内存的读写Excel的开源项目,在尽可能节约内存的情况下支持读写百M的Excel:

  Java解析、生成Excel比较有名的框架有Apache poi、jxl。但他们都存在一个严重的问题就是非常的耗内存,poi有一套SAX模式的API可以一定程度的解决一些内存溢出的问题,但POI还是有一些缺陷,比如07版Excel解压缩以及解压后存储都是在内存中完成的,内存消耗依然很大。easyexcel重写了poi对07版Excel的解析,一个3M的excel用POI sax解析依然需要100M左右内存,改用easyexcel可以降低到几M,并且再大的excel也不会出现内存溢出;03版依赖POI的sax模式,在上层做了模型转换的封装,让使用者更加简单方便。(https://github.com/alibaba/easyexcel/)

一、引入依赖的库

1、在GitEgg-Platform项目中修改gitegg-platform-bom工程的pom.xml文件,增加EasyExcel的Maven依赖。

    <properties>
......
<!-- Excel 数据导入导出 -->
<easyexcel.version>2.2.10</easyexcel.version>
</properties> <dependencymanagement>
<dependencies>
......
<!-- Excel 数据导入导出 -->
<dependency>
<groupid>com.alibaba</groupid>
<artifactid>easyexcel</artifactid>
<version>${easyexcel.version}</version>
</dependency>
......
</dependencies>
</dependencymanagement>

2、修改gitegg-platform-boot工程的pom.xml文件,添加EasyExcel依赖。这里考虑到数据导入导出是系统必备功能,所有引用springboot工程的微服务都需要用到EasyExcel,并且目前版本EasyExcel不支持LocalDateTime日期格式,这里需要自定义LocalDateTimeConverter转换器,用于在数据导入导出时支持LocalDateTime。

pom.xml文件

    <dependencies>
......
<!-- Excel 数据导入导出 -->
<dependency>
<groupid>com.alibaba</groupid>
<artifactid>easyexcel</artifactid>
</dependency>
</dependencies>

自定义LocalDateTime转换器LocalDateTimeConverter.java

package com.gitegg.platform.boot.excel;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Objects; import com.alibaba.excel.annotation.format.DateTimeFormat;
import com.alibaba.excel.converters.Converter;
import com.alibaba.excel.enums.CellDataTypeEnum;
import com.alibaba.excel.metadata.CellData;
import com.alibaba.excel.metadata.GlobalConfiguration;
import com.alibaba.excel.metadata.property.ExcelContentProperty; /**
* 自定义LocalDateStringConverter
* 用于解决使用easyexcel导出表格时候,默认不支持LocalDateTime日期格式
*
* @author GitEgg
*/ public class LocalDateTimeConverter implements Converter<localdatetime> { /**
* 不使用{@code @DateTimeFormat}注解指定日期格式时,默认会使用该格式.
*/
private static final String DEFAULT_PATTERN = "yyyy-MM-dd HH:mm:ss";
@Override
public Class supportJavaTypeKey() {
return LocalDateTime.class;
} @Override
public CellDataTypeEnum supportExcelTypeKey() {
return CellDataTypeEnum.STRING;
} /**
* 这里读的时候会调用
*
* @param cellData excel数据 (NotNull)
* @param contentProperty excel属性 (Nullable)
* @param globalConfiguration 全局配置 (NotNull)
* @return 读取到内存中的数据
*/
@Override
public LocalDateTime convertToJavaData(CellData cellData, ExcelContentProperty contentProperty, GlobalConfiguration globalConfiguration) {
DateTimeFormat annotation = contentProperty.getField().getAnnotation(DateTimeFormat.class);
return LocalDateTime.parse(cellData.getStringValue(),
DateTimeFormatter.ofPattern(Objects.nonNull(annotation) ? annotation.value() : DEFAULT_PATTERN));
} /**
* 写的时候会调用
*
* @param value java value (NotNull)
* @param contentProperty excel属性 (Nullable)
* @param globalConfiguration 全局配置 (NotNull)
* @return 写出到excel文件的数据
*/
@Override
public CellData convertToExcelData(LocalDateTime value, ExcelContentProperty contentProperty, GlobalConfiguration globalConfiguration) {
DateTimeFormat annotation = contentProperty.getField().getAnnotation(DateTimeFormat.class);
return new CellData(value.format(DateTimeFormatter.ofPattern(Objects.nonNull(annotation) ? annotation.value() : DEFAULT_PATTERN)));
}
}

以上依赖及转换器编辑好之后,点击Platform的install,将依赖重新安装到本地库,然后GitEgg-Cloud就可以使用定义的依赖和转换器了。

二、业务实现及测试

因为依赖的库及转换器都是放到gitegg-platform-boot工程下的,所以,所有使用到gitegg-platform-boot的都可以直接使用EasyExcel的相关功能,在GitEgg-Cloud项目下重新Reload All Maven Projects。这里以gitegg-code-generator微服务项目举例说明数据导入导出的用法。

1、EasyExcel可以根据实体类的注解来进行Excel的读取和生成,在entity目录下新建数据导入和导出的实体类模板文件。

文件导入的实体类模板DatasourceImport.java

package com.gitegg.code.generator.datasource.entity;

import com.alibaba.excel.annotation.ExcelProperty;
import com.alibaba.excel.annotation.write.style.ColumnWidth;
import com.alibaba.excel.annotation.write.style.ContentRowHeight;
import com.alibaba.excel.annotation.write.style.HeadRowHeight;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data; /**
* <p>
* 数据源配置上传
* </p>
*
* @author GitEgg
* @since 2021-08-18 16:39:49
*/
@Data
@HeadRowHeight(20)
@ContentRowHeight(15)
@ApiModel(value="DatasourceImport对象", description="数据源配置导入")
public class DatasourceImport { @ApiModelProperty(value = "数据源名称")
@ExcelProperty(value = "数据源名称" ,index = 0)
@ColumnWidth(20)
private String datasourceName; @ApiModelProperty(value = "连接地址")
@ExcelProperty(value = "连接地址" ,index = 1)
@ColumnWidth(20)
private String url; @ApiModelProperty(value = "用户名")
@ExcelProperty(value = "用户名" ,index = 2)
@ColumnWidth(20)
private String username; @ApiModelProperty(value = "密码")
@ExcelProperty(value = "密码" ,index = 3)
@ColumnWidth(20)
private String password; @ApiModelProperty(value = "数据库驱动")
@ExcelProperty(value = "数据库驱动" ,index = 4)
@ColumnWidth(20)
private String driver; @ApiModelProperty(value = "数据库类型")
@ExcelProperty(value = "数据库类型" ,index = 5)
@ColumnWidth(20)
private String dbType; @ApiModelProperty(value = "备注")
@ExcelProperty(value = "备注" ,index = 6)
@ColumnWidth(20)
private String comments; }

文件导出的实体类模板DatasourceExport.java

package com.gitegg.code.generator.datasource.entity;

import com.alibaba.excel.annotation.ExcelProperty;
import com.alibaba.excel.annotation.format.DateTimeFormat;
import com.alibaba.excel.annotation.write.style.ColumnWidth;
import com.alibaba.excel.annotation.write.style.ContentRowHeight;
import com.alibaba.excel.annotation.write.style.HeadRowHeight;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import com.gitegg.platform.boot.excel.LocalDateTimeConverter;
import lombok.Data; import java.time.LocalDateTime; /**
* <p>
* 数据源配置下载
* </p>
*
* @author GitEgg
* @since 2021-08-18 16:39:49
*/
@Data
@HeadRowHeight(20)
@ContentRowHeight(15)
@ApiModel(value="DatasourceExport对象", description="数据源配置导出")
public class DatasourceExport { @ApiModelProperty(value = "主键")
@ExcelProperty(value = "序号" ,index = 0)
@ColumnWidth(15)
private Long id; @ApiModelProperty(value = "数据源名称")
@ExcelProperty(value = "数据源名称" ,index = 1)
@ColumnWidth(20)
private String datasourceName; @ApiModelProperty(value = "连接地址")
@ExcelProperty(value = "连接地址" ,index = 2)
@ColumnWidth(20)
private String url; @ApiModelProperty(value = "用户名")
@ExcelProperty(value = "用户名" ,index = 3)
@ColumnWidth(20)
private String username; @ApiModelProperty(value = "密码")
@ExcelProperty(value = "密码" ,index = 4)
@ColumnWidth(20)
private String password; @ApiModelProperty(value = "数据库驱动")
@ExcelProperty(value = "数据库驱动" ,index = 5)
@ColumnWidth(20)
private String driver; @ApiModelProperty(value = "数据库类型")
@ExcelProperty(value = "数据库类型" ,index = 6)
@ColumnWidth(20)
private String dbType; @ApiModelProperty(value = "备注")
@ExcelProperty(value = "备注" ,index = 7)
@ColumnWidth(20)
private String comments; @ApiModelProperty(value = "创建日期")
@ExcelProperty(value = "创建日期" ,index = 8, converter = LocalDateTimeConverter.class)
@ColumnWidth(22)
@DateTimeFormat("yyyy-MM-dd HH:mm:ss")
private LocalDateTime createTime; }

2、在DatasourceController中新建上传和下载方法:

    /**
* 批量导出数据
* @param response
* @param queryDatasourceDTO
* @throws IOException
*/
@GetMapping("/download")
public void download(HttpServletResponse response, QueryDatasourceDTO queryDatasourceDTO) throws IOException {
response.setContentType("application/vnd.ms-excel");
response.setCharacterEncoding("utf-8");
// 这里URLEncoder.encode可以防止中文乱码 当然和easyexcel没有关系
String fileName = URLEncoder.encode("数据源列表", "UTF-8").replaceAll("\\+", "%20");
response.setHeader("Content-disposition", "attachment;filename*=utf-8''" + fileName + ".xlsx");
List<datasourcedto> dataSourceList = datasourceService.queryDatasourceList(queryDatasourceDTO);
List<datasourceexport> dataSourceExportList = new ArrayList<>();
for (DatasourceDTO datasourceDTO : dataSourceList) {
DatasourceExport dataSourceExport = BeanCopierUtils.copyByClass(datasourceDTO, DatasourceExport.class);
dataSourceExportList.add(dataSourceExport);
}
String sheetName = "数据源列表";
EasyExcel.write(response.getOutputStream(), DatasourceExport.class).sheet(sheetName).doWrite(dataSourceExportList);
} /**
* 下载导入模板
* @param response
* @throws IOException
*/
@GetMapping("/download/template")
public void downloadTemplate(HttpServletResponse response) throws IOException {
response.setContentType("application/vnd.ms-excel");
response.setCharacterEncoding("utf-8");
// 这里URLEncoder.encode可以防止中文乱码 当然和easyexcel没有关系
String fileName = URLEncoder.encode("数据源导入模板", "UTF-8").replaceAll("\\+", "%20");
response.setHeader("Content-disposition", "attachment;filename*=utf-8''" + fileName + ".xlsx");
String sheetName = "数据源列表";
EasyExcel.write(response.getOutputStream(), DatasourceImport.class).sheet(sheetName).doWrite(null);
} /**
* 上传数据
* @param file
* @return
* @throws IOException
*/
@PostMapping("/upload")
public Result<!--?--> upload(@RequestParam("uploadFile") MultipartFile file) throws IOException {
List<datasourceimport> datasourceImportList = EasyExcel.read(file.getInputStream(), DatasourceImport.class, null).sheet().doReadSync();
if (!CollectionUtils.isEmpty(datasourceImportList))
{
List<datasource> datasourceList = new ArrayList<>();
datasourceImportList.stream().forEach(datasourceImport-> {
datasourceList.add(BeanCopierUtils.copyByClass(datasourceImport, Datasource.class));
});
datasourceService.saveBatch(datasourceList);
}
return Result.success();
}

3、前端导出(下载)设置,我们前端框架请求用的是axios,正常情况下,普通的请求成功或失败返回的responseType为json格式,当我们下载文件时,请求返回的是文件流,这里需要设置下载请求的responseType为blob。考虑到下载是一个通用的功能,这里提取出下载方法为一个公共方法:首先是判断服务端的返回格式,当一个下载请求返回的是json格式时,那么说明这个请求失败,需要处理错误新题并提示,如果不是,那么走正常的文件流下载流程。

api请求

//请求的responseType设置为blob格式
export function downloadDatasourceList (query) {
return request({
url: '/gitegg-plugin-code/code/generator/datasource/download',
method: 'get',
responseType: 'blob',
params: query
})
}

导出/下载的公共方法

// 处理请求返回信息
export function handleDownloadBlod (fileName, response) {
const res = response.data
if (res.type === 'application/json') {
const reader = new FileReader()
reader.readAsText(response.data, 'utf-8')
reader.onload = function () {
const { msg } = JSON.parse(reader.result)
notification.error({
message: '下载失败',
description: msg
})
}
} else {
exportBlod(fileName, res)
}
} // 导出Excel
export function exportBlod (fileName, data) {
const blob = new Blob([data])
const elink = document.createElement('a')
elink.download = fileName
elink.style.display = 'none'
elink.href = URL.createObjectURL(blob)
document.body.appendChild(elink)
elink.click()
URL.revokeObjectURL(elink.href)
document.body.removeChild(elink)
}

vue页面调用

 handleDownload () {
this.downloadLoading = true
downloadDatasourceList(this.listQuery).then(response => {
handleDownloadBlod('数据源配置列表.xlsx', response)
this.listLoading = false
})
},

4、前端导入(上传的设置),前端无论是Ant Design of Vue框架还是ElementUI框架都提供了上传组件,用法都是一样的,在上传之前需要组装FormData数据,除了上传的文件,还可以自定义传到后台的参数。

上传组件

      <a-upload name="uploadFile" :show-upload-list="false" :before-upload="beforeUpload">
<a-button> <a-icon type="upload"> 导入 </a-icon></a-button>
</a-upload>

上传方法

 beforeUpload (file) {
this.handleUpload(file)
return false
},
handleUpload (file) {
this.uploadedFileName = ''
const formData = new FormData()
formData.append('uploadFile', file)
this.uploading = true
uploadDatasource(formData).then(() => {
this.uploading = false
this.$message.success('数据导入成功')
this.handleFilter()
}).catch(err => {
console.log('uploading', err)
this.$message.error('数据导入失败')
})
},

以上步骤,就把EasyExcel整合完成,基本的数据导入导出功能已经实现,在业务开发过程中,可能会用到复杂的Excel导出,比如包含图片、图表等的Excel导出,这一块需要根据具体业务需要,参考EasyExcel的详细用法来定制自己的导出方法。

源码地址:

Gitee: https://gitee.com/wmz1930/GitEgg

GitHub: https://github.com/wmz1930/GitEgg

SpringCloud微服务实战——搭建企业级开发框架(三十):整合EasyExcel实现数据表格导入导出功能的更多相关文章

  1. SpringCloud微服务实战——搭建企业级开发框架(十二):OpenFeign+Ribbon实现负载均衡

      Ribbon是Netflix下的负载均衡项目,它主要实现中间层应用程序的负载均衡.为Ribbon配置服务提供者地址列表后,Ribbon就会基于某种负载均衡算法,自动帮助服务调用者去请求.Ribbo ...

  2. SpringCloud微服务实战——搭建企业级开发框架(十五):集成Sentinel高可用流量管理框架【熔断降级】

      Sentinel除了流量控制以外,对调用链路中不稳定的资源进行熔断降级也是保障高可用的重要措施之一.由于调用关系的复杂性,如果调用链路中的某个资源不稳定,最终会导致请求发生堆积.Sentinel ...

  3. SpringCloud微服务实战——搭建企业级开发框架(十):使用Nacos分布式配置中心

    随着业务的发展.微服务架构的升级,服务的数量.程序的配置日益增多(各种微服务.各种服务器地址.各种参数),传统的配置文件方式和数据库的方式已无法满足开发人员对配置管理的要求: 安全性:配置跟随源代码保 ...

  4. SpringCloud微服务实战——搭建企业级开发框架(十四):集成Sentinel高可用流量管理框架【限流】

      Sentinel 是面向分布式服务架构的高可用流量防护组件,主要以流量为切入点,从限流.流量整形.熔断降级.系统负载保护.热点防护等多个维度来帮助开发者保障微服务的稳定性. Sentinel 具有 ...

  5. SpringCloud微服务实战——搭建企业级开发框架(十六):集成Sentinel高可用流量管理框架【自定义返回消息】

    Sentinel限流之后,默认的响应消息为Blocked by Sentinel (flow limiting),对于系统整体功能提示来说并不统一,参考我们前面设置的统一响应及异常处理方式,返回相同的 ...

  6. SpringCloud微服务实战——搭建企业级开发框架(十九):Gateway使用knife4j聚合微服务文档

      本章介绍Spring Cloud Gateway网关如何集成knife4j,通过网关聚合所有的Swagger微服务文档 1.gitegg-gateway中引入knife4j依赖,如果没有后端代码编 ...

  7. SpringCloud微服务实战——搭建企业级开发框架(三十七):微服务日志系统设计与实现

      针对业务开发人员通常面对的业务需求,我们将日志分为操作(请求)日志和系统运行日志,操作(请求)日志可以让管理员或者运营人员方便简单的在系统界面中查询追踪用户具体做了哪些操作,便于分析统计用户行为: ...

  8. SpringCloud微服务实战——搭建企业级开发框架(三十八):搭建ELK日志采集与分析系统

      一套好的日志分析系统可以详细记录系统的运行情况,方便我们定位分析系统性能瓶颈.查找定位系统问题.上一篇说明了日志的多种业务场景以及日志记录的实现方式,那么日志记录下来,相关人员就需要对日志数据进行 ...

  9. SpringCloud微服务实战——搭建企业级开发框架(三十一):自定义MybatisPlus代码生成器实现前后端代码自动生成

      理想的情况下,代码生成可以节省很多重复且没有技术含量的工作量,并且代码生成可以按照统一的代码规范和格式来生成代码,给日常的代码开发提供很大的帮助.但是,代码生成也有其局限性,当牵涉到复杂的业务逻辑 ...

  10. SpringCloud微服务实战——搭建企业级开发框架(三十二):代码生成器使用配置说明

    一.新建数据源配置 因考虑到多数据源问题,代码生成器作为一个通用的模块,后续可能会为其他工程生成代码,所以,这里不直接读取系统工程配置的数据源,而是让用户自己维护. 参数说明 数据源名称:用于查找区分 ...

随机推荐

  1. 矩阵n次幂的计算

    1.归纳法 两大数学归纳法 题目一 2.递推关系 题目一 题目二 3.方阵 题目一 4.矩阵对角化(重点) 题目一 题目二 题目三 题目四 5.矩阵性质(综合) 题目一 题目二 对于副对角线: 题目三

  2. Android 服务名称规则invalid service name 限制16字符以内

    今天调试网络服务的时候为了区分,修改了原有服务名称,同时新增了两个服务. 系统运行的时候报错找不到对应的服务 init: no such service 'wpa_supplicant_common' ...

  3. F. Mattress Run 题解

    F. Mattress Run 挺好的一道题,对于DP的本质的理解有很大的帮助. 首先要想到的就是将这个拆成两个题,一个dp光求获得足够的夜晚的最小代价,一个dp光求获得足够的停留的最小代价. 显然由 ...

  4. i love dingning

    "如果你爱一个人,不是下课给人家买买水,不是短信发来发去,也不是周末一起出来唱唱歌聊聊天吃吃饭,而是做一个出色的人.以后的以后,可能还有别的人爱她,你要做的,是把别人都比下去.你要变得优秀, ...

  5. 【Python+postman接口自动化测试】(3)什么是接口测试?

    什么是接口测试? 接口测试是测试系统组件间接口的一种测试.接口测试主要用于检测外部系统与系统之间以及内部各个子系统之间的交互点.测试的重点是要检查数据的交换.传递和控制管理过程,以及系统间的相互逻辑依 ...

  6. JMeter学习笔记--录制脚本(二)

    第一步:在JMeter中添加线程组,命名为访问首页 第二步:在线程组下添加HTTP请求默认值 添加->配置元件->HTTP请求默认值,设置服务器IP和端口号(JMeter默认使用80端口号 ...

  7. Java——去掉小数点后面多余的0

    当小数点后位数过多,多余的0没有实际意义,根据业务需求需要去掉多余的0.后端存储浮点型数据一般会用到Bigdecimal 类型,可以调用相关方法去掉小数后多余0,然后转为string. public ...

  8. 组件通过props属性传值

    组件之间的传值 组件是一个单独功能模块的封装,有属于自己的data和methods,一个组件的 data 选项必须是一个函数 为什么必须是函数:因为只有当data是函数时,不同实例调用同一个组件时才会 ...

  9. vscode 导入第三方jar包(添加外部JAR)

    添加 jar包 至根目录下lib文件夹,在 .classpath 文件内添加 jar 路径. 注意:新添加的 jar路径 在"src"和"bin"之间,否则无法 ...

  10. Java学习(十一)

    今天学习了this和static关键字,这两个都是c++中学过的,但讲师还是讲了2个小时... 学得东西大部分都知道吧. this是当前对象的地址,类中带有static的方法不能使用this. 类中带 ...