springboot集成easypoi实现excel多sheet导入
-
pom依赖
<!--easypoi依赖,excel导入导出--> <dependency> <groupId>cn.afterturn</groupId> <artifactId>easypoi-spring-boot-starter</artifactId> <version>4.3.0</version> </dependency>
-
定义导入实体类
/** * excel导入user参数 * * @author luoYong * @version 1.0 * @date 2022/2/15 14:34 */ @Data public class ImportUser implements Serializable { private static final long serialVersionUID = 1L; /** * @Excel 作用在一个filed上面,对列的描述 * @param name 列名 * @param orderNum 下标,从0开始。 */ @Excel(name = "姓名", orderNum = "0",width = 10.0) private String name; @Excel(name = "年龄", orderNum = "1",width = 10.0) private Integer age; @Excel(name = "性别", orderNum = "2",width = 5.0) private String sex; @Excel(name = "地址", orderNum = "3",width = 30.0) private String address; @Excel(name = "用户描述", orderNum = "4",width = 20.0) private String describes; }
-
Controller添加excel导入方法
/** * excel多sheet导入 */ @GetMapping("/import-for-sheets") @ApiOperation(value = "excel多sheet导入", notes = "excel多sheet导入") public void importForSheetUsers(@RequestParam("file") MultipartFile file) throws IOException { userService.importForSheetUsers(file); }
-
定义导入接口
/** * excel多sheet导入 */ void importForSheetUsers(MultipartFile file) throws IOException;
-
实现导入方法(核心)
package com.example.demo.util; import cn.afterturn.easypoi.excel.ExcelImportUtil; import cn.afterturn.easypoi.excel.entity.ImportParams; import java.io.InputStream;import java.util.List; import java.util.NoSuchElementException; /** * @author luoYong * @version 1.0 * @date 2022/2/21 10:40 */ public class EasyPoiUtils { /** * 功能描述:根据接收的Excel文件来导入多个sheet,根据索引可返回一个集合 * * @param inputStream excel输入流 * @param sheetIndex 导入sheet索引 * @param titleRows 表标题的行数 * @param headerRows 表头行数 * @param pojoClass Excel实体类 */ public static <T> List<T> importExcel(InputStream inputStream, int sheetIndex, Integer titleRows, Integer headerRows, Class<T> pojoClass) { // 根据file得到Workbook,主要是要根据这个对象获取,传过来的excel有几个sheet页 ImportParams params = new ImportParams(); // 第几个sheet表页 params.setStartSheetIndex(sheetIndex); //设置表标题行数 params.setTitleRows(titleRows); //设置表头行数 params.setHeadRows(headerRows); List<T> list = null; try { //读取的excel数据集合 list = ExcelImportUtil.importExcel(inputStream, pojoClass, params); } catch (NoSuchElementException e) { throw new RuntimeException("模板不能为空"); } catch (Exception e) { e.printStackTrace(); } return list; } }
-
导入实现接口
/** * excel多sheet导入 */ @Override public void importForSheetUsers(MultipartFile file) throws IOException { //分批进行读取excel的sheet工作表 //读取第一个sheet表 List<ImportUser> sheetOneUsers = EasyPoiUtils.importExcel(file.getInputStream(), 0, 1, 1, ImportUser.class); //读取第二个sheet表 List<ImportUser> sheetTwoUsers = EasyPoiUtils.importExcel(file.getInputStream(), 1, 1, 1, ImportUser.class); //批量插入 this.saveUsers(sheetOneUsers); this.saveUsers(sheetTwoUsers); } /** * 批量插入用户数据 */ private boolean saveUsers(List<ImportUser> users) { //存放user List<UserEntity> userList = new ArrayList<>(); //转成user实体 for (ImportUser user : users) { //验空 if (user != null) { UserEntity userEntity = new UserEntity(user); userList.add(userEntity); } } //批量插入 return this.saveBatch(userList); }
-
postman测试接口
-
结果展示