Easyexcel(2-文件读取)

server/2024/11/23 22:41:18/

相关文章链接:

  1. Easyexcel(1-注解使用)
  2. Easyexcel(2-文件读取)

同步读取

读取单个Sheet

  1. 通过sheet方法指定对应的Sheet名称或下标读取文件信息
  2. 通过doReadSync方法实现同步读取
java">@Data
public class UserExcel {@ExcelIgnoreprivate Integer id;@ExcelProperty(index = 0, value = "姓名")private String name;@ExcelProperty(index = 1, value = "年龄")private Integer age;@DateTimeFormat(value = "yyyy-MM-dd")@ExcelProperty(index = 2, value = "出生日期")private Date birthday;
}
java">@RestController
public class Test02Controller {/*** 上传单个文件, 同步读取excel文件*/@PostMapping("/uploadFile")public void uploadFile(MultipartFile file) {try (InputStream in = file.getInputStream()) {List<UserExcel> userExcelList = EasyExcel.read(in)// 读取第一个sheet.sheet(0)// 如果第一行才是标题,第二行是数据,从第二行开始读取.headRowNumber(1).head(UserExcel.class).doReadSync();for (UserExcel userExcel : userExcelList) {System.out.println(userExcel);}} catch (Exception e) {e.printStackTrace();}}
}

读取多个Sheet(同一个对象)

使用doReadAllSync方法读取所有Sheet,适用于每个Sheet的对象都一致的情况

java">@PostMapping("/uploadFile2")
public void uploadFile2(MultipartFile file) {try (InputStream in = file.getInputStream()) {List<UserExcel> userExcelList = EasyExcel.read(in)// 如果第一行才是标题,第二行是数据,从第二行开始读取.headRowNumber(1).head(UserExcel.class).doReadAllSync();for (UserExcel userExcel : userExcelList) {System.out.println(userExcel);}} catch (Exception e) {e.printStackTrace();}
}

读取多个Sheet(不同对象)

当每个Sheet的对象不一致的情况下,使用doReadAllSync方法无法指定每个Sheet的对象,可以依次读取Sheet进行解析

注意:依次读取sheet会出现重复读取流对象的情况,而一个流对象只能读取一次,重复使用会导致异常

java">@PostMapping("/uploadFile4")
public void uploadFile4(MultipartFile file) {InputStream in = null;try {in = file.getInputStream();List<UserExcel> userExcelList1 = EasyExcel.read(in)// 读取第一个sheet.sheet(0)// 如果第一行才是标题,第二行是数据,从第二行开始读取.headRowNumber(1).head(UserExcel.class).doReadSync();// 读取剩余的sheetin = file.getInputStream();List<UserExcel> userExcelList2 = EasyExcel.read(in).sheet(1)// 如果第一行才是标题,第二行是数据,从第二行开始读取.headRowNumber(1).head(UserExcel.class).doReadSync();List<UserExcel> userExcelList = new ArrayList<>();userExcelList.addAll(userExcelList1);userExcelList.addAll(userExcelList2);for (UserExcel userExcel : userExcelList) {System.out.println(userExcel);}} catch (Exception e) {e.printStackTrace();} finally {try {if (in != null) {in.close();}} catch (Exception e) {e.printStackTrace();}}
}

异步读取

监听器

查看监听器源码,通过实现ReadListener接口或继承AnalysisEventListener类可以自定义读取Sheet监听器

java">public interface ReadListener<T> extends Listener {// 在转换异常 获取其他异常下会调用本接口。抛出异常则停止读取。如果这里不抛出异常则继续读取下一行default void onException(Exception exception, AnalysisContext context) throws Exception {throw exception;}// 获取表头数据default void invokeHead(Map<Integer, ReadCellData<?>> headMap, AnalysisContext context) {}// 一行行读取表格内容void invoke(T data, AnalysisContext context);// 读取条额外信息:批注、超链接、合并单元格信息等default void extra(CellExtra extra, AnalysisContext context) {}// 读取完成后的操作void doAfterAllAnalysed(AnalysisContext context);// 是否还有数据default boolean hasNext(AnalysisContext context) {return true;}
}
java">public abstract class AnalysisEventListener<T> implements ReadListener<T> {// 解析表头数据@Overridepublic void invokeHead(Map<Integer, ReadCellData<?>> headMap, AnalysisContext context) {invokeHeadMap(ConverterUtils.convertToStringMap(headMap, context), context);}public void invokeHeadMap(Map<Integer, String> headMap, AnalysisContext context) {}
}

异常处理

ExcelDateConvertException

表示数据转换异常错误,出现该异常时会继续解析文件信息

java">@Getter
@Setter
@EqualsAndHashCode
public class ExcelDataConvertException extends ExcelRuntimeException {private Integer rowIndex;private Integer columnIndex;private CellData<?> cellData;private ExcelContentProperty excelContentProperty;public ExcelDataConvertException(Integer rowIndex, Integer columnIndex, CellData<?> cellData,ExcelContentProperty excelContentProperty, String message) {super(message);this.rowIndex = rowIndex;this.columnIndex = columnIndex;this.cellData = cellData;this.excelContentProperty = excelContentProperty;}public ExcelDataConvertException(Integer rowIndex, Integer columnIndex, CellData<?> cellData,ExcelContentProperty excelContentProperty, String message, Throwable cause) {super(message, cause);this.rowIndex = rowIndex;this.columnIndex = columnIndex;this.cellData = cellData;this.excelContentProperty = excelContentProperty;}
}

ExcelAnalysisStopException

非数据转换异常错误,在onexcetpion中抛出该异常后停止解析

java">public class ExcelAnalysisStopException extends ExcelAnalysisException {public ExcelAnalysisStopException() {}public ExcelAnalysisStopException(String message) {super(message);}public ExcelAnalysisStopException(String message, Throwable cause) {super(message, cause);}public ExcelAnalysisStopException(Throwable cause) {super(cause);}
}

读取单个Sheet(不指定对象)

读取文件时使用doRead方法进行异步操作,同时指定对应的监听器解析文件数据

Map<Integer, String>中的key表示列号、value表示数据

java">public class UserExcelListener1 extends AnalysisEventListener<Map<Integer, String>> {Logger log = LoggerFactory.getLogger(getClass());private List<Map<Integer, String>> userExcelList = new ArrayList<>();@Overridepublic void invoke(Map<Integer, String> map, AnalysisContext analysisContext) {log.info("解析到一条数据:{}", JSON.toJSONString(map));userExcelList.add(map);}@Overridepublic void doAfterAllAnalysed(AnalysisContext analysisContext) {log.info("已解析完所有数据!");userExcelList.clear();}@Overridepublic void onException(Exception exception, AnalysisContext context) throws Exception {if (exception instanceof ExcelDataConvertException) {ExcelDataConvertException convertException = (ExcelDataConvertException) exception;Integer row = convertException.getRowIndex();log.error("第{}行数据转换失败,异常信息:{}", row, exception.getMessage());} else {log.error("导入其他异常信息:{}", exception.getMessage());}}public List<Map<Integer, String>> getUserExcelList() {return userExcelList;}public void setUserExcelList(List<Map<Integer, String>> userExcelList) {this.userExcelList = userExcelList;}
}
java">@PostMapping("/uploadFile1")
public void uploadFile1(MultipartFile file) {try (InputStream in = file.getInputStream()) {UserExcelListener1 listener = new UserExcelListener1();EasyExcel.read(in, listener).sheet(0).headRowNumber(1) // 第一行是标题, 从第二行开始读取.doRead();} catch (Exception e) {e.printStackTrace();}
}

读取单个Sheet(指定对象)

java">public class UserExcelListener extends AnalysisEventListener<UserExcel> {Logger log = LoggerFactory.getLogger(getClass());private List<UserExcel> userExcelList = new ArrayList<>();@Overridepublic void invoke(UserExcel userExcel, AnalysisContext analysisContext) {log.info("解析到一条数据:{}", JSON.toJSONString(userExcel));userExcelList.add(userExcel);}@Overridepublic void doAfterAllAnalysed(AnalysisContext analysisContext) {log.info("已解析完所有数据!");userExcelList.clear();}@Overridepublic void onException(Exception exception, AnalysisContext context) throws Exception {if (exception instanceof ExcelDataConvertException) {ExcelDataConvertException convertException = (ExcelDataConvertException) exception;Integer row = convertException.getRowIndex();log.error("第{}行数据转换失败,异常信息:{}", row, exception.getMessage());} else {log.error("导入其他异常信息:{}", exception.getMessage());}}public List<UserExcel> getUserExcelList() {return userExcelList;}public void setUserExcelList(List<UserExcel> userExcelList) {this.userExcelList = userExcelList;}
}
java">@PostMapping("/uploadFile5")
public void uploadFile5(MultipartFile file) {try (InputStream in = file.getInputStream()) {UserExcelListener listener = new UserExcelListener();EasyExcel.read(in, UserExcel.class, listener).sheet(0).headRowNumber(1) // 第一行是标题, 从第二行开始读取.doRead();} catch (Exception e) {e.printStackTrace();}
}

读取多个Sheet

  1. 获取Sheet的总数,通过循环遍历的方式指定每个Sheet的监听器进行解析
  2. 使用构造器的方式传入Sheet对应的下标,在抛出异常时获取SheetNo和对应的行号,方便进行排查
java">public class UserExcelListener2 extends AnalysisEventListener<UserExcel> {Logger log = LoggerFactory.getLogger(getClass());private Integer sheetNo;private List<UserExcel> userExcelList = new ArrayList<>();public UserExcelListener2(Integer sheetNo) {this.sheetNo = sheetNo;}@Overridepublic void invoke(UserExcel userExcel, AnalysisContext analysisContext) {log.info("解析到一条数据:{}", JSON.toJSONString(userExcel));userExcelList.add(userExcel);}@Overridepublic void doAfterAllAnalysed(AnalysisContext analysisContext) {log.info("已解析完所有数据!");userExcelList.clear();}@Overridepublic void onException(Exception exception, AnalysisContext context) throws Exception {if (exception instanceof ExcelDataConvertException) {ExcelDataConvertException convertException = (ExcelDataConvertException) exception;Integer row = convertException.getRowIndex();log.error("sheetNo:{},第{}行数据转换失败,异常信息:{}", sheetNo, row, exception.getMessage());} else {log.error("导入其他异常信息:{}", exception.getMessage());}}public List<UserExcel> getUserExcelList() {return userExcelList;}public void setUserExcelList(List<UserExcel> userExcelList) {this.userExcelList = userExcelList;}
}
java">@PostMapping("/uploadFile6")
public void uploadFile6(MultipartFile file) {try (InputStream in = file.getInputStream();ExcelReader build = EasyExcel.read(in).build();) {List<ReadSheet> readSheets = build.excelExecutor().sheetList();for (int i = 0, len = readSheets.size(); i < len; i++) {UserExcelListener2 listener = new UserExcelListener2(i);ReadSheet sheet = EasyExcel.readSheet(readSheets.get(i).getSheetNo()).head(UserExcel.class).headRowNumber(1).registerReadListener(listener).build();build.read(sheet);}build.finish();} catch (Exception e) {e.printStackTrace();}
}

