public ActionResult UpLoadFile() { var result = new ResultInfoModel() { IsSuccess = false }; StringBuilder strbuild = new StringBuilder(); string FileName; string savePath; HttpPostedFileBase file = Request.Files["file"]; if (file == null || file.ContentLength <= 0) { result.Message = "please choose file"; return(Content(JsonHelper.JsonSerializer(result))); } else { string fileName = Path.GetFileName(file.FileName); int filesize = file.ContentLength; //获取上传文件的大小单位为字节byte string fileEx = Path.GetExtension(fileName); //获取上传文件的扩展名 string NoFileName = Path.GetFileNameWithoutExtension(fileName); //获取无扩展名的文件名 int Maxsize = 4000 * 1024; //定义上传文件的最大空间大小为4M string FileType = ".xls,.xlsx"; //定义上传文件的类型字符串 FileName = NoFileName + fileEx; if (!FileType.Contains(fileEx)) { result.Message = "please upload .xls and .xlsx"; return(Content(JsonHelper.JsonSerializer(result))); } if (filesize >= Maxsize) { result.Message = string.Format("file size can't big than {0}", Maxsize); return(Content(JsonHelper.JsonSerializer(result))); } string path = Server.MapPath("~/App_Data/uploads"); savePath = Path.Combine(path, FileName); file.SaveAs(savePath); } string strConn; strConn = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + savePath + ";Extended Properties=Excel 12.0;"; using (OleDbConnection conn = new OleDbConnection(strConn)) { conn.Open(); OleDbDataAdapter myCommand = new OleDbDataAdapter("select * from [Sheet1$]", strConn); DataSet myDataSet = new DataSet(); try { myCommand.Fill(myDataSet, "ExcelInfo"); } catch (Exception ex) { result.Message = ex.Message; return(Content(JsonHelper.JsonSerializer(result))); } DataTable table = myDataSet.Tables["ExcelInfo"].DefaultView.ToTable(); try { var maxJobNumber = string.Empty; for (int i = 0; i < table.Rows.Count; i++) { var model = new UserModel(); model.BUName = table.Rows[i]["ChineseName"].ToString() ?? ""; model.BUSex = 0; model.BUAvatars = string.Empty; model.BUPhoneNum = table.Rows[i]["PhoneNum"].ToString() ?? ""; model.BUEmail = table.Rows[i]["Email"].ToString() ?? ""; model.BUDepartId = 0; model.BUTitle = string.Empty; model.BUIsValid = EnabledEnum.Enabled.GetHashCode(); model.BUCreateUserNo = LoginUser.JobNum; model.BUCreateUserName = LoginUser.UserName; model.BUCreateTime = DateTime.Now; model.BUOperateUserNo = LoginUser.JobNum; model.BUOperateUserName = LoginUser.UserName; model.BUOperateTime = DateTime.Now; model.BUDepartName = table.Rows[i]["DepartName"].ToString() ?? ""; model.BUExtensionPhone = table.Rows[i]["ExtensionPhone"].ToString() ?? ""; model.BUEnglishName = table.Rows[i]["EnglishName"].ToString() ?? ""; model.BUPosition = table.Rows[i]["Position"].ToString() ?? ""; model.BUMobilePhone = table.Rows[i]["MobilePhone"].ToString() ?? ""; model.Account = table.Rows[i]["Account"].ToString() ?? ""; maxJobNumber = HomeBusiness.GenerateNewJobNumber(maxJobNumber); model.BUJobNumber = maxJobNumber; var inserResult = HomeBusiness.SaveNewUser(model); } result.IsSuccess = true; } catch (Exception ex) { result.Message = ex.Message; return(Content(JsonHelper.JsonSerializer(result))); } conn.Close(); } return(Content(JsonHelper.JsonSerializer(result))); }
/// <summary> /// /// </summary> /// <param name="model"></param> /// <returns></returns> public ActionResult UsrSave(UserView model) { var result = new ResultInfoModel { IsSuccess = true }; try { if (string.IsNullOrEmpty(model.UserId) || model.UserId.Equals("0")) { //新增 var user = new UserModel(); user.BUName = model.BUName; user.BUSex = model.BUSex; if (!string.IsNullOrWhiteSpace(model.BUAvatars)) { var path = model.BUAvatars.Substring(ServerInfo.RootURI.Length); user.BUAvatars = path; } else { user.BUAvatars = string.Empty; } user.BUPhoneNum = model.BUPhoneNum; user.BUEmail = model.BUEmail; //user.BUDepartId = int.Parse(EncryptHelper.DesDecrypt(model.DepartId)); user.BUTitle = model.BUTitle; user.BUIsValid = model.BUIsValid; user.BUCreateUserNo = LoginUser.JobNum; user.BUCreateUserName = LoginUser.UserName; user.BUCreateTime = DateTime.Now; user.BUOperateUserNo = LoginUser.JobNum;; user.BUOperateUserName = LoginUser.UserName; user.BUOperateTime = DateTime.Now; user.BUDepartName = model.BUDepartName; user.BUExtensionPhone = model.BUExtensionPhone; user.BUEnglishName = model.BUEnglishName; user.BUPosition = model.BUPosition; user.BUMobilePhone = model.BUMobilePhone; user.BUJobNumber = HomeBusiness.GenerateNewJobNumber(string.Empty); user.Account = model.Account; result = HomeBusiness.SaveNewUser(user); } else { //解密id model.UserId = EncryptHelper.DesDecrypt(model.UserId); var user = HomeBusiness.GetUserById(long.Parse(model.UserId)); user.BUName = model.BUName; user.BUSex = model.BUSex; user.BUAvatars = model.BUAvatars; user.BUPhoneNum = model.BUPhoneNum; user.BUEmail = model.BUEmail; //user.BUDepartId = int.Parse(EncryptHelper.DesDecrypt(model.DepartId)); user.BUTitle = model.BUTitle; user.BUIsValid = model.BUIsValid; user.BUOperateUserNo = LoginUser.JobNum; user.BUOperateUserName = LoginUser.UserName; //user.BUOperateUserNo = "33029"; //user.BUOperateUserName = "******"; user.BUOperateTime = DateTime.Now; user.BUDepartName = model.BUDepartName; user.BUExtensionPhone = model.BUExtensionPhone; user.BUEnglishName = model.BUEnglishName; user.BUPosition = model.BUPosition; user.BUMobilePhone = model.BUMobilePhone; user.Account = model.Account; var isExistAccount = model.IsExistAccount; result = HomeBusiness.SaveUser(user, isExistAccount); } } catch (Exception ex) { result.IsSuccess = false; result.Message = ex.Message; } return(Json(result)); }