导入excel,校验数据,标记错误数据
需求是导入excel表,后台校验数据后,把不正确(不符合需求)的数据标记成红色,返回表格下载链接(也可以返回表格),代码如下(代码不能全贴,见谅):
//封装返回值Map<String,Object> map = new HashMap<>();//封装查询参数Map<String,Object> params = new HashMap<>();//储存正确的对象List<String> receiptList = new ArrayList<>();//储存错误的对象List<String> excelToMapFail = new ArrayList<>(); //输入流InputStream is = file.getInputStream();//调用下面的封装方法Workbook wb = getWorkbok(file);//输出时写入格式CellStyle cellStyle = wb.createCellStyle();cellStyle.setFillForegroundColor(IndexedColors.RED.getIndex());cellStyle.setFillPattern(FillPatternType.SOLID_FOREGROUND);Sheet sheet = wb.getSheetAt(0);List<Integer> caseIndx = new ArrayList<>();//定义下标int caseNum = 0; //定义初始化数据量//获取表头数据for (int i = 0; i < 1; i++) {Row row = sheet.getRow(0);if (org.springframework.util.StringUtils.isEmpty(row)){return ResultUtil.error(500,"导入表格数据为空!");}//获取最后一列int lastNum = row.getLastCellNum();for (int j = 0; j < lastNum; j++) {Cell cell = row.getCell(j);if (org.springframework.util.StringUtils.isEmpty(cell)){return ResultUtil.error(500,"导入表格表头数据为空!");}cell.setCellType(CellType.STRING);//XXX代表的是你需要获取的表头字段,也可以用equals,//这里因需求字段不明确所以用的containsif (cell.getStringCellValue().contains("XXX")){caseIndx.add(j) ;}}}//这里还是校验,如果提供模板的话,就可以不用写这么多校验if (caseIndx.size() == 0 | caseIndx.size() > 1){return ResultUtil.error(500,"导入表格格式错误!表头中无XXX或有多列XXX!");}//循环,处理数据for (int i = 1; i <= sheet.getLastRowNum(); i++) {Row row = sheet.getRow(i);if (row == null) {continue;}//调用下方封装方法String xxx = getCellValue(caseIndx, row);if (org.springframework.util.StringUtils.isEmpty(xxx) ){if(row.getCell(caseIndx.get(0)) == null) row.createCell(caseIndx.get(0));//设置颜色row.getCell(caseIndx.get(0)).setCellStyle(cellStyle);//数值自增caseNum++;}if (!org.springframework.util.StringUtils.isEmpty(XXX)){params.put("XXX",XXX);params.put("XXX",XXX);SSSS sss= sssService.qryInfoByParams(params);if (org.springframework.util.StringUtils.isEmpty(sss)){//为空//错误数值excelToMapFail.add(xxx);//设置颜色row.getCell(caseIndx.get(0)).setCellStyle(cellStyle);}else {//正确数值receiptList.add(xxx);}}}//如果错误数值list.size>0 或者 caseNum>0 就说明有不符合要求的值if (excelToMapFail.size()>0 | caseNum>0 ){ByteArrayOutputStream outputStream = new ByteArrayOutputStream();wb.write(outputStream);byte[] finalBytes = outputStream.toByteArray();String url = "这里是返回的url,需要自己写个上传服务器,返回url的方法,或者自己直接返回excel";map.put("failFileUrl",url);is.close();outputStream.close();//把服务器文件删除,避免文件越来越多File file1 = new File("错误文件" + DateUtils.getTodayDate() + ".xls");if (file1.exists()){file1.delete();}return map;}
//文件版本获取对应的wb对象
public Workbook getWorkbok(MultipartFile file) throws IOException {Workbook wb = null;String filename = file.getOriginalFilename();if (filename.endsWith(EXCEL_XLS)) { //Excel 2003wb = new HSSFWorkbook(file.getInputStream());} else if (filename.endsWith(EXCEL_XLSX)) { // Excel 2007/2010wb = new XSSFWorkbook(file.getInputStream());}return wb;}
//获取列的值private String getCellValue(List<Integer> caseIndx, Row row) {try {return org.springframework.util.StringUtils.isEmpty(ToolUtil.getValue(row.getCell(caseIndx.get(0)))) ? "" : ToolUtil.getValue(row.getCell(caseIndx.get(0))).toString();}catch (Exception e) {return "";}}
大致就是这么回事,主要就是在//循环,处理数据,那里面写逻辑处理,然后正确的返回值和错误的返回值都保存一下,毕竟有的场景需要,不需要的话,也可以不加,在不符合需求的那个单元格设置下颜色就行了
本文来自互联网用户投稿,文章观点仅代表作者本人,不代表本站立场,不承担相关法律责任。如若转载,请注明出处。 如若内容造成侵权/违法违规/事实不符,请点击【内容举报】进行投诉反馈!
