Skip to content

Excel CSV import

清沐 edited this page Feb 18, 2020 · 1 revision

Import is divided into general import and sax import. The difference between them is that Sax import pays more attention to memory, uses less memory, and the Sax import function is enhanced. It is recommended to use Sax import (readable formula value)

1. General import (CSV file import is not supported, image reading is supported)

URL htmlToExcelEampleURL = this.getClass().getResource("/templates/read_example.xlsx");
Path path = Paths.get(htmlToExcelEampleURL.toURI());

// Mode 1: after reading all
List<ArtCrowd> result = DefaultExcelReader.of(ArtCrowd.class)
        .sheet(0) // 0 represents the first sheet. If it is 0, the operation can be omitted or sheet ("name") can read it
        .rowFilter(row -> row.getRowNum() > 0) // If no filtering is needed, the operation can be omitted. 0 represents the first line
        .beanFilter(ArtCrowd::isDance) // Bean filtering
        .read(path.toFile());// Can receive InputStream

// Mode 2: read one line and process one line, and you can decide the termination conditions at your own discretion
// Readthen has two rewriting interfaces. Returning a Boolean interface allows the reading to be terminated directly when returning false
DefaultExcelReader.of(ArtCrowd.class)
        .sheet(0)
        .rowFilter(row -> row.getRowNum() > 0)
        .beanFilter(ArtCrowd::isDance)
        .readThen(path.toFile() ,artCrowd -> {System.out.println(artCrowd.getName);});

public class ArtCrowd {
    // Index represents column index, starting from 0
    // Annotation free import is supported, i.e. it does not need to specify the column corresponding to the field @Excelcolumn, and will be imported in the default order of all fields
    // Can be read according to the specified title
    @ExcelColumn(index = 0)
    private String name;

    @ExcelColumn(index = 1)
    private String age;

    @ExcelColumn(index = 2,format="yyyy-MM-dd")
    private Date birthday;
}

2.Sax import (support CSV file import)

The import interface of CSV file is the same as the import interface of Excel, only the import file is different, the program will automatically distinguish

To import as map, set the import class to SaxExcelReader.Of(Map.class), the result isList<Map>, the actual type of map is LinkedHashMap, the key value is Cell, and the value value is the content value of string type

URL htmlToExcelEampleURL = this.getClass().getResource("/templates/read_example.xlsx");
Path path = Paths.get(htmlToExcelEampleURL.toURI());

// Mode 1: after reading all data, Sax mode, avoiding OOM, recommended to use a large amount of data
List<ArtCrowd> result = SaxExcelReader.of(ArtCrowd.class)
        .sheet(0) // 0 represents the first sheet. If it is 0, the operation can be omitted or sheet ("name") can be read. CSV file is invalid
        .rowFilter(row -> row.getRowNum() > 0) // If no filtering is needed, the operation can be omitted. 0 represents the first line
        .charset("GBK") // Currently, only. CSV file is valid. Set the code of current file
        .beanFilter(ArtCrowd::isDance) // Bean filtering
        .read(path.toFile());// Can receive InputStream

// Mode 2: read one line and process one line. You can decide the termination conditions by yourself. Sax mode can avoid OOM. It is recommended to use a large amount of data
// Readthen has two rewriting interfaces. Returning a Boolean interface allows the reading to be terminated directly when returning false
SaxExcelReader.of(ArtCrowd.class)
        .sheet(0)
        .rowFilter(row -> row.getRowNum() > 0)
        .charset("GBK")
        .beanFilter(ArtCrowd::isDance) 
        .readThen(path.toFile() ,artCrowd -> {System.out.println(artCrowd.getName);});

public class ArtCrowd {
   // Index represents column index, starting from 0
   // Annotation free import is supported, i.e. it does not need to specify the column corresponding to the field @Excelcolumn, and will be imported in the default order of all fields
   // Can be read according to the specified title
    @ExcelColumn(index = 0)
    private String name;

    @ExcelColumn(index = 1)
    private String age;

    @ExcelColumn(index = 2,format="yyyy-MM-dd")
    private Date birthday;
}

For details of corresponding notes, please refer to notes

For operation API, see API

Clone this wiki locally