Пример #1
0
        public ActionResult UploadStaffInfo(HttpPostedFileBase excelFileName)
        {
            if (excelFileName.ContentLength <= 0)
            {
                return(RedirectToAction("Index"));
            }
            string fileName = DateTime.Now.ToString("yyyyMMddHHmmss") + Path.GetFileName(excelFileName.FileName);
            string extName  = Path.GetExtension(excelFileName.FileName);

            if (extName != ".xlsx")
            {
                return(RedirectToAction("Index", new { error = enumErrorCode.FileOnlyExcel }));
            }
            string serverPath = Path.Combine(Server.MapPath("~/Uploads"), fileName);

            excelFileName.SaveAs(serverPath);
            List <StaffInfo> staffList = new List <StaffInfo>();
            enumErrorCode    result    = Excel.CheckAndReadStaffInfo(serverPath, staffList);

            if (result == enumErrorCode.HandlerSuccess)
            {
                try
                {
                    foreach (StaffInfo staff in staffList)
                    {
                        FactoryRoom tempRoom = db.FactoryRoom.FirstOrDefault(item => item.RoomNumber == staff.DeptNumber);
                        staff.DeptId   = tempRoom.RoomID;
                        staff.DeptName = tempRoom.RoomName;
                        db.StaffInfo.Add(staff);
                        //同步管理账户
                        WebSecurity.CreateUserAndAccount(staff.Number, staff.Phone);
                    }
                    db.SaveChanges();
                }
                catch
                {
                    result = enumErrorCode.ExcelContentError;
                }
            }
            return(RedirectToAction("Index", new { error = result }));
        }