Skip to content

一对多导入

清沐 edited this page Aug 21, 2022 · 7 revisions

4.3.0.RC版本及以上支持一对多导入,且仅限于使用SaxExcelReader导入,DefaultExcelReader不支持

示例1

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

List<Grade> multiParents = SaxExcelReader.of(Grade.class)
        .rowFilter(row -> row.getRowNum() > 0)
        .detectedMerge() // 必须要调用识别合并单元格方法,否则无效
        .read(Files.newInputStream(path));

public class Grade {
    @ExcelColumn(index=0)
    private String gradeName;

    @MultiColumn(classType=String.class)
    @ExcelColumn(index=1)
    private List<String> studentNames;

    @MultiColumn(classType=Integer.class)
    @ExcelColumn(index=2)
    private List<Integer> studentAges;
}

示例二

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

List<Grade> multiParents = SaxExcelReader.of(Grade.class)
        .rowFilter(row -> row.getRowNum() > 0)
        .detectedMerge() // 必须要调用识别合并单元格方法,否则无效
        .read(Files.newInputStream(path));

public class Grade {
    @ExcelColumn(index=0)
    private String gradeName;

    @MultiColumn(classType=Student.class)
    private List<Student> students;
}

public class Student {
   @ExcelColumn(index=1)
   private String name;

   @ExcelColumn(index=2)
   private Integer age;
}

示例三

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

List<School> multiParents = SaxExcelReader.of(School.class)
        .rowFilter(row -> row.getRowNum() > 0)
        .detectedMerge() // 必须要调用识别合并单元格方法,否则无效
        .read(Files.newInputStream(path));

public class School {
    @ExcelColumn(index=0)
    private String name;

    @MultiColumn(classType=Grade.class)
    private List<Grade> grades;
}

public class Grade {
    @ExcelColumn(index=1)
    private String gradeName;

    @MultiColumn(classType=Student.class)
    private List<Student> students;
}

public class Student {
   @ExcelColumn(index=2)
   private String name;

   @ExcelColumn(index=3)
   private Integer age;
}
Clone this wiki locally