这篇文章主要介绍Springboot如何上传excel并将表格数据导入或更新mySql数据库,文中介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们一定要看完!
本文主要描述,Springboot-mybatis框架下上传excel,并将之导入mysql数据库的过程,如果用户id已存在,则进行更新修改数据库中该项信息,由于用到的是前后端分离技术,这里记录的主要是后端java部分,通过与前端接口进行对接实现功能,使用layui等前端框架与之对接,也可以自己写前端代码,本文以Controller开始,从导入过程开始讲述,其中包括字典表的转换
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数据库”这篇文章的所有内容,感谢各位的阅读!希望分享的内容对大家有帮助,更多相关知识,欢迎关注亿速云行业资讯频道!
亿速云「云数据库 MySQL」免部署即开即用,比自行安装部署数据库高出1倍以上的性能,双节点冗余防止单节点故障,数据自动定期备份随时恢复。点击查看>>
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。