前言
在我们日常的开发过程中经常会使用Excel文件的形式来批量地上传下载系统数据,我们最常用的工具是Apache poi,但是如果数据到底上百万时,将会造成内存溢出的问题,那么我们怎么去实现百万数据批量导入导出。
正文
Easyexcel
Easyexcel 是阿里巴巴的开源项目,用来优化Excel文件处理过程:
- poi消耗内存严重:Java解析、生成Excel比较有名的框架有Apache poi、jxl。但他们都存在一个严重的问题就是非常的耗内存,poi有一套SAX模式的API可以一定程度的解决一些内存溢出的问题,但poi还是有一些缺陷,比如07版Excel解压缩以及解压后存储都是在内存中完成的,内存消耗依然很大。
- easyexcel针对内存做出了优化:重写了poi对07版Excel的解析,能够原本一个3M的excel用POI sax依然需要100M左右内存降低到几M,并且再大的excel不会出现内存溢出。
SpringBoot+ EasyExcel实现Excel文件的导入导出
导入依赖
org.projectlombok lombok1.18.2 true com.alibaba easyexcel1.1.2-beat1 com.fasterxml.jackson.core jackson-databindcom.fasterxml.jackson.core jackson-annotationscom.fasterxml.jackson.core jackson-annotations
为了防止Excel文件被破坏在pom.xml添加以下内容
org.apache.maven.plugins maven-resources-pluginxls xlsx
application.propertis:配置文件
#temp files project.tmp.files.path=/Users/mac/Desktop/image/tmp/files/
在SpringBoot启动类添加临时文件设置
@Value("${project.tmp.files.path}")
public String filesPath;
@Bean
MultipartConfigElement multipartConfigElement() {
MultipartConfigFactory factory = new MultipartConfigFactory();
//设置路径xxx
factory.setLocation(filesPath);
return factory.createMultipartConfig();
}
ExcelUtil:Excel工具类
@Slf4j
public class ExcelUtil {
private static Sheet initSheet;
static {
initSheet = new Sheet(1, 0);
initSheet.setSheetName("sheet");
//设置自适应宽度
initSheet.setAutoWidth(Boolean.TRUE);
}
public static void downLoadExcel(String fileName, HttpServletResponse response, Workbook workbook) {
try {
response.setCharacterEncoding("UTF-8");
response.setContentType("application/octet-stream;charset=utf-8");
response.setHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode(fileName, "UTF-8"));
workbook.write(response.getOutputStream());
} catch (IOException e) {
// throw new NormalException(e.getMessage());
}
}
public static List
CommonUtil:工具类
public class CommonUtil {
public static String getUUID() {
String uuid = UUID.randomUUID().toString().trim().replaceAll("-", "");
return uuid.toUpperCase();
}
public static String getTodayStr(){
return new SimpleDateFormat("yyyy-MM-dd").format(new Date()) ;
}
public static String toJson(T t) throws JsonProcessingException {
return OBJECT_MAPPER.get().writevalueAsString(t);
}
}
UserPojoRes:实体类
@Setter
@Getter
@ToString
public class UserPojoRes extends baseRowModel implements Serializable {
private static final long serialVersionUID = -2145503717390503506L;
@ExcelProperty(value = "ID", index = 0)
private String id;
@ExcelProperty(value = "用户名", index = 1)
private String name;
public UserPojoRes(String id, String name) {
this.id = id;
this.name = name;
}
public UserPojoRes(){
}
}
验证
模板下载
这里将模板文件放在resources中
@GetMapping("/exportExcelTempalte")
@ApiOperation(value = "下载导入模板")
public void exportExcelTempalte(HttpServletResponse response) throws Exception {
//Resource目录中的文件
String filePath = "/excels/导入模板.xlsx";
ClassPathResource classPathResource = new ClassPathResource(filePath);
Workbook workbook=WorkbookFactory.create(classPathResource.getInputStream());
ExcelUtil.downLoadExcel("导入模板.xlsx", response, workbook);
}
Excel文件导入
@PostMapping("/importExcel")
@ApiOperation(value = "Excel文件导入")
public Response importExcel(HttpServletRequest request, MultipartFile file, HttpServletResponse response) throws Exception {
List objects = ExcelUtil.readMoreThan1000RowBySheetFromInputStream(file.getInputStream(),null);
List list = new ArrayList<>();
for (Object o : objects) {
UserPojoRes userPojoRes = new UserPojoRes();
List stringList = (List) o;
userPojoRes.setId(stringList.get(0) != null ? stringList.get(0).toString() : "");
userPojoRes.setName(stringList.get(1) != null ? stringList.get(0).toString() : "");
list.add(userPojoRes);
}
String json = CommonUtil.toJson(list);
return new Response(json);
}
Excel文件导出
@Value("${project.tmp.files.path}")
public String filesPath;
@GetMapping("/exportExcel")
@ApiOperation(value = "Excel文件导出")
public void exportExcel(HttpServletResponse response) throws Exception {
//创建临时文件
String path = filesPath + CommonUtil.getUUID() + ".xlsx";
List list = new ArrayList<>();
UserPojoRes userPojoRes = new UserPojoRes("009", "张三");
UserPojoRes userPojoRes1 = new UserPojoRes("009", "李四");
list.add(userPojoRes);
list.add(userPojoRes1);
ExcelUtil.writeWithTemplate(path, list);
// 根据excel创建对象
Workbook workbook = WorkbookFactory.create(new FileInputStream(path));
String fileName = "用户模块" + CommonUtil.getTodayStr() + ".xlsx";
ExcelUtil.downLoadExcel(fileName, response, workbook);
}
到此这篇关于SpringBoot中EasyExcel实现Excel文件的导入导出的文章就介绍到这了,更多相关Java Excel文件导入导出内容请搜索考高分网以前的文章或继续浏览下面的相关文章希望大家以后多多支持考高分网!



