首页 > 其他分享 >EasyExcel读取所有sheet(doReadAll)报BatchUpdateException: Duplicate entry

EasyExcel读取所有sheet(doReadAll)报BatchUpdateException: Duplicate entry

时间:2023-10-23 22:55:21浏览次数:34  
标签:COUNT sheet doReadAll EasyExcel excel cacheDataList importService import com

报错截图

 

 

原因:监听器保存数据时,但是没有重置缓存数据,导致读取第二个sheet的时候,缓存里存的数据还包含上一个或多个sheet的数据。每个sheet读取完毕后调用一次doAfterAllAnalysed。在doAfterAllAnalysed方法调用批量插入后,需要 清除缓存数据

cacheDataList = ListUtils.newArrayListWithExpectedSize(BATCH_COUNT);
 1 package com.example.excel.listener;
 2 
 3 import com.alibaba.excel.context.AnalysisContext;
 4 import com.alibaba.excel.read.listener.ReadListener;
 5 import com.alibaba.excel.util.ListUtils;
 6 import com.alibaba.fastjson.JSON;
 7 import com.example.excel.entity.ImportExcel;
 8 import com.example.excel.service.ImportService;
 9 import com.example.excel.service.impl.ImportServiceImpl;
10 import lombok.extern.slf4j.Slf4j;
11 
12 import java.util.List;
13 
14 /**
15  * <p>
16  *
17  * </p >
18  *
19  * @author Heqq
20  */
21 // 有个很重要的点 ImportExcelListener 不能被spring管理,要每次读取excel都要new,然后里面用到spring可以构造方法传进去
22 @Slf4j
23 public class ImportExcelListener implements ReadListener<ImportExcel> {
24 
25 
26     /**
27      * 每隔5条存储数据库,实际使用中可以100条,然后清理list ,方便内存回收
28      */
29     private static final int BATCH_COUNT = 100;
30 
31     /**
32      * 缓存的数据
33      */
34     private List<ImportExcel> cacheDataList = ListUtils.newArrayListWithExpectedSize(BATCH_COUNT);
35 
36     /**
37      * 假设这个是一个DAO,当然有业务逻辑这个也可以是一个service。当然如果不用存储这个对象没用。
38      */
39     private ImportService importService;
40 
41     /**
42      * 这里是importService,所以随便new一个。实际使用如果到了spring,请使用下面的有参构造函数
43      */
44     public ImportExcelListener() {
45         // 这里是demo,所以随便new一个。实际使用如果到了spring,请使用下面的有参构造函数
46         importService = new ImportServiceImpl();
47     }
48 
49     /**
50      * 如果使用了spring,请使用这个构造方法。每次创建Listener的时候需要把spring管理的类传进来
51      *
52      * @param importService
53      */
54     public ImportExcelListener(ImportService importService) {
55         this.importService = importService;
56     }
57 
58     /**
59      * 这个每一条数据解析都会来调用
60      *
61      * @param importExcel
62      * @param analysisContext
63      */
64     @Override
65     public void invoke(ImportExcel importExcel, AnalysisContext analysisContext) {
66         log.info("解析到一条数据:{}", JSON.toJSONString(importExcel));
67         cacheDataList.add(importExcel);
68         // 达到BATCH_COUNT了,需要去存储一次数据库,防止数据几万条数据在内存,容易OOM
69         if (cacheDataList.size() >= BATCH_COUNT) {
70             saveData();
71             // 存储完成清理 list
72             cacheDataList = ListUtils.newArrayListWithExpectedSize(BATCH_COUNT);
73         }
74     }
75 
76     /**
77      * 所有数据解析完成了 都会来调用
78      *
79      * @param analysisContext
80      */
81     @Override
82     public void doAfterAllAnalysed(AnalysisContext analysisContext) {
83         // 这里也要保存数据,确保最后遗留的数据也存储到数据库
84         saveData();
85         cacheDataList = ListUtils.newArrayListWithExpectedSize(BATCH_COUNT);
86         log.info("所有数据解析完成!");
87     }
88 
89     /**
90      * 加上存储数据库
91      */
92     public void saveData() {
93         log.info("{}条数据,开始存储数据库!", cacheDataList.size());
94         importService.saveBatch(cacheDataList);
95         log.info("存储数据成功!");
96     }
97 }

 

