1. 程式人生 > >springboot整合poi進行上傳存進資料庫中

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;
    }

完成匯入.