excel导入,springboot示例
@PostMapping("/importExcel") public AjaxResult importExcel(MultipartFile file) { System.out.println("file" + file.getSize()); int addNum=0;//导入的行数 计数 try { // MultipartFile file 是用来接收前端传递过来的文件 // 创建workbook对象,读取整个文档 InputStream inputStream = file.getInputStream(); //POIFSFileSystem poifsFileSystem = new POIFSFileSystem(inputStream); XSSFWorkbook wb = new XSSFWorkbook(inputStream); // 读取页脚sheet XSSFSheet sheetAt = wb.getSheetAt(0); // 循环读取某一行 int index = 0; for (Row row : sheetAt) { // 读取每一行的单元格 if (index == 0) { index++; continue; } //创建一个对象 AppSubject subject = new AppSubject(); //题目名称 row.getCell(0).getStringCellValue() 为excel 下标0位置的值 subject.setSubjectName("<p><strong>"+row.getCell(0).getStringCellValue()+"</p></strong>"); //excel列为number类型 需要转成字符串 row.getCell(1).setCellType(CellType.STRING); //正确答案 row.getCell(1).setCellType(CellType.STRING); subject.setRightOption(row.getCell(1).getStringCellValue()); //执行自己保存的逻辑 int result= appSubjectService.importSubject(subject); //行数+1 addNum +=result; } } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } //返回导入的行数 return AjaxResult.success(addNum); }