Java使用Apache的poi實現Excel匯入(日常總結)
阿新 • • 發佈:2018-12-05
書接上文
poi實現Excel匯入
@Override public List<Student> importExcelStudent(String xlsPath) throws IOException { List<Student> students = new ArrayList<>(); //獲得一個絕對路徑的流 FileInputStream fileInputStream = new FileInputStream(xlsPath); //新建一個excel工作薄 Workbook wb = new HSSFWorkbook(fileInputStream); //得到excel工作薄的第一個表單 Sheet sheet = wb.getSheetAt(0); //迭代表單(遍歷每一行) for (Row row : sheet) { if (row.getRowNum() == 0) { continue; } Student student = new Student(); student.setName(row.getCell(0).getStringCellValue()); student.setId(row.getCell(1).getStringCellValue()); student.setAge((int)row.getCell(2).getNumericCellValue()); student.setSex(row.getCell(3).getStringCellValue()); students.add(student); } fileInputStream.close(); return students; }