java读取Excel数据并存储到MongoDB

1、导入依赖

<!-- https://mvnrepository.com/artifact/org.apache.poi/poi -->  <dependency>
    <groupId>org.apache.poi</groupId>
       <artifactId>poi</artifactId>
    <version>3.15</version>
  </dependency>
   <dependency>
    <groupId>org.apache.poi</groupId>
    <artifactId>ooxml-schemas</artifactId>
    <version>1.3</version>
   </dependency>
<!-- https://mvnrepository.com/artifact/org.apache.poi/poi-ooxml -->
  <dependency>
    <groupId>org.apache.poi</groupId>
    <artifactId>poi-ooxml</artifactId>
    <version>3.15</version>
 </dependency>

2、java读取Excel文件

int i=0;
        String excelPath = "C:\\Users\\RunWsh\\Desktop\\aaa.xlsx";
        Date date=new Date();
        try {
            //String encoding = "GBK";
            File excel = new File(excelPath);
            if (excel.isFile() && excel.exists()) {   //判断文件是否存在
 
                String[] split = excel.getName().split("\\.");  //.是特殊字符,需要转义!!!!!
                Workbook wb;
                //根据文件后缀(xls/xlsx)进行判断
                if ( "xls".equals(split[1])){
                    System.out.println(111111);
                    FileInputStream fis = new FileInputStream(excel);   //文件流对象
                    wb = new HSSFWorkbook(fis);
                }else if ("xlsx".equals(split[1])){
                    System.out.println(222222222);
                    wb = new XSSFWorkbook(excel);
 
                }else {
                    System.out.println("文件类型错误!");
                    return;
                }
 
                //开始解析
                Sheet sheet = wb.getSheetAt(0);     //读取sheet 0
 
                int firstRowIndex = sheet.getFirstRowNum()+1;   //第一行是列名,所以不读
                int lastRowIndex = sheet.getLastRowNum();
                System.out.println("firstRowIndex: "+firstRowIndex);
                System.out.println("lastRowIndex: "+lastRowIndex);
                double aaaa=0;
                for(int rIndex = firstRowIndex; rIndex <= lastRowIndex; rIndex++) {   //遍历行
                    System.out.println("rIndex: " + rIndex);
                    Row row = sheet.getRow(rIndex);
                    System.out.println(row.getCell(1).toString());
                    Query query=Query.query(Criteria.where("duty").is("手术医生").andOperator(Criteria.where("userName").is(row.getCell(2).toString())));
                    //query.addCriteria(Criteria.where("department").is(row.getCell(5).toString()));
                    //query.addCriteria(criteria);
                    List<Users> users = mongoTemplate.find(query, Users.class);
                    if (users.isEmpty()){
                        i++;
                        //System.out.println("Save!!!!!!!!"+row.getCell(2).toString());
                        Users users1=new Users();
                        users1.set_class("class");
                        users1.setDuty("手术医生");
                        users1.setEntryTime(date);
                        users1.setTitle("麻醉医生");
                        users1.setPassword("c4ca4238a0b923820dcc509a6f75849b");
                        users1.setUserCode(row.getCell(1).toString());
                        users1.setRuler("麻醉医生");
                        users1.setUserName(row.getCell(2).toString());
                        users1.setDepartment(row.getCell(5).toString());
                        users1.setPx(aaaa);
                        users1.setPy("cq");
                        mongoTemplate.save(users1,"Users");
                    }
                    else {
                        System.out.println("存在的姓名"+row.getCell(2).toString()+"工号:"+row.getCell(1).toString());
                    }
                    //Thread.sleep(50000);
                    if (row != null) {
                        int firstCellIndex = row.getFirstCellNum();
                        int lastCellIndex = row.getLastCellNum();
                        for (int cIndex = firstCellIndex; cIndex < lastCellIndex; cIndex++) {   //遍历列
                            Cell cell = row.getCell(cIndex);
 
 
                            if (cell != null) {
                                System.out.println(cell.toString());
                            }
                        }
                    }
                }
                System.out.println("多少个:"+i);
            } else {
                System.out.println("找不到指定的文件");
            }
        } catch (Exception e) {
            e.printStackTrace();
 
        }