文件导入
作者:互联网
public String importKeyPointDevice(MultipartFile file) { //生成任务id String taskid = assessTaskUtilsService.getTaskid(); try { Workbook workbook; List<TaskDevice> taskDevices = new ArrayList<>(); Map<String, List<String>> map = new HashMap<>(10); //获取文件名 String fileName = file.getOriginalFilename(); //文件名后缀 String postfix = fileName.substring(fileName.lastIndexOf(".")); //获取输入流 InputStream inputStream = file.getInputStream(); //验证文件格式 if (".xls".equalsIgnoreCase(postfix)) { workbook = new HSSFWorkbook(inputStream); } else if (".xlsx".equalsIgnoreCase(postfix)) { workbook = new XSSFWorkbook(inputStream); } else { return null; } //获取第一页表格数据 Sheet sheet = workbook.getSheetAt(0); int first = sheet.getFirstRowNum(); int last = sheet.getLastRowNum(); Date date = new Date(); //遍历每一行数据 for (int i = first + 1; i <= last; i++) { List<String> contentList = new ArrayList<>(); //获取行数据 Row row = sheet.getRow(i); //去除空行 if (null == row) { continue; } int firstCellNum = 0; int lastCellNum = 5; for (int j = firstCellNum; j < lastCellNum; j++) { //验证每行的列数据 if (j < 3) { if (null == sheet.getRow(i).getCell(j) || "".equals(sheet.getRow(i).getCell(j).toString())) { //存在未空或者null,清空contentList数据 contentList.clear(); break; } } //添加数据 if (null != sheet.getRow(i).getCell(j)) { contentList.add(sheet.getRow(i).getCell(j).toString()); } else { contentList.add(""); } } if (CollectionUtils.isNotEmpty(contentList)) { map.put("deviceInfo" + i, contentList); } } for (Map.Entry<String, List<String>> entry : map.entrySet()) { List<String> list = entry.getValue(); TaskDevice taskDevice = new TaskDevice(); taskDevice.setDeviceid(list.get(0)); taskDevice.setDevicename(list.get(1)); taskDevice.setCivilcode(list.get(2)); taskDevice.setParentid(list.get(3)); taskDevice.setPkeep(list.get(4)); taskDevice.setLabel(taskid); taskDevice.setCtime(date); taskDevice.setTaskno(""); taskDevice.setIssend(0); taskDevices.add(taskDevice); } detectionDao.insertTaskDevices(taskDevices); } catch (Exception e) { e.printStackTrace(); } return taskid; }
标签:文件,sheet,int,list,contentList,导入,new,taskDevice 来源: https://www.cnblogs.com/zhaoatian/p/13906451.html