iis服务器助手广告广告
返回顶部
首页 > 资讯 > 数据库 >Springboot怎么上传excel并将表格数据导入或更新mySql数据库
  • 169
分享到

Springboot怎么上传excel并将表格数据导入或更新mySql数据库

2023-06-08 05:06:18 169人浏览 泡泡鱼
摘要

这篇文章主要讲解了“SpringBoot怎么上传excel并将表格数据导入或更新mySql数据库”,文中的讲解内容简单清晰,易于学习与理解,下面请大家跟着小编的思路慢慢深入,一起来研究和学习“springboot怎么上传excel并将表格数

这篇文章主要讲解了“SpringBoot怎么上传excel并将表格数据导入或更新mySql数据库”,文中的讲解内容简单清晰,易于学习与理解,下面请大家跟着小编的思路慢慢深入,一起来研究和学习“springboot怎么上传excel并将表格数据导入或更新Mysql数据库”吧!

1.在pom.xml文件中导入注解,主要利用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 "文件异常,导入失败";     } }

3.服务层接口

boolean import(String fileName, MultipartFile file) throws Exception;

4.业务层实现类

@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("导入失败(第"+(r+1)+"行,ID单元格格式请设为文本格式)");   }   String id = row.getCell(0).getStrinGCellValue();   if(id==null || id.isEmpty()){     throw new MyException("导入失败(第"+(r+1)+"行,ID未填写)");   }   String name = row.getCell(1).getStringCellValue();   if(name==null || name.isEmpty()){     throw new MyException("导入失败(第"+(r+1)+"行,姓名未填写)");   }   String department = row.getCell(2).getStringCellValue();   if(departmentMap.get(department)==null){     throw new MyException("导入失败(第"+(r+1)+"行,不存在此单位或单位未填写)");   }   String role = row.getCell(3).getStringCellValue();   if(roleMap.get(role)==null){    throw new MyException("导入失败(第"+(r+1)+"行,不存在此角色或角色未填写)");   }   Date date;   if(row.getCell(4).getCellType() !=0){    throw new MyException("导入失败(第"+(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并将表格数据导入或更新mysql数据库”的内容了,经过本文的学习后,相信大家对Springboot怎么上传excel并将表格数据导入或更新mysql数据库这一问题有了更深刻的体会,具体使用情况还需要大家实践验证。这里是编程网,小编将为大家推送更多相关知识点的文章,欢迎关注!

您可能感兴趣的文档:

--结束END--

本文标题: Springboot怎么上传excel并将表格数据导入或更新mySql数据库

本文链接: https://www.lsjlt.com/news/251894.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

本篇文章演示代码以及资料文档资料下载

下载Word文档到电脑,方便收藏和打印~

下载Word文档
猜你喜欢
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作