springBoot整合POI匯入Excel
阿新 • • 發佈:2019-01-09
//獲取一共有多少sheet,遍歷int numberOfSheets = book.getNumberOfSheets(); for (int i=0; i<numberOfSheets; i++) { Sheet sheet = book.getSheetAt(i);//獲取sheet中有多少行,遍歷每一行int physicalNumberOfRows = sheet.getPhysicalNumberOfRows(); for (int j=0;j<physicalNumberOfRows;j++){if (j == 0) {continue;//標題行} Demo demo = new Demo();Row row = sheet.getRow(j);//獲得當前行資料demo .setName(row.getCell(0).getStringCellValue()); //姓名String sex = row.getCell(1).getStringCellValue(); if("男".equals(sex)){ demo .setSex(1); //性別}else if("女".equals(sex)){ demo .setSex(0); //性別} demo .setStudyYear((long) row.getCell(2).getNumericCellValue());demoList .add(demo );} }int num = bmStudentService.addStudentBatch(studentList); if(num>0){ r.setResult(num);r.setStatus("ok");}else{ r.setResult(0);r.setStatus("error") ;} }catch (Exception e){ r.setResult(e.getClass().getName() + ":" + e.getMessage());r.setStatus("error");e.printStackTrace();}return JSON.toJSONString(r);