/** 从EXCEL导入到数据库 */ @RequestMapping(value = "/readExcel") public ModelAndView readExcel(@RequestParam(value = "excel", required = false) MultipartFile file) throws Exception { ModelAndView mv = this.getModelAndView(); PageData pd = new PageData(); if (!Jurisdiction.buttonJurisdiction(menuUrl, "add")) { return null; } if (null != file && !file.isEmpty()) { String filePath = PathUtil.getClasspath() + Const.FILEPATHFILE; // 文件上传路径 String fileName = FileUpload.fileUp(file, filePath, "userexcel"); // 执行上传 List<PageData> listPd = (List) ObjectExcelRead.readExcel(filePath, fileName, 2, 0, 0); // 执行读EXCEL操作,读出的数据导入List // 2:从第3行开始;0:从第A列开始;0:第0个sheet /* 存入数据库操作====================================== */ pd.put("RIGHTS", ""); // 权限 pd.put("LAST_LOGIN", ""); // 最后登录时间 pd.put("IP", ""); // IP pd.put("STATUS", "0"); // 状态 pd.put("SKIN", "default"); // 默认皮肤 List<Role> roleList = roleService.listAllERRoles(); // 列出所有二级角色 pd.put("ROLE_ID", roleList.get(0).getROLE_ID()); // 设置角色ID为随便第一个 /** var0 :编号 var1 :姓名 var2 :手机 var3 :邮箱 var4 :备注 */ for (int i = 0; i < listPd.size(); i++) { pd.put("USER_ID", this.get32UUID()); // ID pd.put("NAME", listPd.get(i).getString("var1")); // 姓名 String USERNAME = GetPinyin.getPingYin(listPd.get(i).getString("var1")); // 根据姓名汉字生成全拼 pd.put("USERNAME", USERNAME); if (userService.findByUId(pd) != null) { // 判断用户名是否重复 USERNAME = GetPinyin.getPingYin(listPd.get(i).getString("var1")) + Tools.getRandomNum(); pd.put("USERNAME", USERNAME); } pd.put("BZ", listPd.get(i).getString("var4")); // 备注 if (Tools.checkEmail(listPd.get(i).getString("var3"))) { // 邮箱格式不对就跳过 pd.put("EMAIL", listPd.get(i).getString("var3")); if (userService.findByUE(pd) != null) { // 邮箱已存在就跳过 continue; } } else { continue; } pd.put("NUMBER", listPd.get(i).getString("var0")); // 编号已存在就跳过 pd.put("PHONE", listPd.get(i).getString("var2")); // 手机号 pd.put("PASSWORD", new SimpleHash("SHA-1", USERNAME, "123").toString()); // 默认密码123 if (userService.findByUN(pd) != null) { continue; } userService.saveU(pd); } /* 存入数据库操作====================================== */ mv.addObject("msg", "success"); } mv.setViewName("save_result"); return mv; }
/** 发送电子邮件 */ @RequestMapping(value = "/sendEmail") @ResponseBody public Object sendEmail() { PageData pd = new PageData(); pd = this.getPageData(); Map<String, Object> map = new HashMap<String, Object>(); String msg = "ok"; // 发送状态 int count = 0; // 统计发送成功条数 int zcount = 0; // 理论条数 String strEMAIL = Tools.readTxtFile(Const.EMAIL); // 读取邮件配置 List<PageData> pdList = new ArrayList<PageData>(); String toEMAIL = pd.getString("EMAIL"); // 对方邮箱 String TITLE = pd.getString("TITLE"); // 标题 String CONTENT = pd.getString("CONTENT"); // 内容 String TYPE = pd.getString("TYPE"); // 类型 String isAll = pd.getString("isAll"); // 是否发送给全体成员 yes or no String fmsg = pd.getString("fmsg"); // 判断是系统用户还是会员 "appuser"为会员用户 if (null != strEMAIL && !"".equals(strEMAIL)) { String strEM[] = strEMAIL.split(",fh,"); if (strEM.length == 4) { if ("yes".endsWith(isAll)) { try { List<PageData> userList = new ArrayList<PageData>(); userList = "appuser".equals(fmsg) ? appuserService.listAllUser(pd) : userService.listAllUser(pd); zcount = userList.size(); try { for (int i = 0; i < userList.size(); i++) { if (Tools.checkEmail(userList.get(i).getString("EMAIL"))) { // 邮箱格式不对就跳过 SimpleMailSender.sendEmail( strEM[0], strEM[1], strEM[2], strEM[3], userList.get(i).getString("EMAIL"), TITLE, CONTENT, TYPE); // 调用发送邮件函数 count++; } else { continue; } } msg = "ok"; } catch (Exception e) { msg = "error"; } } catch (Exception e) { msg = "error"; } } else { toEMAIL = toEMAIL.replaceAll(";", ";"); toEMAIL = toEMAIL.replaceAll(" ", ""); String[] arrTITLE = toEMAIL.split(";"); zcount = arrTITLE.length; try { for (int i = 0; i < arrTITLE.length; i++) { if (Tools.checkEmail(arrTITLE[i])) { // 邮箱格式不对就跳过 SimpleMailSender.sendEmail( strEM[0], strEM[1], strEM[2], strEM[3], arrTITLE[i], TITLE, CONTENT, TYPE); // 调用发送邮件函数 count++; } else { continue; } } msg = "ok"; } catch (Exception e) { msg = "error"; } } } else { msg = "error"; } } else { msg = "error"; } pd.put("msg", msg); pd.put("count", count); // 成功数 pd.put("ecount", zcount - count); // 失败数 pdList.add(pd); map.put("list", pdList); return AppUtil.returnObject(pd, map); }