好得很程序员自学网

<tfoot draggable='sEl'></tfoot>

关于easyExcel中读取Excel表头的实例说明

前言

在使用easy Excel 读取文件时,对于Excel的表头,在解析读取时分成不同的状态,需要加以区分.

 

1 环境准备

准备一个可以正常访问的SpringBoot项目.

1 添加pom

        <!-- https://mvnrepository测试数据/artifact/com.alibaba/easyexcel -->
      <dependency>
          <groupId>com.alibaba</groupId>
          <artifactId>easyexcel</artifactId>
          <version>3.0.5</version>
      </dependency>
      <dependency>
          <groupId>org.springframework.boot</groupId>
          <artifactId>spring-boot-starter-web</artifactId>
      </dependency>

      <dependency>
          <groupId>org.projectlombok</groupId>
          <artifactId>lombok</artifactId>
          <optional>true</optional>
      </dependency>
      <dependency>
          <groupId>org.springframework.boot</groupId>
          <artifactId>spring-boot-starter-test</artifactId>
          <scope>test</scope>
      </dependency>

2 添加dto对象

// 表格对应实体类
@Data
public class EasyExcelDemo {

  @ExcelProperty("标题")
  private String string;
  @ExcelProperty("日期")
  private Date date;
  @ExcelProperty("金额")
  private Double money;
  /**
   * 忽略这个字段
   */
  @ExcelIgnore
  private String name;
}
// 返回对象
@Data
public class Resp {
  private List<EasyExcelDemo> importList;

}

3 准备一个控制器

@RestController
@RequestMapping("/easyExcel")
@Slf4j
public class EasyExcelController {
  
  @PostMapping("/upload")
  public void upload(@RequestParam("file") MultipartFile file) throws IOException {
  	// 读取Excel    
      EasyExcel.read(file.getInputStream(), EasyExcelDemo.class,
              new EasyExcelListener()).sheet().headRowNumber(1).doRead();

      // 从监听中获取结果集
      Resp resp = EasyExcelListener.RESP.get();
      List<EasyExcelDemo> importList = resp.getImportList();
      log.info("导入集合 list = {}", importList);
      // 清除数据
      EasyExcelListener.RESP.remove();
  }
  
}

4 准备一个监听类

@Slf4j
public class EasyExcelListener extends AnalysisEventListener<EasyExcelDemo> {

  public static List<EasyExcelDemo> importList = new ArrayList<>();
  public static final ThreadLocal<Resp> RESP = new ThreadLocal<>();

  @Override
  public void invoke(EasyExcelDemo data, AnalysisContext context) {
      log.info("解析到的一条数据: excelRow = {}", data);
      importList.add(data);
  }

  @Override
  public void doAfterAllAnalysed(AnalysisContext context) {
      // 解析完所有excel行, 保存到数据库或进行业务处理
      log.info("解析的所有数据 list = {}", importList);
      Resp resp = new Resp();
      resp.setImportList(importList);
      RESP.set(resp);
  }

  @Override
  public void invokeHeadMap(Map<Integer, String> headMap, AnalysisContext context) {
      log.info("表头数据 excelHead= {}", headMap);
  }
}

 

2 单表头Excel

单表头Excel, 即Excel的表头只有一行.

如上单行表头, 在读取时,在EasyExcel读取时设置headRowNumber属性,表示Excel的表头的行数,默认为1,设置为1时,表示第一行是表头,从第二行是表数据

 EasyExcel.read(file.getInputStream(), EasyExcelDemo.class,
              new EasyExcelListener()).sheet().headRowNumber(1).doRead();

使用postman上传excel查看结果:

读取到Excel的表头

读取到Excel的表数据

 

3 多表头Excel

多表头Excel, 即Excel的表头有多行.

如上单行表头, 在读取时,在EasyExcel读取时设置headRowNumber属性,表示Excel的表头的行数,设置为2时,表示第二和之前行都是表头.

EasyExcel.read(file.getInputStream(), EasyExcelDemo.class, new EasyExcelListener()).sheet().headRowNumber(2).doRead(); EasyExcel.read(file.getInputStream(), EasyExcelDemo.class,
              new EasyExcelListener()).sheet().headRowNumber(2).doRead();

使用postman上传excel查看结果:

读取到Excel的表头, 读取到第一行

接着读取表头第二行

读取到Excel的表数据

 

4 总结

关于EasyExcel的表格读取,使用起来比较方便,但是对于多表头和单表头的读取,需要注意,是按照一层层的解析的.即再一些特殊的场景,需要校验表格的表头是否正确等, 要注意多表头的读取按照行数顺序读取数据.

到此这篇关于easyExcel中读取Excel表头的文章就介绍到这了,更多相关easyExcel读取Excel表头内容请搜索以前的文章或继续浏览下面的相关文章希望大家以后多多支持!

原文链接:https://blog.csdn.net/ABestRookie/article/details/124136841

查看更多关于关于easyExcel中读取Excel表头的实例说明的详细内容...

  阅读:52次