Featured image of post 【合集】EasyExcel-读取excel

【合集】EasyExcel-读取excel

相关文章

EasyExcel官网

【合集】EasyExcel-写出excel

【合集】EasyPOI-写出excel

【合集】EasyPOI-读取excel

依赖

1
2
3
4
5
6
<!-- https://mvnrepository.com/artifact/com.alibaba/easyexcel -->
<dependency>
    <groupId>com.alibaba</groupId>
    <artifactId>easyexcel</artifactId>
    <version>4.0.1</version>
</dependency>

Spring-Web读(读MultipartFile)

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.read.listener.PageReadListener;
import com.alibaba.excel.util.MapUtils;

/**
 * easyExcel读取文件
 */
private static void easyExcelReadExcel(MultipartFile file) {
    try {
        InputStream inputStream = file.getInputStream();
        PageReadListener<ReadDto> pageReadListener = new PageReadListener<>(list -> {
            // 处理读到的分页list,batchCount控制每页数量
        }, 100);
        EasyExcel.read(inputStream, ReadDto.class, pageReadListener)
                .sheet(0)
                .headRowNumber(1)
                .doRead();
    } catch (Exception e) {
        log.error("读取Excel文件失败", e);
        throw new ResponseStatusException(HttpStatus.INTERNAL_SERVER_ERROR, "读取Excel文件失败");
    }
}
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
@ContentRowHeight(10)
@HeadRowHeight(20)
@Data
@Builder
@NoArgsConstructor
@AllArgsConstructor
public class ReadDto {

    @ExcelProperty(value = "主键")
    private Long id;

    @ExcelProperty(value = "开始日")
    @DateTimeFormat("yyyy-MM-dd")
    private LocalDate startDate;

    @ExcelProperty(value = "开始时间")
    @DateTimeFormat("yyyy-MM-dd HH:mm:ss")
    private LocalDateTime startTime;

    @ExcelProperty(value = "换电模式*")
    private String swapMode;

}

读取一个sheet(使用PageReadListener)

img_29.png

1
2
3
4
5
6
7
8
String in = easyExcel_read.class.getResource("/").getPath() + "easyExcel" + File.separator + "demo.xlsx";

// 读取一个sheet
EasyExcel.read(in, DemoData.class, new PageReadListener<DemoData>(list -> {
            // 业务。。。
            list.stream().forEach(System.out::println);
        }, 100))
        .sheet(0).headRowNumber(1).doRead();
1
2
3
4
5
6
@Data
public class DemoData {
    private String string;
    private Date date;
    private Double doubleData;
}
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
// 输出
DemoData(string=字符串0, date=Wed Jan 01 01:01:01 CST 2020, doubleData=1.0)
DemoData(string=字符串1, date=Thu Jan 02 01:01:01 CST 2020, doubleData=2.0)
DemoData(string=字符串2, date=Fri Jan 03 01:01:01 CST 2020, doubleData=3.0)
DemoData(string=字符串3, date=Sat Jan 04 01:01:01 CST 2020, doubleData=4.0)
DemoData(string=字符串4, date=Sun Jan 05 01:01:01 CST 2020, doubleData=5.0)
DemoData(string=字符串5, date=Mon Jan 06 01:01:01 CST 2020, doubleData=6.0)
DemoData(string=字符串6, date=Tue Jan 07 01:01:01 CST 2020, doubleData=7.0)
DemoData(string=字符串7, date=Wed Jan 08 01:01:01 CST 2020, doubleData=8.0)
DemoData(string=字符串8, date=Thu Jan 09 01:01:01 CST 2020, doubleData=9.0)
DemoData(string=字符串9, date=Fri Jan 10 01:01:01 CST 2020, doubleData=10.0)

读多个或者全部sheet(使用PageReadListener)

img_29.png

img_30.png

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
String in = easyExcel_read.class.getResource("/").getPath() + "easyExcel" + File.separator + "demo.xlsx";

