api接口编写
@POST
@Path("import/personnel/excel")
// @RolesAllowed({UserRoleConstants.ADMIN})
@ApiOperation(value = "根据excel和活动id导入该年已适配器材用户的相关信息", tags = {AssistEquipmentConstants.ADMIN, AssistEquipmentConstants.ASSIST_ACTIVITY_INFO})
@Consumes(MediaType.MULTIPART_FORM_DATA)
public Response importPersonnelExcel(
@FormDataParam("file") InputStream inputStream,
@FormDataParam("file") FormDataContentDisposition fileDetail,
@QueryParam("fileTypes") String fileTypes,
@QueryParam("activity_id") Long activityId) {
String fileType = fileDetail.getFileName().substring(fileDetail.getFileName().lastIndexOf(".") + 1);
verifyFileTypes(fileTypes, fileType);
assistActivityInfoService.importPersonnelExcel(inputStream, activityId);
return ok();
}
读取文件服务逻辑
@Override
public void importPersonnelExcel(InputStream inputStream, Long activityId) {
List<AssistPersonnelImportDto> newSelects = new ArrayList<>(); //数据存放实体
// 按照.class类索引读取
read(inputStream, AssistPersonnelImportDto.class, new AnalysisEventListener<AssistPersonnelImportDto>() {
@Override
public void invoke(AssistPersonnelImportDto data, AnalysisContext context) {
newSelects.add(data);
}
@Override
public void doAfterAllAnalysed(AnalysisContext context) {
log.info("读取完成");
}
//从第三行开始读取数据, 这里主要是因为有大标题和表头各占两行
}).sheet().headRowNumber(2).doRead();
if (CollectionUtils.isEmpty(newSelects)) {
return;
}
//填充到数据库逻辑
.....
}