引入依赖
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>easyexcel</artifactId>
<version>3.1.1</version>
<exclusions>
<exclusion>
<artifactId>poi-ooxml-schemas</artifactId>
<groupId>org.apache.poi</groupId>
</exclusion>
</exclusions>
</dependency>
定义实体(由于我导入数据没有oid、从name开始导入,所以需要加index标识,如果不加index标识easyExcel会默认从第一个属性开始导入)
@Data
public class SysSmsSendPersonVo {
/**
*接收人oid
*/
private Integer oid;
/**
* 接收人姓名
*/
@ExcelProperty (value = "用户名",index = 0)
private String name;
/**
* 接收人职务
*/
@ExcelProperty(value = "所属单位及职务",index = 1)
private String position;
/**
* 接收人电话
*/
@ExcelProperty(value = "电话",index = 2)
private String phone;
}
导入代码serviceImpl(建议无须自定义监听实现类,这样操作数据容易处理)
try { EasyExcel.read(file.getInputStream(), SysSmsSendPersonVo.class, new ReadListener(){ @Override public void invoke(Object object, AnalysisContext analysisContext) { SysSmsSendPersonVo sysSmsSendPersonVo = (SysSmsSendPersonVo)object; SysSmsSendPersonEntity sysSmsSendPersonEntity = new SysSmsSendPersonEntity(); BeanUtils.copyProperties(sysSmsSendPersonEntity, sysSmsSendPersonVo); sysSmsSendPersonEntity.setSmsTemplateId(templateOid); setCreator(sysSmsSendPersonEntity,sysUserVo); sysSmsSendPersonEntities.add(sysSmsSendPersonEntity); } @Override public void doAfterAllAnalysed(AnalysisContext analysisContext) { } }).sheet().doRead(); } catch (IOException e) { e.printStackTrace(); log.error("导入短信接收人信息异常"+e.toString()); }
controller层
@PostMapping("/import")
public JsonEntity<Boolean>importSysSmsTemplateSendPerson(@RequestBody MultipartFile file){
sysSmsTemplateService.importSysSmsTemplateSendPerson(file);
}