// 读多个或者全部sheet
try (ExcelReader excelReader = EasyExcel.read(in).build()) {
    ReadSheet readSheet1 = EasyExcel.readSheet(0).head(DemoData.class).registerReadListener(new PageReadListener<DemoData>(list -> {
                // 业务。。。
                list.forEach(System.out::println);
            }, 100)
    ).headRowNumber(1).build();
    ReadSheet readSheet2 = EasyExcel.readSheet(1).head(DemoData.class).registerReadListener(new PageReadListener<DemoData>(list -> {
                // 业务。。。
                list.forEach(System.out::println);
            }, 100)
    ).headRowNumber(1).build();
    // 这里注意 一定要把sheet1 sheet2 一起传进去,不然有个问题就是03版的excel 会读取多次,浪费性能
    excelReader.read(readSheet1, readSheet2);
}
1
2
3
4
5
6
@Data
public class DemoData {
    private String string;
    private Date date;
    private Double doubleData;
}
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
// 输出
DemoData(string=字符串0, date=Wed Jan 01 01:01:01 CST 2020, doubleData=1.0)
DemoData(string=字符串1, date=Thu Jan 02 01:01:01 CST 2020, doubleData=2.0)
DemoData(string=字符串2, date=Fri Jan 03 01:01:01 CST 2020, doubleData=3.0)
DemoData(string=字符串3, date=Sat Jan 04 01:01:01 CST 2020, doubleData=4.0)
DemoData(string=字符串4, date=Sun Jan 05 01:01:01 CST 2020, doubleData=5.0)
DemoData(string=字符串5, date=Mon Jan 06 01:01:01 CST 2020, doubleData=6.0)
DemoData(string=字符串6, date=Tue Jan 07 01:01:01 CST 2020, doubleData=7.0)
DemoData(string=字符串7, date=Wed Jan 08 01:01:01 CST 2020, doubleData=8.0)
DemoData(string=字符串8, date=Thu Jan 09 01:01:01 CST 2020, doubleData=9.0)
DemoData(string=字符串9, date=Fri Jan 10 01:01:01 CST 2020, doubleData=10.0)
DemoData(string=表2字符串0, date=Wed Jan 01 01:01:01 CST 2020, doubleData=1.0)
DemoData(string=表2字符串1, date=Thu Jan 02 01:01:01 CST 2020, doubleData=2.0)
DemoData(string=表2字符串2, date=Fri Jan 03 01:01:01 CST 2020, doubleData=3.0)
DemoData(string=表2字符串3, date=Sat Jan 04 01:01:01 CST 2020, doubleData=4.0)
DemoData(string=表2字符串4, date=Sun Jan 05 01:01:01 CST 2020, doubleData=5.0)
DemoData(string=表2字符串5, date=Mon Jan 06 01:01:01 CST 2020, doubleData=6.0)
DemoData(string=表2字符串6, date=Tue Jan 07 01:01:01 CST 2020, doubleData=7.0)
DemoData(string=表2字符串7, date=Wed Jan 08 01:01:01 CST 2020, doubleData=8.0)
DemoData(string=表2字符串8, date=Thu Jan 09 01:01:01 CST 2020, doubleData=9.0)
DemoData(string=表2字符串9, date=Fri Jan 10 01:01:01 CST 2020, doubleData=10.0)

读取额外信息(批注、超链接、合并单元格)(使用ReadListener)

img_31.png

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
String extra = easyExcel_read.class.getResource("/").getPath() + "easyExcel" + File.separator + "extra.xlsx";

// 读取额外信息(批注、超链接、合并单元格)
ReadListener<ReadExtraExcelBO> readListener = new ReadListener<>() {
    @SneakyThrows
    @Override
    public void invoke(ReadExtraExcelBO readExtraExcelBO, AnalysisContext analysisContext) {
        log.info("读取到了一行表格信息:{}", new ObjectMapper().writeValueAsString(readExtraExcelBO));
    }

    @SneakyThrows
    @Override
    public void extra(CellExtra extra, AnalysisContext context) {
        log.info("读取到了一条额外信息:{}", new ObjectMapper().writeValueAsString(extra));
    }

    @Override
    public void doAfterAllAnalysed(AnalysisContext analysisContext) {
    }
};

EasyExcel.read(extra, ReadExtraExcelBO.class, readListener)
        .excelType(ExcelTypeEnum.XLSX)
        .extraRead(CellExtraTypeEnum.COMMENT) // 需要读取批注 默认不读取
        .extraRead(CellExtraTypeEnum.HYPERLINK) // 需要读取超链接 默认不读取
        .extraRead(CellExtraTypeEnum.MERGE) // 需要读取合并单元格信息 默认不读取
        .sheet().doRead();
