這篇文章主要講解了“Springboot怎么上傳excel并將表格數(shù)據(jù)導(dǎo)入或更新MySQL數(shù)據(jù)庫”,文中的講解內(nèi)容簡單清晰,易于學(xué)習(xí)與理解,下面請大家跟著小編的思路慢慢深入,一起來研究和學(xué)習(xí)“Springboot怎么上傳excel并將表格數(shù)據(jù)導(dǎo)入或更新mySql數(shù)據(jù)庫”吧!
專注于為中小企業(yè)提供成都網(wǎng)站建設(shè)、成都網(wǎng)站設(shè)計服務(wù),電腦端+手機端+微信端的三站合一,更高效的管理,為中小企業(yè)紅河免費做網(wǎng)站提供優(yōu)質(zhì)的服務(wù)。我們立足成都,凝聚了一批互聯(lián)網(wǎng)行業(yè)人才,有力地推動了成百上千家企業(yè)的穩(wěn)健成長,幫助中小企業(yè)通過網(wǎng)站建設(shè)實現(xiàn)規(guī)模擴充和轉(zhuǎn)變。
1.在pom.xml文件中導(dǎo)入注解,主要利用POI
org.apache.poi poi-ooxml 3.9 commons-fileupload commons-fileupload 1.3.1 commons-io commons-io 2.4
2.Controller接口
@PostMapping("/save") public String addUser(@RequestParam("file") MultipartFile file) { String fileName = file.getOriginalFilename(); try { return sysService.batchImport(fileName, file); } catch (MyException e) { e.printStackTrace(); return e.getMessage(); }catch(Exception e){ e.printStackTrace(); return "文件異常,導(dǎo)入失敗"; } }
3.服務(wù)層接口
boolean import(String fileName, MultipartFile file) throws Exception;
4.業(yè)務(wù)層實現(xiàn)類
@Transactional(readOnly = false,rollbackFor = Exception.class) @Override public boolean import(String fileName, MultipartFile file) throws Exception { MapdepartmentMap = findDepartment(); Map roleMap = findRole(); boolean notNull = false; List userList = new ArrayList (); if (!fileName.matches("^.+\\.(?i)(xls)$") && !fileName.matches("^.+\\.(?i)(xlsx)$")) { throw new MyException("上傳文件格式不正確"); } 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; } User user; for (int r = 1; r <= sheet.getLastRowNum(); r++) { Row row = sheet.getRow(r); if (row == null){ continue; } user = new User(); if( row.getCell(0).getCellType() !=1){ throw new MyException("導(dǎo)入失敗(第"+(r+1)+"行,ID單元格格式請設(shè)為文本格式)"); } String id = row.getCell(0).getStringCellValue(); if(id==null || id.isEmpty()){ throw new MyException("導(dǎo)入失敗(第"+(r+1)+"行,ID未填寫)"); } String name = row.getCell(1).getStringCellValue(); if(name==null || name.isEmpty()){ throw new MyException("導(dǎo)入失敗(第"+(r+1)+"行,姓名未填寫)"); } String department = row.getCell(2).getStringCellValue(); if(departmentMap.get(department)==null){ throw new MyException("導(dǎo)入失敗(第"+(r+1)+"行,不存在此單位或單位未填寫)"); } String role = row.getCell(3).getStringCellValue(); if(roleMap.get(role)==null){ throw new MyException("導(dǎo)入失敗(第"+(r+1)+"行,不存在此角色或角色未填寫)"); } Date date; if(row.getCell(4).getCellType() !=0){ throw new MyException("導(dǎo)入失敗(第"+(r+1)+"行,入職日期格式不正確或未填寫)"); }else{ date = row.getCell(4).getDateCellValue(); } user.setId(id); user.setName(name); user.setDepartmentId((int) departmentMap.get(department)); user.setRoleId((int) roleMap.get(role)); user.setDate(date); userList.add(user); } for (User user : userList) { String id = user.getId(); int cnt = userMapper.selectById(id); if (cnt == 0) { userMapper.addUser(user); } else { userMapper.updateUserById(user); } } return notNull; }
感謝各位的閱讀,以上就是“Springboot怎么上傳excel并將表格數(shù)據(jù)導(dǎo)入或更新mySql數(shù)據(jù)庫”的內(nèi)容了,經(jīng)過本文的學(xué)習(xí)后,相信大家對Springboot怎么上傳excel并將表格數(shù)據(jù)導(dǎo)入或更新mySql數(shù)據(jù)庫這一問題有了更深刻的體會,具體使用情況還需要大家實踐驗證。這里是創(chuàng)新互聯(lián),小編將為大家推送更多相關(guān)知識點的文章,歡迎關(guān)注!