分批读取(线程池操作)

  1. 使用构造器的方式传入Sheet对应的下标和自定义线程池,使用这种分批处理的方式,避免内存的消耗,加快文件的解析入库
  2. 数据库入库时可以使用MySQL的批量插入语法,同时指定每次插入数据的大小,相较于MyBatisPlus的批量插入方法较快(疑问:在MyBatisPlus的批量插入方法时出现数据部分丢失的情况
java">/*** UserListener 不能被spring管理,要每次读取excel都要new,* 然后里面用到spring可以构造方法传进去*/
public class UserExcelListener3 extends AnalysisEventListener<UserExcel> {Logger log = LoggerFactory.getLogger(getClass());private static final Integer BATCH_SIZE = 1000;private Integer sheetNo;private Executor executor;private List<UserExcel> userExcelList = new ArrayList<>();public UserExcelListener3(Integer sheetNo, Executor executor) {this.sheetNo = sheetNo;this.executor = executor;}@Overridepublic void invoke(UserExcel userExcel, AnalysisContext analysisContext) {log.info("解析到一条数据:{}", JSON.toJSONString(userExcel));userExcelList.add(userExcel);if (userExcelList.size() >= BATCH_SIZE) {List<UserExcel> userExcels = BeanUtil.copyToList(userExcelList, UserExcel.class);CompletableFuture.runAsync(() -> {// 业务操作// saveToDB(userExcels);}, executor);userExcelList.clear();}}@Overridepublic void doAfterAllAnalysed(AnalysisContext analysisContext) {log.info("已解析完所有数据!");if (!userExcelList.isEmpty()) {List<UserExcel> userExcels = BeanUtil.copyToList(userExcelList, UserExcel.class);CompletableFuture.runAsync(() -> {// 业务操作// saveToDB(userExcels);}, executor);userExcelList.clear();}}@Overridepublic void onException(Exception exception, AnalysisContext context) throws Exception {if (exception instanceof ExcelDataConvertException) {ExcelDataConvertException convertException = (ExcelDataConvertException) exception;Integer row = convertException.getRowIndex();log.error("sheetNo:{},第{}行数据转换失败,异常信息:{}", sheetNo, row, exception.getMessage());} else {log.error("导入其他异常信息:{}", exception.getMessage());}}
}
java">@PostMapping("/uploadFile7")
public void uploadFile77(MultipartFile file) {try (InputStream in = file.getInputStream();ExcelReader build = EasyExcel.read(in).build();) {ThreadPoolExecutor executor = new ThreadPoolExecutor(10, 20, 60L,TimeUnit.MILLISECONDS, new ArrayBlockingQueue<>(1000), new ThreadPoolExecutor.AbortPolicy());List<ReadSheet> readSheets = build.excelExecutor().sheetList();for (int i = 0, len = readSheets.size(); i < len; i++) {UserExcelListener3 listener = new UserExcelListener3(i, executor);ReadSheet sheet = EasyExcel.readSheet(readSheets.get(i).getSheetNo()).head(UserExcel.class).headRowNumber(1).registerReadListener(listener).build();build.read(sheet);}build.finish();} catch (Exception e) {e.printStackTrace();}
}

事务操作

当使用监听器读取文件数据,使用分批插入数据的方法时,因为监听器不归Spring管理,所以无法使用Spring的事务注解进行事务的相关操作,怎么保证事务?

可以通过构造器的方式传入事务管理器,手动提交和回滚事务

java">@Slf4j
public class TestDataListener extends AnalysisEventListener<Test> {//每隔5条存储数据库,实际使用中可以设置为2500条,然后清理list ,方便内存回收private static final int BATCH_COUNT = 5;private List<Test> list = new ArrayList<>();//事务管理private DataSourceTransactionManager dataSourceTransactionManager;//事务定义private DefaultTransactionDefinition transactionDefinition;private TransactionStatus transactionStatus = null;private TestService testService;public TestDataListener(TestService testService,DataSourceTransactionManager dataSourceTransactionManager,TransactionDefinition transactionDefinition) {this.testService = testService;this.dataSourceTransactionManager = dataSourceTransactionManager;this.transactionDefinition = new DefaultTransactionDefinition(transactionDefinition);//设置事务的隔离级别 :未提交读写this.transactionDefinition.setIsolationLevel(TransactionDefinition.ISOLATION_READ_UNCOMMITTED);// 手动开启事务this.transactionStatus = dataSourceTransactionManager.getTransaction(transactionDefinition);}@Overridepublic void invoke(Test data, AnalysisContext context) {log.info("解析到一条数据:{}", JSON.toJSONString(data));boolean hasCompleted = transactionStatus.isCompleted();// 如果事务已经关闭,不执行业务代码if (hasCompleted){return;}list.add(data);if (list.size() >= BATCH_COUNT) {saveData();list.clear();}}// 这个方法会在easyexcel读取完文件中所有数据后执行@Overridepublic void doAfterAllAnalysed(AnalysisContext context) {//判断事务是否已被处理,未处理则进行提交事务boolean hasCompleted = transactionStatus.isCompleted();if (hasCompleted){return;}saveData();log.info("所有数据解析完成!");if (!hasCompleted){//提交事务dataSourceTransactionManager.commit(transactionStatus);log.info("SensitiveWordListener doAfterAllAnalysed:当前事务已提交");}}@Overridepublic void onException(Exception exception, AnalysisContext context) throws Exception {log.info("导入过程中出现异常会进入该方法,重写了父类方法");log.info("结束前事务状态:"+  transactionStatus.isCompleted());dataSourceTransactionManager.rollback(transactionStatus);log.info("结束后事务状态:"+  transactionStatus.isCompleted());throw exception;}/*** 加上存储数据库*/private void saveData() {log.info("{}条数据,开始存储数据库!", list.size());if (!CollectionUtils.isEmpty(list)) {testService.saveBatch(list);System.out.println(list);}//TODO 这里是测试事务,如有需要可以打开注释//int a = 1/0;log.info("存储数据库成功!");}
}

http://www.ppmy.cn/server/144376.html

相关文章

项目中排查bug的思路案例

bug描述&#xff1a;调用了删除的接口&#xff0c;执行成功了&#xff0c;也删掉了选中的数据&#xff0c;但是不执行删除后的处理操作&#xff0c;会报一个“系统未知错误&#xff0c;请反馈给管理员” 解决&#xff1a; 成功删掉了数据&#xff0c;但删除后的操作没有执行&a…

Docker 容器自动启动设置

在 Docker 中&#xff0c;可以通过设置容器的重启策略来实现容器的自动启动。这意味着&#xff0c;当 Docker 守护进程启动时&#xff0c;它可以自动启动特定的容器&#xff0c;无论是因为系统重启还是 Docker 服务本身的重启。 设置容器自动启动 要设置容器自动启动&#xf…

ElasticSearch学习笔记四:基础操作(二)

一、前言 上一篇文章中我们学习了ES中的基础操作&#xff0c;包括索引和映射&#xff0c;同时也学习了ES中的基础数据类型&#xff0c;今天我们继续学习其他的数据类型。 二、复杂数据类型 1、数组&#xff08;Array&#xff09; 在ES中没有特别指定数据类型&#xff0c;换…

C++ String

C中的字符串详解 在C中&#xff0c;字符串处理是一个重要的编程主题。字符串是一种用于存储文本数据的对象&#xff0c;C为字符串提供了丰富的操作接口&#xff0c;使得处理字符串变得灵活而高效。本文将详细介绍C的字符串&#xff0c;包括其定义、创建方式、操作技巧以及相关…

介绍一下strncmp(c基础)

strncmp是strcmp的进阶版 链接介绍一下strcmp(c基础)-CSDN博客 作用 比较两个字符串的前n位 格式 #include <string.h> strncmp (arr1,arr2,n); 工作原理&#xff1a;strcmp函数按照ACII&#xff08;字符编码顺序&#xff09;比较两个字符串。它从两个字符串的第一…

FreeRTOS——信号量

目录 一、概念及其应用 1.1定义 1.2作用 二、二值信号量 2.1定义 2.2二值信号量工作机制 2.3二值信号量应用场景—同步 2.4二值信号量API 2.4.1创建二值信号量 2.4.2任务中释放信号量 2.4.3中断中释放信号量 2.4.4任务中获取信号量 2.4.5中断中获取信号量 三、计…

《鸿蒙系统:开启智能新时代的璀璨之星》

一、鸿蒙系统&#xff1a;崛起之路 鸿蒙系统的发展历程堪称一部科技创新的传奇。2012 年&#xff0c;华为前瞻性地启动鸿蒙系统研发项目&#xff0c;彼时或许很少有人能预见到它未来的辉煌。2019 年&#xff0c;鸿蒙系统首个开发者预览版的发布&#xff0c;如同夜空中的一颗璀…

第二十二章 Spring之假如让你来写AOP——Target Object(目标对象)篇

Spring源码阅读目录 第一部分——IOC篇 第一章 Spring之最熟悉的陌生人——IOC 第二章 Spring之假如让你来写IOC容器——加载资源篇 第三章 Spring之假如让你来写IOC容器——解析配置文件篇 第四章 Spring之假如让你来写IOC容器——XML配置文件篇 第五章 Spring之假如让你来写…