1
2
3
4
5
@Data
public class ReadExtraExcelBO {
        private String row1;
        private String row2;
}
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
// 输出
读取到了一行表格信息:{"row1":"普通超链接","row2":null}
读取到了一行表格信息:{"row1":"多单元格超链接A","row2":"多单元格超链接B"}
读取到了一行表格信息:{"row1":"多单元格超链接C","row2":"多单元格超链接D"}
读取到了一行表格信息:{"row1":"批注","row2":null}
读取到了一行表格信息:{"row1":"合并单元格","row2":null}
读取到了一条额外信息:{"rowIndex":5,"columnIndex":0,"type":"MERGE","text":null,"firstRowIndex":5,"lastRowIndex":6,"firstColumnIndex":0,"lastColumnIndex":1}
读取到了一条额外信息:{"rowIndex":1,"columnIndex":0,"type":"HYPERLINK","text":"Sheet1!A1","firstRowIndex":1,"lastRowIndex":1,"firstColumnIndex":0,"lastColumnIndex":0}
读取到了一条额外信息:{"rowIndex":2,"columnIndex":0,"type":"HYPERLINK","text":"Sheet2!A1","firstRowIndex":2,"lastRowIndex":3,"firstColumnIndex":0,"lastColumnIndex":1}
读取到了一条额外信息:{"rowIndex":4,"columnIndex":0,"type":"COMMENT","text":"批注的内容","firstRowIndex":4,"lastRowIndex":4,"firstColumnIndex":0,"lastColumnIndex":0}

读取每个单元格的所有信息(有公式的单元格也可以读取到)(PageReadListener)

img_32.png

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
String cell = easyExcel_read.class.getResource("/").getPath() + "easyExcel" + File.separator + "cellDataDemo.xlsx";

// 读取每个单元格的所有信息(有公式的单元格也可以读取到)
PageReadListener<ReadCellExcelBO> readListener = new PageReadListener<>(cachedDataList -> {
    for (ReadCellExcelBO cellDataReadDemoData : cachedDataList) {
        try {
            log.info("读取到一条数据{}", new ObjectMapper().writeValueAsString(cellDataReadDemoData));
        } catch (JsonProcessingException e) {
            throw new RuntimeException(e);
        }
    }
}, 100);

EasyExcel.read(cell, ReadCellExcelBO.class, readListener)
        .excelType(ExcelTypeEnum.XLSX)
        .sheet().doRead();
