ETJava Beta | Java    注册   登录
  • POI 导入excel数据

    发表于 2024-06-17 21:04:55     阅读(199)     博客类别:POI

    POI 导入excel数据

    public static void poiImport(){
            System.out.println("POI导入学生信息");
            String path="D:\\students.xlsx";
            try {
                //创建工作表对象
                Workbook workbook=new XSSFWorkbook(path);
                //获取目标sheet
                Sheet sheet = workbook.getSheetAt(0);
                //获取sheet数据总行数
                int rows = sheet.getPhysicalNumberOfRows();
                //获取sheet中所有数据
                for (int i = 1; i < rows; i++) {
                    //获取当前行对象
                    Row row = sheet.getRow(i);
                    String sname = row.getCell(0).getStringCellValue();//姓名
                    double value = row.getCell(1).getNumericCellValue();//年龄
                    Integer age = (int)value;
                    double tel = row.getCell(2).getNumericCellValue();//手机号
                    BigDecimal bigDecimal=new BigDecimal(tel);
                    String phone = bigDecimal.toString();
                    //将获取的数据封装到学生对象 sname,age,phone
                    System.out.println("sname="+sname+",age="+age+",phone="+phone);
                    //将数据保存数据库表
    
                }
                System.out.println("POI导入学生信息完毕!");
    
            } catch (Exception e) {
                e.printStackTrace();
            }
        }