[alibaba/easyexcel]多表头如何读取?

2024-04-26 624 views
3

实体类如下: image 读取的excel: image 执行+结果: image

为啥表头值映射到实体类中了呢?

回答

4

关键的读取的代码没有贴……我把我能成功读取的代码给你参考看看

@Data
public class MultipleData {
    @ExcelProperty({"基本信息", "a", "姓名"})
    private String name;
    @ExcelProperty({"基本信息", "b", "年龄"})
    private Integer age;
    @ExcelProperty(value = {"手机号","手机号","手机号"}, index = 2)
    private String phone;
}

监听器

@Slf4j
public class MultipleDataListener extends AnalysisEventListener<MultipleData> {
    @Override
    public void invoke(MultipleData multipleData, AnalysisContext analysisContext) {
        log.info("data = {}", multipleData);
    }

    @Override
    public void doAfterAllAnalysed(AnalysisContext analysisContext) {

    }
}

测试类

    @Test
    public void testMultiple() throws IOException {
        final ClassPathResource resource = new ClassPathResource("excel/multiple.xlsx");
        EasyExcel.read(resource.getInputStream(), MultipleData.class, new MultipleDataListener()).doReadAll();
    }

@Data是lombok的注解 image image

6

OK,可以用了,我用的是别人封装的方法,有问题

4

image 这种表格能读取吗