1
2
3
4
5
6
7
8
9
@Data
public class ReadCellExcelBO {
    private CellData<String> string;
    // 这里注意 虽然是日期 但是 类型 存储的是number 因为excel 存储的就是number
    private CellData<Date> date;
    private CellData<Double> doubleData;
    // 这里并不一定能完美的获取 有些公式是依赖性的 可能会读不到 这个问题后续会修复
    private CellData<String> formulaValue;
}
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
// 输出
读取到一条数据: {"string":{"rowIndex":null,"columnIndex":null,"type":"STRING","numberValue":null,"stringValue":"字符串0","booleanValue":null,"data":"字符串0","formulaData":null,"originalNumberValue":null,"dataFormatData":{"index":0,"format":"General"}},"date":{"rowIndex":null,"columnIndex":null,"type":"NUMBER","numberValue":43831.0423726852,"stringValue":null,"booleanValue":null,"data":1577811661000,"formulaData":null,"originalNumberValue":43831.0423726852,"dataFormatData":{"index":22,"format":"yyyy-m-d h:mm"}},"doubleData":{"rowIndex":null,"columnIndex":null,"type":"NUMBER","numberValue":1,"stringValue":null,"booleanValue":null,"data":1.0,"formulaData":null,"originalNumberValue":1,"dataFormatData":{"index":0,"format":"General"}},"formulaValue":{"rowIndex":null,"columnIndex":null,"type":"STRING","numberValue":null,"stringValue":"字符串01","booleanValue":null,"data":"字符串01","formulaData":{"formulaValue":"_xlfn.CONCAT(A2,C2)"},"originalNumberValue":null,"dataFormatData":{"index":0,"format":"General"}}}
读取到一条数据: {"string":{"rowIndex":null,"columnIndex":null,"type":"STRING","numberValue":null,"stringValue":"字符串1","booleanValue":null,"data":"字符串1","formulaData":null,"originalNumberValue":null,"dataFormatData":{"index":0,"format":"General"}},"date":{"rowIndex":null,"columnIndex":null,"type":"NUMBER","numberValue":43832.0423726852,"stringValue":null,"booleanValue":null,"data":1577898061000,"formulaData":null,"originalNumberValue":43832.0423726852,"dataFormatData":{"index":22,"format":"yyyy-m-d h:mm"}},"doubleData":{"rowIndex":null,"columnIndex":null,"type":"NUMBER","numberValue":2,"stringValue":null,"booleanValue":null,"data":2.0,"formulaData":null,"originalNumberValue":2,"dataFormatData":{"index":0,"format":"General"}},"formulaValue":{"rowIndex":null,"columnIndex":null,"type":"STRING","numberValue":null,"stringValue":"字符串12","booleanValue":null,"data":"字符串12","formulaData":{"formulaValue":"_xlfn.CONCAT(A3,C3)"},"originalNumberValue":null,"dataFormatData":{"index":0,"format":"General"}}}
读取到一条数据: {"string":{"rowIndex":null,"columnIndex":null,"type":"STRING","numberValue":null,"stringValue":"字符串2","booleanValue":null,"data":"字符串2","formulaData":null,"originalNumberValue":null,"dataFormatData":{"index":0,"format":"General"}},"date":{"rowIndex":null,"columnIndex":null,"type":"NUMBER","numberValue":43833.0423726852,"stringValue":null,"booleanValue":null,"data":1577984461000,"formulaData":null,"originalNumberValue":43833.0423726852,"dataFormatData":{"index":22,"format":"yyyy-m-d h:mm"}},"doubleData":{"rowIndex":null,"columnIndex":null,"type":"NUMBER","numberValue":3,"stringValue":null,"booleanValue":null,"data":3.0,"formulaData":null,"originalNumberValue":3,"dataFormatData":{"index":0,"format":"General"}},"formulaValue":{"rowIndex":null,"columnIndex":null,"type":"STRING","numberValue":null,"stringValue":"字符串23","booleanValue":null,"data":"字符串23","formulaData":{"formulaValue":""},"originalNumberValue":null,"dataFormatData":{"index":0,"format":"General"}}}
读取到一条数据: {"string":{"rowIndex":null,"columnIndex":null,"type":"STRING","numberValue":null,"stringValue":"字符串3","booleanValue":null,"data":"字符串3","formulaData":null,"originalNumberValue":null,"dataFormatData":{"index":0,"format":"General"}},"date":{"rowIndex":null,"columnIndex":null,"type":"NUMBER","numberValue":43834.0423726852,"stringValue":null,"booleanValue":null,"data":1578070861000,"formulaData":null,"originalNumberValue":43834.0423726852,"dataFormatData":{"index":22,"format":"yyyy-m-d h:mm"}},"doubleData":{"rowIndex":null,"columnIndex":null,"type":"NUMBER","numberValue":4,"stringValue":null,"booleanValue":null,"data":4.0,"formulaData":null,"originalNumberValue":4,"dataFormatData":{"index":0,"format":"General"}},"formulaValue":{"rowIndex":null,"columnIndex":null,"type":"STRING","numberValue":null,"stringValue":"字符串34","booleanValue":null,"data":"字符串34","formulaData":{"formulaValue":""},"originalNumberValue":null,"dataFormatData":{"index":0,"format":"General"}}}
读取到一条数据: {"string":{"rowIndex":null,"columnIndex":null,"type":"STRING","numberValue":null,"stringValue":"字符串4","booleanValue":null,"data":"字符串4","formulaData":null,"originalNumberValue":null,"dataFormatData":{"index":0,"format":"General"}},"date":{"rowIndex":null,"columnIndex":null,"type":"NUMBER","numberValue":43835.0423726852,"stringValue":null,"booleanValue":null,"data":1578157261000,"formulaData":null,"originalNumberValue":43835.0423726852,"dataFormatData":{"index":22,"format":"yyyy-m-d h:mm"}},"doubleData":{"rowIndex":null,"columnIndex":null,"type":"NUMBER","numberValue":5,"stringValue":null,"booleanValue":null,"data":5.0,"formulaData":null,"originalNumberValue":5,"dataFormatData":{"index":0,"format":"General"}},"formulaValue":{"rowIndex":null,"columnIndex":null,"type":"STRING","numberValue":null,"stringValue":"字符串45","booleanValue":null,"data":"字符串45","formulaData":{"formulaValue":""},"originalNumberValue":null,"dataFormatData":{"index":0,"format":"General"}}}
读取到一条数据: {"string":{"rowIndex":null,"columnIndex":null,"type":"STRING","numberValue":null,"stringValue":"字符串5","booleanValue":null,"data":"字符串5","formulaData":null,"originalNumberValue":null,"dataFormatData":{"index":0,"format":"General"}},"date":{"rowIndex":null,"columnIndex":null,"type":"NUMBER","numberValue":43836.0423726852,"stringValue":null,"booleanValue":null,"data":1578243661000,"formulaData":null,"originalNumberValue":43836.0423726852,"dataFormatData":{"index":22,"format":"yyyy-m-d h:mm"}},"doubleData":{"rowIndex":null,"columnIndex":null,"type":"NUMBER","numberValue":6,"stringValue":null,"booleanValue":null,"data":6.0,"formulaData":null,"originalNumberValue":6,"dataFormatData":{"index":0,"format":"General"}},"formulaValue":{"rowIndex":null,"columnIndex":null,"type":"STRING","numberValue":null,"stringValue":"字符串56","booleanValue":null,"data":"字符串56","formulaData":{"formulaValue":""},"originalNumberValue":null,"dataFormatData":{"index":0,"format":"General"}}}
读取到一条数据: {"string":{"rowIndex":null,"columnIndex":null,"type":"STRING","numberValue":null,"stringValue":"字符串6","booleanValue":null,"data":"字符串6","formulaData":null,"originalNumberValue":null,"dataFormatData":{"index":0,"format":"General"}},"date":{"rowIndex":null,"columnIndex":null,"type":"NUMBER","numberValue":43837.0423726852,"stringValue":null,"booleanValue":null,"data":1578330061000,"formulaData":null,"originalNumberValue":43837.0423726852,"dataFormatData":{"index":22,"format":"yyyy-m-d h:mm"}},"doubleData":{"rowIndex":null,"columnIndex":null,"type":"NUMBER","numberValue":7,"stringValue":null,"booleanValue":null,"data":7.0,"formulaData":null,"originalNumberValue":7,"dataFormatData":{"index":0,"format":"General"}},"formulaValue":{"rowIndex":null,"columnIndex":null,"type":"STRING","numberValue":null,"stringValue":"字符串67","booleanValue":null,"data":"字符串67","formulaData":{"formulaValue":""},"originalNumberValue":null,"dataFormatData":{"index":0,"format":"General"}}}
读取到一条数据: {"string":{"rowIndex":null,"columnIndex":null,"type":"STRING","numberValue":null,"stringValue":"字符串7","booleanValue":null,"data":"字符串7","formulaData":null,"originalNumberValue":null,"dataFormatData":{"index":0,"format":"General"}},"date":{"rowIndex":null,"columnIndex":null,"type":"NUMBER","numberValue":43838.0423726852,"stringValue":null,"booleanValue":null,"data":1578416461000,"formulaData":null,"originalNumberValue":43838.0423726852,"dataFormatData":{"index":22,"format":"yyyy-m-d h:mm"}},"doubleData":{"rowIndex":null,"columnIndex":null,"type":"NUMBER","numberValue":8,"stringValue":null,"booleanValue":null,"data":8.0,"formulaData":null,"originalNumberValue":8,"dataFormatData":{"index":0,"format":"General"}},"formulaValue":{"rowIndex":null,"columnIndex":null,"type":"STRING","numberValue":null,"stringValue":"字符串78","booleanValue":null,"data":"字符串78","formulaData":{"formulaValue":""},"originalNumberValue":null,"dataFormatData":{"index":0,"format":"General"}}}
读取到一条数据: {"string":{"rowIndex":null,"columnIndex":null,"type":"STRING","numberValue":null,"stringValue":"字符串8","booleanValue":null,"data":"字符串8","formulaData":null,"originalNumberValue":null,"dataFormatData":{"index":0,"format":"General"}},"date":{"rowIndex":null,"columnIndex":null,"type":"NUMBER","numberValue":43839.0423726852,"stringValue":null,"booleanValue":null,"data":1578502861000,"formulaData":null,"originalNumberValue":43839.0423726852,"dataFormatData":{"index":22,"format":"yyyy-m-d h:mm"}},"doubleData":{"rowIndex":null,"columnIndex":null,"type":"NUMBER","numberValue":9,"stringValue":null,"booleanValue":null,"data":9.0,"formulaData":null,"originalNumberValue":9,"dataFormatData":{"index":0,"format":"General"}},"formulaValue":{"rowIndex":null,"columnIndex":null,"type":"STRING","numberValue":null,"stringValue":"字符串89","booleanValue":null,"data":"字符串89","formulaData":{"formulaValue":""},"originalNumberValue":null,"dataFormatData":{"index":0,"format":"General"}}}
读取到一条数据: {"string":{"rowIndex":null,"columnIndex":null,"type":"STRING","numberValue":null,"stringValue":"字符串9","booleanValue":null,"data":"字符串9","formulaData":null,"originalNumberValue":null,"dataFormatData":{"index":0,"format":"General"}},"date":{"rowIndex":null,"columnIndex":null,"type":"NUMBER","numberValue":43840.0423726852,"stringValue":null,"booleanValue":null,"data":1578589261000,"formulaData":null,"originalNumberValue":43840.0423726852,"dataFormatData":{"index":22,"format":"yyyy-m-d h:mm"}},"doubleData":{"rowIndex":null,"columnIndex":null,"type":"NUMBER","numberValue":10,"stringValue":null,"booleanValue":null,"data":10.0,"formulaData":null,"originalNumberValue":10,"dataFormatData":{"index":0,"format":"General"}},"formulaValue":{"rowIndex":null,"columnIndex":null,"type":"STRING","numberValue":null,"stringValue":"字符串910","booleanValue":null,"data":"字符串910","formulaData":{"formulaValue":""},"originalNumberValue":null,"dataFormatData":{"index":0,"format":"General"}}}

