content: package com.alibaba.easyexcel.test.demo.read;
  
  import java.util.List;
  
  import com.alibaba.excel.context.AnalysisContext;
  import com.alibaba.excel.event.AnalysisEventListener;
  import com.alibaba.excel.util.ListUtils;
  import com.alibaba.fastjson2.JSON;
  
  import lombok.extern.slf4j.Slf4j;
  
  /**
   * 模板的读取类
   *
   * @author Jiaju Zhuang
   */
  @Slf4j
  public class IndexOrNameDataListener extends AnalysisEventListener<IndexOrNameData> {
      /**
       * 每隔5条存储数据库，实际使用中可以100条，然后清理list ，方便内存回收
       */
      private static final int BATCH_COUNT = 5;
      private List<IndexOrNameData> cachedDataList = ListUtils.newArrayListWithExpectedSize(BATCH_COUNT);
  
      @Override
      public void invoke(IndexOrNameData data, AnalysisContext context) {
          log.info("解析到一条数据:{}", JSON.toJSONString(data));
          cachedDataList.add(data);
          if (cachedDataList.size() >= BATCH_COUNT) {
              saveData();
              cachedDataList = ListUtils.newArrayListWithExpectedSize(BATCH_COUNT);
          }
      }
  
      @Override
      public void doAfterAllAnalysed(AnalysisContext context) {
          saveData();
          log.info("所有数据解析完成！");
      }
  
      /**
       * 加上存储数据库
       */
      private void saveData() {
          log.info("{}条数据，开始存储数据库！", cachedDataList.size());
          log.info("存储数据库成功！");
      }
  }
  