zoukankan      html  css  js  c++  java
  • easyExcel-复杂表头-多个流,多个sheet循环读取【分段读取】案例

    //title监听器

    @Slf4j
    public class TitleListener extends AnalysisEventListener<Title> {
      private final List<Title> rows = new ArrayList<>();
      @Override
      public void invoke(Title title, AnalysisContext analysisContext) {
        rows.add(title);
      }
      @Override
      public void doAfterAllAnalysed(AnalysisContext analysisContext) {
      }
      public List<Title> getRows() {
        return rows;
      }
    }

    ===============================================================

    //SignalListener 监听器

    @Slf4j
    public class SignalListener extends AnalysisEventListener<Signal> {
    private final List<Signal> rows = new ArrayList<>();
    private static String subdivisionalWork = "";
    private static String partitionedPrj = "";
    private static String singlePrj="";
    @Override
    public void invoke(Signal signal, AnalysisContext analysisContext) {
    if (null != signal.getSubdivisionalWork()) {
    subdivisionalWork = signal.getSubdivisionalWork();
    }
    if (null != signal.getPartitionedPrj()) {
    partitionedPrj = signal.getPartitionedPrj();
    }
    if (null != signal.getSinglePrj()) {
    singlePrj = signal.getSinglePrj();
    }
    signal.setSubdivisionalWork(subdivisionalWork);
    signal.setPartitionedPrj(partitionedPrj);
    signal.setSinglePrj(singlePrj);
    rows.add(signal);
    }

    @Override
    public void doAfterAllAnalysed(AnalysisContext analysisContext) {
    }

    public List<Signal> getRows() {
    return rows;
    }
    }

    =============================================================================

    //实体

    @Data
    public class Signal extends BaseRowModel {
    @ExcelProperty(value = {"单项", "单项"}, index = 0)
    private String singlePrj="";
    @ExcelProperty(value = {"分部", "分部"}, index = 1)
    private String partitionedPrj="";
    @ExcelProperty(value = {"分项", "分项"}, index = 2)
    private String subdivisionalWork="";
    @ExcelProperty(value = {"工作事项清单", "工作事项清单"}, index = 3)
    private String workItem = "";
    @ExcelProperty(value = {"单位", "单位"}, index = 4)
    private String description="";
    @ExcelProperty(value = {"设计总数量", "左"}, index = 5)
    private String designLeft ="0";
    @ExcelProperty(value = {"设计总数量", "右"}, index = 6)
    private String designRight = "0";
    @ExcelProperty(value = {"设计总数量", "设计总数量"}, index = 7)
    private String designTotal = "0";
    @ExcelProperty(value = {"当日完成数量", "左"}, index = 8)
    private String currentLeft = "0";
    @ExcelProperty(value = {"当日完成数量", "右"}, index = 9)
    private String currentRight = "0";
    @ExcelProperty(value = {"当日完成数量", "当日完成数量"}, index = 10)
    private String currentTotal = "0";
    @ExcelProperty(value = "备注", index = 11)
    private String comment;
    }

    //实体

    @Data
    public class Title extends BaseRowModel {
    @ExcelProperty(value = "项目名称",index = 1)
    private String proName;
    }

    =======================================================================================================
    部分代码

    @ResponseBody
    public Result<String> read4File(MultipartFile[] file) throws Exception {
         for (int i = 0; i < file.length; i++) {

    //循环的部分
    ExcelReader excelReaderOnePart = EasyExcel.read(file[i].getInputStream()).build();
    ExcelReader excelReaderTwoPart = EasyExcel.read(file[i].getInputStream()).build();
    List<ReadSheet> readSheets = EasyExcel.read(file[i].getInputStream()).build().excelExecutor().sheetList();
    if (!readSheets.isEmpty()) {
    for (ReadSheet readSheet : readSheets) {
    //ExcelListener<Title> listener = new ExcelListener<>();
    TitleListener titleListener=new TitleListener();
    SignalListener signalListener = new SignalListener();
          //第一部分从第一行开始读
    readSheet = EasyExcel.readSheet(readSheet.getSheetNo()).head(Title.class).registerReadListener(titleListener).headRowNumber(1).build();
    excelReaderOnePart.read(readSheet);
           //第二部分从第7行开始读
    readSheet = EasyExcel.readSheet(readSheet.getSheetNo()).head(Signal.class).registerReadListener(signalListener).headRowNumber(7).build();
    excelReaderTwoPart.read(readSheet);
          //读取名称
          String proName = titleListener.getRows().get(0).getProName();
          //读取单位名称
          String unitName = titleListener.getRows().get(1).getProName();
          //读取日期
          String excelDate = titleListener.getRows().get(2).getProName();
          //拿到读取到的集合
          List<Signal> signals = signalListener.getRows();

          // doSomething
          .......
    }
        excelReaderOnePart.finish();
    excelReaderTwoPart.finish();
    }
    }
  • 相关阅读:
    C++之友元机制(友元函数和友元类)
    C++之全局函数和成员函数互相转换
    C++之面向对象初探----对象管理模型(关键是this指针)
    C++之const类成员变量,const成员函数
    C++之static类成员,static类成员函数
    c++之函数值传递和引用传递解析----关键在于理解函数return的实现机制(内存分配)
    C++之new和delete
    C++之构造函数、参数列表、析构函数
    C++之匿名对象解析
    C结构体、C++结构体、C++类的区别
  • 原文地址:https://www.cnblogs.com/austinspark-jessylu/p/15207078.html
Copyright © 2011-2022 走看看