不创建对象的读(直接用map接收数据)(使用AnalysisEventListener)

img_29.png

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
String in = easyExcel_read.class.getResource("/").getPath() + "easyExcel" + File.separator + "demo.xlsx";

// 不创建对象的读(直接用map接收数据)
AnalysisEventListener<Map<Integer, String>> eventListener = new AnalysisEventListener<>() {
    private static final int BATCH_COUNT = 5;
    private List<Map<Integer, String>> cachedDataList = ListUtils.newArrayListWithExpectedSize(BATCH_COUNT);

    @SneakyThrows
    @Override
    public void invoke(Map<Integer, String> data, AnalysisContext context) {
        log.info("解析到一条数据:{}", new ObjectMapper().writeValueAsString(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("存储数据库成功!");
    }
};
       
EasyExcel.read(in, eventListener)
    .sheet(0).headRowNumber(1).doRead();
1
2
3
4
5
6
@Data
public class DemoData {
    private String string;
    private Date date;
    private Double doubleData;
}
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
// 输出
解析到一条数据:{"0":"字符串0","1":"2020-1-1 1:01","2":"1"}
解析到一条数据:{"0":"字符串1","1":"2020-1-2 1:01","2":"2"}
解析到一条数据:{"0":"字符串2","1":"2020-1-3 1:01","2":"3"}
解析到一条数据:{"0":"字符串3","1":"2020-1-4 1:01","2":"4"}
解析到一条数据:{"0":"字符串4","1":"2020-1-5 1:01","2":"5"}
5条数据,开始存储数据库!
存储数据库成功!
解析到一条数据:{"0":"字符串5","1":"2020-1-6 1:01","2":"6"}
解析到一条数据:{"0":"字符串6","1":"2020-1-7 1:01","2":"7"}
解析到一条数据:{"0":"字符串7","1":"2020-1-8 1:01","2":"8"}
解析到一条数据:{"0":"字符串8","1":"2020-1-9 1:01","2":"9"}
解析到一条数据:{"0":"字符串9","1":"2020-1-10 1:01","2":"10"}
5条数据,开始存储数据库!
存储数据库成功!
0条数据,开始存储数据库!
存储数据库成功!
所有数据解析完成!
皖ICP备2024056275号-1
发表了80篇文章 · 总计150.57k字
本站已稳定运行