@PostMapping(value = "importLibExcel") public String uploadLibExcel(HttpServletRequest request, @RequestParam("upload")MultipartFile file, Model model) throws IOException { String msg = ""; int count = 1; byte[] bytes = file.getBytes(); Long currentTimeMillis = System.currentTimeMillis(); Path path = Paths.get("D:\\Imp_upload\\" + currentTimeMillis+"_"+file.getOriginalFilename()); Files.write(path,bytes); File f = new File(path.toString()); FileInputStream fileInputStream = new FileInputStream(f); Workbook workbook = new HSSFWorkbook(fileInputStream); fileInputStream.close(); Sheet sheet = workbook.getSheetAt(0); byte[] b = new byte[1024]; Iterator<Row> iterator = sheet.rowIterator(); if (iterator.hasNext()){ iterator.next(); } while (iterator.hasNext()){ try { Row row = iterator.next(); Cell libType = row.getCell(0); Cell libSub = row.getCell(1); Cell answerA = row.getCell(2); Cell answerB = row.getCell(3); Cell answerC = row.getCell(4); Cell answerD = row.getCell(5); Cell result = row.getCell(6); logger.info("libType:"+libType); logger.info("libSub:"+libSub); logger.info("answerA:"+answerA); logger.info("answerB:"+answerB); logger.info("answerC:"+answerC); logger.info("answerD:"+answerD); logger.info("result:"+result); count ++; }catch (Exception e){ e.printStackTrace(); msg = "在导入第"+count+"题时报错;已导入"+count+"道题"; break; } } if ("".equals(msg)){ msg = "导入成功!共导入"+count+"条试题"; } model.addAttribute("msg",msg); return "system/import_result"; }
springboot上传Excel并解析
最新推荐文章于 2024-07-25 13:23:20 发布