1、controller层
@ApiOperation("客户导入")
@PostMapping("/import")
public Response clientImport(@RequestParam("file") MultipartFile file) {
try {
EasyExcel.read(file.getInputStream(), ExcelHeadDTO.class, new ExcelListener()).sheet().doRead();
} catch (IOException e) {
throw new RuntimeException(e);
}
return Response.ok();
}
2、表头对象
@Data
public class ExcelHeadDTO {
@ExcelProperty("表头1")
private String head1;
@ExcelProperty("表头2")
private String head2;
@ExcelProperty("表头3")
private String head3;
}
3、监听器,处理具体数据导入业务
@Component
public class ExcelListener extends AnalysisEventListener<ExcelHeadDTO> {
// Excel表数据集合
private final List<ExcelHeadDTO> dtoList = Lists.newArrayList();
// 将每行读取到的数据存入集合中
@Override
public void invoke(ExcelHeadDTO headDTO, AnalysisContext analysisContext) {
if (Objects.isNull(headDTO)) {
return;
}
dtoList.add(headDTO);
}
// 将Excel表数据写入数据库
@Override
public void doAfterAllAnalysed(AnalysisContext analysisContext) {
if (CollectionUtils.isEmpty(dtoList)) {
return;
}
dtoList.forEach( dto -> {
System.out.println(dto);
});
}
}

1万+

被折叠的 条评论
为什么被折叠?



