hutool导入

@Override
public R<Boolean> excelImport(MultipartFile file, Long projectId) {
try {
InputStream is = FileMagic.prepareToCheckMagic(file.getInputStream());
FileMagic fileMagic = FileMagic.valueOf(is);
// 判断是否为excel文件
if (Objects.equals(fileMagic, FileMagic.OLE2) || Objects.equals(fileMagic, FileMagic.OOXML)) {
ExcelReader reader = ExcelUtil.getReader(file.getInputStream());
List<List<Object>> readAll = reader.read();
if (CollUtil.isNotEmpty(readAll) && readAll.size() > 1) {
List<AssessmentProjectProblem> problems=new ArrayList<>(readAll.size()-1);
Integer userIdSession=Integer.valueOf(httpSessionService.getCurrentUserId());
for (int i = 1; i < readAll.size(); i++) {
List<Object> content = readAll.get(i);
int maxSize=content.size();
AssessmentProjectProblem bean=new AssessmentProjectProblem();
bean.setProjectId(projectId);
bean.setProblemCreator(userIdSession);
bean.setProblemTitle(content.get(0)==null?"":content.get(0).toString());
if(maxSize>1){
bean.setDutyDept(content.get(1)==null?"":content.get(1).toString());
if(maxSize>2){
bean.setProblemDescribe(content.get(2)==null?"":content.get(2).toString());
if(maxSize>3){
bean.setProblemSource(content.get(3)==null?"":content.get(3).toString());
}
}
}
problems.add(bean);
}
this.saveBatch(problems);
}
} else {
return R.failed("不是excel文件!");
}
} catch (IOException e) {
e.printStackTrace();
}
return R.ok(Boolean.TRUE);
}

@Override
public R<Boolean> excelImport(MultipartFile file, Long projectId) {
try {
InputStream is = FileMagic.prepareToCheckMagic(file.getInputStream());
FileMagic fileMagic = FileMagic.valueOf(is);
// 判断是否为excel文件
if (Objects.equals(fileMagic, FileMagic.OLE2) || Objects.equals(fileMagic, FileMagic.OOXML)) {
ExcelReader reader = ExcelUtil.getReader(file.getInputStream());
List<List<Object>> readAll = reader.read();
if (CollUtil.isNotEmpty(readAll) && readAll.size() > 1) {
Map<String,Integer> priorityMap=new HashedMap<>(4);
priorityMap.put("低",1);
priorityMap.put("中",2);
priorityMap.put("高",3);
priorityMap.put("急",4);
Integer userIdSession=Integer.valueOf(httpSessionService.getCurrentUserId());
//用户idMap
Map<String, Integer> userMap=remoteUserService.getIdMapByDeptIds(projectService.getDeptIds(projectId));
for (int i = 1; i < readAll.size(); i++) {
List<Object> content = readAll.get(i);
int maxSize=content.size();
if(maxSize<6){
continue;
}
AssessmentProjectTask bean=new AssessmentProjectTask();
bean.setProjectId(projectId);
bean.setTaskCreator(userIdSession);
bean.setTaskTitle(content.get(0)==null?"":content.get(0).toString());
if(StrUtil.isNotBlank(content.get(1)==null?"":content.get(1).toString())){
QueryWrapper<AssessmentProjectTask> qw=new QueryWrapper<>();
qw.lambda().select(AssessmentProjectTask::getId);
qw.lambda().eq(AssessmentProjectTask::getProjectId, projectId).eq(AssessmentProjectTask::getTaskTitle, content.get(1).toString()).last("LIMIT 1");
AssessmentProjectTask parent=this.getOne(qw);
bean.setParentId(parent!=null ? parent.getId():0);
}
bean.setCompleteTime(DateUtil.parseDate(content.get(2)==null?DateUtil.today():content.get(2).toString()));
bean.setTaskHandler(userMap.get(content.get(3)==null?"":content.get(3).toString()));
bean.setTaskReviewer(userMap.get(content.get(4)==null?"":content.get(4).toString()));
bean.setTaskPriority(priorityMap.getOrDefault(content.get(5)==null?"":content.get(5).toString(), 1));
if(maxSize>6){
bean.setTaskDescribe(content.get(6)==null?"":content.get(6).toString());
if(maxSize>7){
bean.setTaskStandard(content.get(7)==null?"":content.get(7).toString());
}
}
this.save(bean);
}
}
} else {
return R.failed("不是excel文件!");
}
} catch (IOException e) {
e.printStackTrace();
}
return R.ok(Boolean.TRUE);
}
posted @ 2022-05-30 14:40  全琪俊  阅读(329)  评论(0编辑  收藏  举报