EasyExcel解析工具,同步处理数据

     <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>easyexcel</artifactId>
            <version>2.0.4</version>
        </dependency>

定义实体

public class IndustryDto {
    @ExcelProperty(value = "服务大类", index = 0)
    private String industryName;
    @ExcelProperty(value = "品类", index = 1)
    private String category;
    @ExcelProperty(value = "件数", index = 3)
    private Integer num;
	//set/get....
}

读取Excel 批量处理数据

@Service
public class ArticleServiceImpl implements ArticleService {

    private static final Logger logger = LoggerFactory.getLogger(ArticleServiceImpl.class);
    @Override
    public PagedResult<SearchRespVo> search( SearchReqVo req) {
        return null;
    }

    @Autowired
    IndustryMapperExt industryCategoryMapperExt;
    @Override
    public void uploadIndustryCategory(MultipartFile file)  {
        try {
            EasyExcel.read( file.getInputStream(), IndustryDto.class, new DataFlowPriceExcelListener(industryCategoryMapperExt)).sheet().doRead();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    private static class DataFlowPriceExcelListener extends AnalysisEventListener<IndustryDto> {
        private int maxSize = 1000;
        private List<Industry> industryCategoryList = new ArrayList<>(maxSize);
        private IndustryMapperExt industryCategoryMapperExt;

        DataFlowPriceExcelListener(IndustryMapperExt industryCategoryMapperExt) {
            this.industryCategoryMapperExt = industryCategoryMapperExt;
        }

        /**
         * 每隔1000条存储数据库,然后清理list ,方便内存回收
         */
        @Override
        public void invoke(IndustryDto industryDto, AnalysisContext analysisContext) {
            // 达到maxSize了,需要去存储一次数据库,防止数据几万条数据在内存,容易OO
            if (industryCategoryList.size() == maxSize) {
                industryCategoryMapperExt.batchInsertSelective(industryCategoryList);
                industryCategoryList.clear();
            }
            Industry industryCategory = new Industry();
            BeanUtils.copyProperties(industryDto, industryCategory);
            industryCategory.setCreateTime(new Date());
            industryCategoryList.add(industryCategory);
        }

        /**
         * 所有数据解析完成了 都会来调用
         */
        @Override
        public void doAfterAllAnalysed(AnalysisContext analysisContext) {
            if (industryCategoryList.size() > 0) {
                industryCategoryMapperExt.batchInsertSelective(industryCategoryList);
                industryCategoryList.clear();
            }
        }
    }
}

参考https://www.cnblogs.com/ngrzr/archive/2019/12/04/11982697.html

你可能感兴趣的:(EasyExcel解析工具,同步处理数据)