springboot整合poi进行上传存进数据库中

springboot整合poi进行文件的上传

首先引入maven依赖

<dependency>
            <groupId>org.apache.poi</groupId>
            <artifactId>poi</artifactId>
            <version>3.15</version>
        </dependency>
        <dependency>
            <groupId>org.apache.poi</groupId>
            <artifactId>poi-ooxml-schemas</artifactId>
            <version>3.15</version>
        </dependency>
        <dependency>
            <groupId>org.apache.poi</groupId>
            <artifactId>poi-ooxml</artifactId>
            <version>3.15</version>
        </dependency>

在springmvc中接收文件

 @RequestMapping(value = "/requestfile",method = RequestMethod.POST)
    public ResponseEntity<ReturnedResult> addStudentBatch(@RequestParam("file") MultipartFile file) {
        Integer a = null;
        ReturnedResult result = null;
        String fileName = file.getOriginalFilename();

        try {
            a = userInfoService.batchImport(fileName, file);
            if(a>0){
                result= resultuntil.okresult(200,"success",null);
            }else if(a==0){
                result= resultuntil.okresult(404,"notfound",null);
            }
        } catch (Exception e) {
            result= resultuntil.okresult(500,"error",null);

            return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(result);
        }

在service中使用

 boolean notNull = false;
        Integer status = -1;
        List<UserInfo> resultList = new ArrayList<>();
        if (!fileName.matches("^.+\\.(?i)(xls)$") && !fileName.matches("^.+\\.(?i)(xlsx)$")) {
            String error = "上传文件格式不正确";
            status = 0;
            return status;
        }
        boolean isExcel2003 = true;
        if (fileName.matches("^.+\\.(?i)(xlsx)$")) {
            isExcel2003 = false;
        }
        InputStream is = file.getInputStream();
        Workbook wb = null;
        if (isExcel2003) {
            wb = new HSSFWorkbook(is);
        } else {
            wb = new XSSFWorkbook(is);
        }
        Sheet sheet = wb.getSheetAt(0);
        if (sheet != null) {
            notNull = true;
        }
        System.out.println(sheet.getLastRowNum());
        for (int r = 1; r <= sheet.getLastRowNum(); r++) {
            Row row = sheet.getRow(r);
            if (row == null) {
                continue;
            }
            UserInfo userInfo = new UserInfo();
            row.getCell(0).setCellType(Cell.CELL_TYPE_STRING);//设置读取转String类型
            row.getCell(1).setCellType(Cell.CELL_TYPE_STRING);
            String employeeid = row.getCell(0).getStringCellValue();
            String username = row.getCell(1).getStringCellValue();
            if (employeeid == null || username == null || ward == null || rolename == null || phone==null) {
                continue;
            }
            userInfo.setUsername(username);
            userInfo.setEmployeeid(employeeid);
            //进行保存到数据库中
            UserInfo userInfo1 = userInfodao.selectBYemployeeid(employeeid);
            if (userInfo1 == null) {
                status = userInfodao.insertintoUserinfo(userInfo);
            } else {
           logger.info(userInfo1.getEmployeeid()+"员工编号重复无法导入");
                continue;
            }
        }
        return status;
    }

完成导入.

你可能感兴趣的:(springboot整合poi进行上传存进数据库中)