溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

Springboot如何上傳excel并將表格數(shù)據(jù)導(dǎo)入或更新mySql數(shù)據(jù)庫

發(fā)布時間:2021-06-18 09:53:05 來源:億速云 閱讀:296 作者:小新 欄目:編程語言

這篇文章主要介紹Springboot如何上傳excel并將表格數(shù)據(jù)導(dǎo)入或更新mySql數(shù)據(jù)庫,文中介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們一定要看完!

本文主要描述,Springboot-mybatis框架下上傳excel,并將之導(dǎo)入mysql數(shù)據(jù)庫的過程,如果用戶id已存在,則進行更新修改數(shù)據(jù)庫中該項信息,由于用到的是前后端分離技術(shù),這里記錄的主要是后端java部分,通過與前端接口進行對接實現(xiàn)功能,使用layui等前端框架與之對接,也可以自己寫前端代碼,本文以Controller開始,從導(dǎo)入過程開始講述,其中包括字典表的轉(zhuǎn)換

1.在pom.xml文件中導(dǎo)入注解,主要利用POI

<dependency> 
  <groupId>org.apache.poi</groupId> 
  <artifactId>poi-ooxml</artifactId> 
  <version>3.9</version> 
</dependency> 
<dependency> 
  <groupId>commons-fileupload</groupId> 
  <artifactId>commons-fileupload</artifactId> 
  <version>1.3.1</version> 
</dependency> 
<dependency> 
  <groupId>commons-io</groupId> 
  <artifactId>commons-io</artifactId> 
  <version>2.4</version> 
</dependency>

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 { 
 Map<String, Integer> departmentMap = findDepartment(); 
 Map<String, Integer> roleMap = findRole(); 
 boolean notNull = false; 
  List<User> userList = new ArrayList<User>(); 
 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)容,感謝各位的閱讀!希望分享的內(nèi)容對大家有幫助,更多相關(guān)知識,歡迎關(guān)注億速云行業(yè)資訊頻道!

向AI問一下細節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI