0
点赞
收藏
分享

微信扫一扫

主要实现一个上传Excel读取数据的Controller的实现java的代码逻辑


1、代码实现前端上传Excel文件的一个功能模块

@RequestMapping(value = "/uploadexcel", method = RequestMethod.POST)
public CorResponse uploadExcel(@RequestParam("file") MultipartFile file, @RequestParam String eventCode) throws Exception {
CorResponse response = CorResponseBuilder.newSuccessResponse();
if (!FileCheckUtils.checkFileName(file.getOriginalFilename())) {
response = CorResponseBuilder.newErrorResponse(new CorException("不是规范的Excel文件,请检查"));
response.setMsg("不是规范的Excel文件,请检查");
return response;
}
InputStream is = null;
Workbook workbook = null;
try {
is = file.getInputStream();
} catch (IOException e) {
e.printStackTrace();
response = CorResponseBuilder.newErrorResponse(e);
}
workbook = new XSSFWorkbook(is);
Sheet sheet = workbook.getSheetAt(0);
List<TagVo> eventTagProcessVos = Lists.newArrayList();
int minRowIx = 1;// 从第二行开始算
int maxRowIx = sheet.getLastRowNum();
if (maxRowIx > MAX_NUM) {
response = CorResponseBuilder.newErrorResponse(new CorException("Excel行数超过5000条,容易处理超时,请控制行数"));
response.setMsg("Excel行数超过5000条,容易处理超时,请控制行数");
return response;
}
// 行
Integer userId = sessionService.getSessionUserId();
for (int rowIx = minRowIx; rowIx <= maxRowIx; rowIx++) {
TagVo TagVo = new TagVo();
InformationVo informationVo = new InformationVo();
Row row = sheet.getRow(rowIx);
if (null == row) {
continue;
}
if (row.getLastCellNum() == 1) {
break;
}
Cell newsTitleCell = row.getCell(0);
Cell contentCell = row.getCell(1);
String newsTitle = "";
String content = "";
try {
newsTitle = newsTitleCell.getStringCellValue().trim();
content = contentCell.getStringCellValue().trim();
} catch (Exception ex) {
//文字获取失败进行跳过,格式错误排除纯数字、纯字母的情况
continue;
}
informationVo.setContent(content);
informationVo.setNewsTitle(newsTitle);
informationVo.setCreater(userId);
informationVo.setTagFlag(0);//未标注
informationVo.setEventCode(eventCode);
informationVo.setSource(1);//来源
int count = tagFacade.countDuplication(newsTitle, eventCode, "1");
if (count > 0) { //跳过有重复
continue;
}
int infoCount = informationFacade.countLocalInformationVo(eventCode, newsTitle);
if (infoCount > 0) { //跳过有重复
continue;
}
String informationVoId = restTemplate.getForObject(IDURL, String.class);
if (StringUtils.isNotBlank(informationVoId)) {
informationVo.setId(informationVoId);
}
String infoId = informationFacade.saveLocalInformationVo(informationVo);
TagVo.setEventCode(eventCode);
TagVo.setNewsTitle(newsTitle);
TagVo.setInfoId(infoId);
TagVo.setTagFlag(0);
TagVo.setSource(1);
TagVo.setStatus(0);
TagVo.setCreater(userId);
TagVo.setUpdater(userId);
String eventVoId = restTemplate.getForObject(IDURL, String.class);
if (StringUtils.isNotBlank(eventVoId)) {
TagVo.setId(eventVoId);
}
eventTagProcessVos.add(TagVo);
}
int size = eventTagProcessVos.size();
if (size > 0) {
tagFacade.batchSaveEventTagProcessVo(eventTagProcessVos);
response.setMsg("成功保存" + eventTagProcessVos.size() + "条数据");
} else {
response = CorResponseBuilder.newErrorResponse(new CorException("数据出现重复不进行操作!"));
response.setMsg("数据出现重复不进行操作!");
return response;
}
return response;
}



举报

相关推荐

0 条评论