验证中...
Languages: Java
Categories: 常用工具包
Latest update 2019-01-18 09:03
代码实现
Raw Copy
public Message uploadData(@RequestParam(value = "uploadFile", required = false) MultipartFile uploadFile,
HttpServletRequest request) throws FileNotFoundException, IOException {
logger.info("com.jryun.mhexpress.view.user.controller.userController.uploadData---start");
List<User> list = new ArrayList<User>();
try {
InputStream inputStream = new FileInputStream(multipartToFile(uploadFile));
HSSFWorkbook workbook;
workbook = new HSSFWorkbook(inputStream);// 把一张xls的数据表读到workbook里,upload是文件(MultipartFile转化为File类型)
HSSFSheet sheet = workbook.getSheetAt(0);// 读取第一页,一般一个excel文件会有三个工作表,这里获取第一个工作表来进行操作
int rows = sheet.getPhysicalNumberOfRows();// 循环遍历表sheet.getPhysicalNumberOfRows()是获取记录条数
System.out.println("一共有--------------->" + (rows - 1) + "<<条记录");
if (rows > 0) {
StringBuffer message = new StringBuffer(8);
List<HSSFPictureData> picture = workbook.getAllPictures();
Boolean pictureFlag = false;
if (picture != null && picture.size() != (rows - 1)) {
message.append("图片数量与会员数量不匹配<br>");
}else if(picture != null && picture.size() == (rows - 1)){
pictureFlag = true;
}
for (int r = 1; r < rows; r++) {
HSSFRow row = sheet.getRow(r);
User user = new User();
if (row != null) {
for (int clo = 0; clo < row.getLastCellNum(); clo++) {
if (clo == 0) {
// 昵称
row.getCell(clo).setCellType(Cell.CELL_TYPE_STRING);
if (row.getCell(clo) == null) {
return new Message(MessageEnumStatus.FAIL, MessageEnumTitle.失败, "第" + (r + 1) + "行:会员昵称不能为空<br>");
} else if (row.getCell(clo).getStringCellValue().trim().length() > 20) {
return new Message(MessageEnumStatus.FAIL, MessageEnumTitle.失败, "第" + (r + 1) + "行:会员昵称过长<br>");
} else {
user.setNick_name(row.getCell(clo).getStringCellValue().trim());
}
logger.info("com.jryun.mhexpress.view.user.controller.userController.uploadData---会员昵称添加成功");
} else if (clo == 1) {
// 真是姓名
row.getCell(clo).setCellType(Cell.CELL_TYPE_STRING);
if (row.getCell(clo) == null) {
return new Message(MessageEnumStatus.FAIL, MessageEnumTitle.失败, "第" + (r + 1) + "行:真实姓名不能为空<br>");
} else if (row.getCell(clo).getStringCellValue().trim().length() > 20) {
return new Message(MessageEnumStatus.FAIL, MessageEnumTitle.失败, "第" + (r + 1) + "行:姓名过长<br>");
} else {
user.setAccountname(row.getCell(clo).getStringCellValue().trim());
}
logger.info("com.jryun.mhexpress.view.user.controller.userController.uploadData---真实姓名添加成功");
} else if (clo == 2) {
// 性别
row.getCell(clo).setCellType(Cell.CELL_TYPE_STRING);
user.setGender(row.getCell(clo).getStringCellValue().trim());
logger.info("com.jryun.mhexpress.view.user.controller.userController.uploadData---性别添加成功");
} else if (clo == 3) {
// 电话号码
row.getCell(clo).setCellType(Cell.CELL_TYPE_STRING);
if(row.getCell(clo) == null) {
return new Message(MessageEnumStatus.FAIL, MessageEnumTitle.失败, "第" + (r + 1) + "行:手机号码不能为空<br>");
} else {
Pattern p2 = Pattern.compile("^1[3|4|5|7|8][0-9]\\d{4,8}$");// 验证手机号
if (p2.matcher(row.getCell(clo).getStringCellValue().trim()).matches() == false) {
return new Message(MessageEnumStatus.FAIL, MessageEnumTitle.失败, "第" + (r + 1) + "行:手机号格式非法<br>");
}
//会员用户名判重
List<Map<String,Object>> all = userService.repeat(user);
String user_name = row.getCell(clo).getStringCellValue().trim();
for (Map<String,Object> map:all) {
String name = ObjectTranslate.getString(map.get("mobile"));
if (user_name.equals(name)) {
return new Message(MessageEnumStatus.FAIL, MessageEnumTitle.失败, "第" + (r + 1) + "行:账号已存在<br>");
}
}
user.setMobile(row.getCell(clo).getStringCellValue().trim());
}
logger.info("com.jryun.mhexpress.view.user.controller.userController.uploadData---电话号码添加成功");
} else if (clo == 4) {
// 教育情况
row.getCell(clo).setCellType(Cell.CELL_TYPE_STRING);
user.setProfession(row.getCell(clo).getStringCellValue().trim());
logger.info("com.jryun.mhexpress.view.user.controller.userController.uploadData---教育情况添加成功");
} else if (clo == 5) {
// 初始价
row.getCell(clo).setCellType(Cell.CELL_TYPE_STRING);
user.setEmail(row.getCell(clo).getStringCellValue().trim());
logger.info("com.jryun.mhexpress.view.user.controller.userController.uploadData---初始价添加成功");
} else if (clo == 6) {
// 简介
row.getCell(clo).setCellType(Cell.CELL_TYPE_STRING);
user.setSummary(row.getCell(clo).getStringCellValue().trim());
logger.info("com.jryun.mhexpress.view.user.controller.userController.uploadData---简介添加成功");
} else if (clo == 7) {
// 用户组
row.getCell(clo).setCellType(Cell.CELL_TYPE_STRING);
user.setGroup_name(row.getCell(clo).getStringCellValue().trim());
logger.info("com.jryun.mhexpress.view.user.controller.userController.uploadData--- 用户组名称获取成功");
}
}
// 图片
if (pictureFlag && picture.get((r - 1)) != null) {
user.setUrl(OssUtil.upload(picture.get((r - 1)).getData(),
picture.get((r - 1)).suggestFileExtension(), "user"));
}
String passWord = (int)((Math.random()*1000000))+"";
logger.info(user.getMobile() + "<--------->" + passWord);
user.setPass(passWord);
list.add(user);
}
}
}
System.out.println("需要导入的数据记录数为-------->" + list.size());
} catch (Exception e) {
return new Message(MessageEnumStatus.FAIL, MessageEnumTitle.失败, "导入失败");
}
if(list.size() > 0){
userService.uploadUser(list);
//数据导入成功后,将短信发送到会员手机。
for(User userSend : list) {
SmsUtil.sendMemberInvite(userSend.getMobile(), userSend.getPass());
}
}else{
return new Message(MessageEnumStatus.FAIL, MessageEnumTitle.失败, "导入失败");
}
logger.info("com.jryun.mhexpress.view.user.controller.userController.uploadData---end");
return new Message(MessageEnumStatus.SUCESS, MessageEnumTitle.成功, "操作成功");
}

Comment list( 0 )

You need to Sign in for post a comment

Help Search

183227_9af5e6a8_1826025 111910_4d91f001_1826025