有校验手机号和邮箱以及其他的枚举校验。可以参考
Controller :
/**
* 导入用户数据
* @param file
* @return
*/
@RequestMapping(value = "/import", method = RequestMethod.POST)
public UserCentreResponse importUser(@RequestPart("file") FilePart file) {
UserCentreResponse userCentreResponse = new UserCentreResponse();
List<String> errorList = new ArrayList<>();
String fileName = null;
if (file != null) {
fileName = file.filename();//getOriginalFilename();
try {
errorList = userService.batchImport(fileName, file);
} catch (Exception e) {
log.error(e.getMessage(), e);
}
userCentreResponse.setValue("导入成功!", errorList);
return userCentreResponse;
}else {
return userCentreResponse.setError(400, "请导入文件!");
}
}
Service:
/**
* 导入用户信息
* @param fileName
* @param file
* @return
*/
@Override
@Transactional(rollbackFor = Exception.class)
public List<String> batchImport(String fileName, FilePart file) throws Exception {
boolean notNull = false;
List<User> userList = new ArrayList<User>();//用来存储user
List<String> errorList = new ArrayList<>();//存储错误信息
if (!fileName.matches("^.+\\.(?i)(xls)$") && !fileName.matches("^.+\\.(?i)(xlsx)$")) {
errorList.add("上传文件格式不正确");
throw new RuntimeException ("上传文件格式不正确");
}
boolean isExcel2003 = true;
if (fileName.matches("^.+\\.(?i)(xlsx)$")) {
isExcel2003 = false;
}
//上传的文件的临时存放目录
String tempDir=File.pathSeparator+"tmp";
StringBuilder tempFilePath=new StringBuilder(tempDir).append(File.pathSeparator).append(UUID.randomUUID())
.append("_").append(fileName);
//将上传的文件保存到临时文件
File tempFile=new File(tempFilePath.toString());
Mono<Void> voidMono = file.transferTo(tempFile);
InputStream is = new FileInputStream(tempFile);
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++) {
String errorString = "(第("+(r+1)+")行导入失败,";
Row row = sheet.getRow(r);
if (row == null){
continue;
}
user = new User();
if (row.getCell(0).getCellType() != 1){
errorList.add(errorString+"姓名请设为文本格式!)");
continue;
// throw new RuntimeException("导入失败(第("+(r+1)+")行,姓名请设为文本格式!)");
}
//用户名
String name = row.getCell(0).getStringCellValue();
if (name == null || name.isEmpty()){
errorList.add(errorString+"姓名未填写!)");
continue;
// throw new RuntimeException("导入失败(第("+(r+1)+")行,姓名未填写!)");
}
//账户类型
row.getCell(1).setCellType(Cell.CELL_TYPE_STRING);
String accountType = row.getCell(1).getStringCellValue();
//类型
Integer type = 0;
String email = row.getCell(2).getStringCellValue();
row.getCell(3).setCellType(Cell.CELL_TYPE_STRING);
String phone = row.getCell(3).getStringCellValue();
if (accountType != null || !accountType.isEmpty()){
type = AccountTypeEnum.getCode(accountType);
if (type == null){//1邮箱,2手机号
errorList.add(errorString+"账户类型填写错误!请填写{邮箱}或者{手机号}!)");
continue;
// throw new RuntimeException("导入失败(第("+(r+1)+")行,账户类型填写错误!请填写{邮箱}或者{手机号}!)");
}else if(type == 1){
//邮箱
if (email == null || email.isEmpty()){
errorList.add(errorString+"账户类型为邮箱!邮箱没有填写!)");
continue;
// throw new RuntimeException("导入失败(第("+(r+1)+")行,账户类型为邮箱!邮箱没有填写!)");
}else {
if (!CheckUtil.isEmail(email)){
errorList.add("导入失败(第("+(r+1)+")行,账户类型为邮箱!邮箱填写格式错误!)");
continue;
// throw new RuntimeException(errorString+"账户类型为邮箱!邮箱填写格式错误!)");
}
}
}else{
//手机号
if (phone == null || phone.isEmpty()){
errorList.add(errorString+"账户类型为手机号!手机号没有填写!)");
continue;
// throw new RuntimeException("导入失败(第("+(r+1)+")行,账户类型为手机号!手机号没有填写!)");
}else {
if (!CheckUtil.isPhone(phone)){
errorList.add(errorString+"账户类型为手机号!手机号填写格式错误!)");
continue;
// throw new RuntimeException("导入失败(第("+(r+1)+")行,账户类型为手机号!手机号填写格式错误!)");
}
}
}
}else {
errorList.add(errorString+"账户类型未填写!)");
continue;
// throw new RuntimeException("导入失败(第("+(r+1)+")行,账户类型未填写!)");
}
//密码
String password = null;
User userByName = this.getUserByName(name);
row.getCell(4).setCellType(Cell.CELL_TYPE_STRING);
password = row.getCell(4).getStringCellValue();
//如果是null 则必填
if (userByName == null){
if (password == null || password.isEmpty()){
errorList.add(errorString+"用户为新增,密码未填写!)");
continue;
// throw new RuntimeException("导入失败(第("+(r+1)+")行,用户为新增,密码未填写!)");
}
}else {
user.setId(userByName.getId());
}
//组织节点
String org = row.getCell(5).getStringCellValue();
List<String> orgString = Arrays.asList(org.split("-"));
Integer orgId = null;
Integer orgPid = null;
if (orgString.get(2) != null) {
Organization orgByName = organizationService.getOrgByName(orgString.get(2));
if (orgByName != null) {
orgId = orgByName.getId();
orgPid = orgByName.getPid();
Result<OrganizationDetailDto> orgInfo = organizationService.getOrgInfo(orgPid);
String orgName = orgInfo.getData().getOrgName();
String parentName = orgInfo.getData().getParentName();
if (orgString.get(0).isEmpty() || !orgString.get(0).equals(parentName) || orgString.isEmpty() || !orgString.get(1).equals(orgName)){
errorList.add(errorString+"组织链不存在!)");
continue;
// throw new RuntimeException("导入失败(第("+(r+1)+")行,组织链不存在!)");
}
}else {
errorList.add(errorString+"组织链不存在!)");
continue;
// throw new RuntimeException("导入失败(第("+(r+1)+")行,组织链不存在!)");
}
}else {
errorList.add(errorString+"组织链不存在!)");
continue;
// throw new RuntimeException("导入失败(第("+(r+1)+")行,组织链不存在!)");
}
//用户类型
String userTypeString = row.getCell(6).getStringCellValue();
Integer userType = UserTypeEnum.getCode(userTypeString);
if (userType == 0){
errorList.add(errorString+"用户类型错误!(全职/兼职))");
continue;
// throw new RuntimeException("导入失败(第("+(r+1)+")行,用户类型错误!(全职/兼职))");
}
//用户状态
String userStatusString = row.getCell(7).getStringCellValue();
Integer userStatus = UserStatusEnum.getCode(userStatusString);
if (userStatus == null){
errorList.add(errorString+"用户状态错误!(启用/禁用))");
continue;
// throw new RuntimeException("导入失败(第("+(r+1)+")行,用户状态错误!(启用/禁用))");
}
//入职时间
Date date = null;
if(row.getCell(8).getCellType() != 0){
errorList.add(errorString+"入职日期格式不正确或未填写)");
continue;
// throw new RuntimeException("导入失败(第"+(r+1)+"行,入职日期格式不正确或未填写)");
}else{
date = row.getCell(8).getDateCellValue();
}
//写入数据库
user.setName(name);//用户名
user.setAccountType(type);//账号类型
user.setEmail(email);//邮箱
user.setPhone(phone);//手机号
user.setPassword(password);//密码
user.setOrgId(orgId);//组织节点
user.setTypeId(userType);//用户类型
user.setStatus(userStatus);//用户状态
user.setEntryTime(date);//入职时间
if (userByName != null){
userMapper.updateByPrimaryKeySelective(user);
}else {
userMapper.insertSelective(user);
}
}
if (!tempFile.delete()){//删除临时文件
log.warn("删除临时文件{}失败.....",tempFilePath);
}
return errorList;
}
excel文件:
数据库结果: