Use EasyExcel read Excel (two ways)

The introduction of the jar package

        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>easyexcel</artifactId>
            <version>1.1.2-beta5</version>
        </dependency>

EasyExcel support entity class attribute annotations facilitate subsequent read and write operations.

 id is the header, index 0 represents the first row

 @ExcelProperty(value="id",index=0)

Create a ExcelModel entity (need to inherit BaseRowModel)

Package com.zh.oukele.model; 

Import com.alibaba.excel.annotation.ExcelProperty;
 Import com.alibaba.excel.metadata.BaseRowModel; 

/ ** 
 * entity table 
 * / 
public  class ExcelMode the extends BaseRowModel { 

    / ** 
     * of one column of data 
     * / 
    @ExcelProperty (index = 0 )
     Private String column1;
     / ** 
     data of the second column * 
     * / 
    @ExcelProperty (index =. 1 )
     Private String Column2; 

    public  void setColumn1 (String column1) {
         the this .column1 = column1;
    }

    public String getColumn1() {
        return column1;
    }

    public String getColumn2() {
        return column2;
    }

    public void setColumn2(String column2) {
        this.column2 = column2;
    }

    @Override
    public String toString() {
        return "ExcelMode{" +
                "column1='" + column1 + '\'' +
                ", column2='" + column2 + '\'' +
                '}';
    }
}

Synchronous read (when large volumes of data, is not recommended, memory consumption is relatively large)

    // simple fetch (read synchronously) 
    public  static  void simpleRead () { 

        // read excel spreadsheet path 
        String readPath = "C: \\ Users \\ oukele \\ Desktop \\ analog data .xlsx" ; 

        the try {
             // sheetNo -> which form read
             // headLineMun -> which row starts to read this line (not including definitions, such headLineMun is 2, then the data is taken out of the third row begins reading from take)
             // clazz -> the read data, converted to the corresponding entities need BaseRowModel the extends 
            Sheet Sheet = new new Sheet (. 1,. 1, ExcelMode. class ); 

            // this is taken out of a set of entities ExcelModel 
            List <Object> = EasyExcelFactory.read readlist ( new new FileInputStream(readPath), sheet);
            // 存 ExcelMode 实体的 集合
            List<ExcelMode> list = new ArrayList<ExcelMode>();
            for (Object obj : readList) {
                list.add((ExcelMode) obj);
            }

            // 取出数据
            StringBuilder str = new StringBuilder();
            str.append("{");
            String link = "";
            for (ExcelMode mode : list) {
                str.append(link).append("\""+mode.getColumn1()+"\":").append("\""+mode.getColumn2()+"\"");
                link= ",";
            }
            str.append("};");
            System.out.println(str);

        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }


    }

Asynchronous read

Create a listener class ExcelModelListener out, and class inheritance AnalysisEventListener

Package com.zh.oukele.listener; 

Import com.alibaba.excel.context.AnalysisContext;
 Import com.alibaba.excel.event.AnalysisEventListener;
 Import com.zh.oukele.model.ExcelMode; 

Import of java.util.ArrayList;
 Import java.util.List; 

/ ** * 
 * listener 
 * / 
public  class ExcelModelListener the extends AnalysisEventListener <ExcelMode> { 

    / ** 
     * every five storage database, practical use may be 3000, then clean List, convenient memory recall 
     * / 
    Private  static  Final  int BATCH_COUNT =. 5 ; 
    List <ExcelMode> List =new ArrayList<ExcelMode>();
    private static int count = 1;
    @Override
    public void invoke(ExcelMode data, AnalysisContext context) {
        System.out.println("解析到一条数据:{ "+ data.toString() +" }");
        list.add(data);
        count ++;
        if (list.size() >= BATCH_COUNT) {
            saveData( count );
            list.clear();
        }
    }

    @Override
    public void doAfterAllAnalysed(AnalysisContext context) {
        saveData( count );
        System.out.println ("All data is parsed!" ); 
        System.out.println ( "COUNT:" + COUNT); 
    } 

    / ** 
     * plus store database 
     * / 
    Private  void the saveData ( int COUNT) { 
        System.out.println ( "{ ! "+ count +"} of data, starts to store the database "+ list.size ()); 
        System.out.println ( " stored in the database successfully! " ); 
    } 

}

Read

    // asynchronous read 
    public  static  void simpleRead1 () { 

        // read excel spreadsheet path 
        String readPath = "C: \\ Users \\ oukele \\ Desktop \\ analog data .xlsx" ; 

        the try { 
            Sheet Sheet = new new Sheet (. 1,1, ExcelMode class ); 
            EasyExcelFactory.readBySax ( new new the FileInputStream (readPath), Sheet, new new ExcelModelListener ()); 

        } the catch (a FileNotFoundException E) { 
            e.printStackTrace (); 
        } 

    }

Detailed case study Address:  https://github.com/alibaba/easyexcel/blob/master/quickstart.md

The case Address:

Guess you like

Origin www.cnblogs.com/oukele/p/11443659.html