Springboot怎么上传excel并将表格数据导入或更新mySql数据库
作者:ClearCorner
时间:2023-04-27
来源:互联网
浏览:0
1.在pom.xml文件中导入注解,主要利用POIorg.apache.poipoi-ooxml3.9commons-fileuploadcommons-fileupload1.3.1commons-iocommons-io2.42.Controller接口@PostMapping("/save")publicStringaddUser(@RequestParam("file")MultipartFilefile){StringfileName=file.getOriginalFilename();try
1.在pom.xml文件中导入注解,主要利用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 "文件异常,导入失败";
}
}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 departmentMap = 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("导入失败(第"+(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;
}
作者最新文章
多张图片转PDF教程:在线批量合成与顺序调整技巧
2026-09-03 10:03
AutoCAD 2014安装指南:环境检查、授权配置与首次启动设置
2026-09-02 13:34
watchstore下载软件教程:设备兼容、安装流程与常见故障
2026-09-02 13:24
PDF转Word免费方法及编辑可行性判断指南
2026-09-01 18:14
魅族20pro怎么样 魅族20pro参数配置
2026-08-25 15:42
热门文章
更多
精品专题
更多
Mac软件
更多
WINDOWS
更多
Windows 10
Windows
Windows 10 是一款微软推出的经典操作系统,拥有硬件兼容性与多任务处理能力。它更偏向把系统状态查看和常用调节动作放在一起,适合需要持续观察和微调设备状态的场景。
极度公式
Windows/macOS/Linux
极度公式是一款跨平台专业LaTeX公式识别编辑软件,支持OCR公式识别和多平台编辑。和使用说明,避免使用,享受完整功能与稳定支持。做扫描整理、文字提取和表格转换时,它能把识别后的处理步骤接得更顺,资料录入这类场景会省下不少时间。
















