当前位置:网站首页>Easyexcel: read Excel data into the list set

Easyexcel: read Excel data into the list set

2022-06-27 06:38:00 YUELEI118

1. pom.xml

Add dependency

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

2. Defining entity classes

 annotation :@ExcelProperty(" Parameters "), Parameters of the corresponding Excel Column name in , There is no order , Write whatever column you want .
public class ReportDetail {
    
		// ExcelIgnore  annotation : Entity classes need , however Excel Columns not in , there id Is the self increment of the database ID Primary key 
        @ExcelIgnore 
        private Integer id;
        @ExcelProperty(" full name ")//Excel Column name in 
        private String personName;
        .
        .
        //  No arguments structure  : must 
        //  There are parametric structures ,get/set Method 

3. Define storage Excel Secondary data list aggregate

	ArrayList<ReportDetail> reportDetails = new ArrayList<>();

4. Read Excel

read Method :
The first parameter :path:Excel File path or InputStream
The second parameter : Entity class
The third parameter :ReadListener Anonymous implementation class of , Parameters in generics : Entity class type

	EasyExcel.read( route ,  Entity class name .class, new AnalysisEventListener< Entity class name >() {
    
       @Override
       public void invoke( Entity class name   Shape parameter , AnalysisContext analysisContext) {
    
            list aggregate .add( Shape parameter );
       }
      @Override
      public void doAfterAllAnalysed(AnalysisContext analysisContext) {
    }
      }).sheet("Excel Table name in , Default first table ").doRead();
  • Corresponding to the above entity class and list aggregate
	EasyExcel.read(path, ReportDetail.class, new AnalysisEventListener<ReportDetail>() {
    
       @Override
       public void invoke(ReportDetail reportExcel, AnalysisContext analysisContext) {
    
       		//  Save each read line into reportDetails Collection 
            reportDetails.add(reportExcel);
       }
      @Override
      public void doAfterAllAnalysed(AnalysisContext analysisContext) {
    }
      }).sheet().doRead();
原网站

版权声明
本文为[YUELEI118]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/178/202206270603345377.html