标签:COUNT,sheet,doReadAll,EasyExcel,excel,cacheDataList,importService,import,com
From: https://www.cnblogs.com/-hqq/p/17783701.html

相关文章

  • js_doucment.styleSheets获取StyleSheetList接口
    通过document.styleSheets可以获取到一个StyleSheetList接口constaStyleList=document.styleSheetsconsole.log(aStyleList.constructor.name)//StyleSheetListconsole.log(Object.prototype.toString.call(aStyleList))//[objectStyleSheetList]console.log(Arra......
  • .net 多sheet页导出表格文件表格文件
     usingSystem;usingSystem.Data;usingSystem.Linq;usingSystem.Configuration;usingSystem.Web;usingSystem.Web.Security;usingSystem.Web.UI;usingSystem.Web.UI.WebControls;usingNPOI.HSSF.UserModel;usingNPOI.SS.UserModel;usingSystem.IO;//usingSystem.W......
  • [911] Read Data from Google Sheets into Pandas without the Google Sheets API (.g
    ref:ReadDatafromGoogleSheetsintoPandaswithouttheGoogleSheetsAPIimportpandasaspdsheet_id="1XqOtPkiE_Q0dfGSoyxrH730RkwrTczcRbDeJJpqRByQ"sheet_name="Sheet1"url=f"https://docs.google.com/spreadsheets/d/{sheet......
  • 解决TypeError: read_excel() got an unexpected keyword argument ‘parse_cols or
    解决TypeError:read_excel()gotanunexpectedkeywordargument‘parse_cols'或‘sheetname‘在使用pandas包进行Excel文件处理时,有时候会遇到TypeError:read_excel()gotanunexpectedkeywordargument‘parse_cols'或TypeError:read_excel()gotanunexpectedkeyword......
  • EasyExcel实现读写操作
    (1)Excel导入导出的应用场景①数据导入:减轻录入工作量②数据导出:统计信息归档③数据传输:异构系统之间数据传输(2)EasyExcel简介EasyExcel的特点:Java领域解析、生成Excel比较有名的框架有Apachepoi、jxl等。但他们都存在一个严重的问题就是非常的耗内存。如果你的系统并发量......
  • Spring Boot + EasyExcel 导入导出,好用到爆,可以扔掉 POI 了
    1、EasyExcelEasyExcel是阿里巴巴开源poi插件之一,主要解决了poi框架使用复杂,sax解析模式不容易操作,数据量大起来容易OOM,解决了POI并发造成的报错。主要解决方式:通过解压文件的方式加载,一行一行的加载,并且抛弃样式字体等不重要的数据,降低内存的占用。EasyExcel优势注解式自定义操作......
  • Python 合并Excel文件(Excel文件多sheet)
    一、Python合并Excel文件多sheet《方法1》importosimportpandasaspd#指定包含Excel文件的文件夹路径folder_path='C:\\Users\\Admin\\Desktop\\数据核对'#获取文件夹中的所有Excel文件excel_files=[fileforfileinos.listdir(folder_path)iffile.endswith......
  • Unity保存一组sprite为spritesheet
    背景之前尝试在Unity编辑器用代码读取打包好的数据,发现在布置场景的时候不能做到所见即所得,这里将运行时读取到的图片素材,保存为spritesheet,并设置对应的sprite。效果展示代码usingSystem;usingSystem.Collections.Generic;usingSystem.IO;usingSystem.Linq;usingSy......
  • pandas读取一个文件夹下所有excel表格中的第三个sheet,怎么破?
    大家好,我是皮皮。一、前言前几天在Python最强王者交流群【wen】问了一个Python自动化办公的问题,一起来看看吧。请教,pandas读取一个文件夹下所有excel表格中的第三个sheet,但是不同的excel的第三个sheetname也不同,怎么设定参数比较方面呢?二、实现过程这里【哎呦喂 是豆子~】......
  • 使用EasyExcel 导入数据,失败原因数据导出
    引言在日常开发过程中,Excel导入是非常常见的场景,而且也有很多开源的项目是针对Excel的读写的,如Apache的poi,最近用的比较好的还是阿里的EasyExcel开源工具。平时我们只是简单的读取文件并写入数据库持久化即可,但是前段时间,产品搞了个需求,需要将导入失败的数据及原因写入Excel......