示例#1
0
 public ActionResult BatchAdd(HttpPostedFileBase filebase)
 {
     HttpPostedFileBase file = Request.Files["files"];
     string FileName;
     string savePath;
     if (file == null || file.ContentLength <= 0)
     {
         ViewBag.error = "文件不能为空";
         return Redirect("/PersonalManger/CheckMember/AddStu");
     }
     else
     {
         string filename = Path.GetFileName(file.FileName);
         int filesize = file.ContentLength;//获取上传文件的大小单位为字节byte
         string fileEx = Path.GetExtension(filename);//获取上传文件的扩展名
         string NoFileName = Path.GetFileNameWithoutExtension(filename);//获取无扩展文件名
         int MaxSize = 8000 * 1024;//定义上传文件的最大空间大小为8M
         string FileType = ".xls,.xlsx";//定义上传文件的类型字符串
         FileName = NoFileName+ fileEx;
         if (!FileType.Contains(fileEx))
         {
             ViewBag.error = "文件类型不对,只能导入xls和xlsx格式的文件";
             return Redirect("/PersonalManger/CheckMember/AddStu");
         }
         if (filesize >= MaxSize)
         {
             ViewBag.error = "上传文件超过8M,请小于8M";
             return Redirect("/PersonalManger/CheckMember/AddStu");
         }
         string path = AppDomain.CurrentDomain.BaseDirectory + "Excel\\";
         savePath = Path.Combine(path, FileName);
         file.SaveAs(savePath);
     }
     FileStream fs = new FileStream(savePath,FileMode.Open, FileAccess.ReadWrite, FileShare.None);//创建文件流
     DataTable dt = new XlsStreamToDT(fs, 15).Xls2DT();
     using (var scope = new TransactionScope())
     {
         MODEL.T_MemberInformation member;
         for (int i = 1; i <dt.Rows.Count; i++)
         {
             member = new MODEL.T_MemberInformation();
             member.StuNum = dt.Rows[i][0].ToString();//获取学号
             member.StuName = dt.Rows[i][1].ToString();//获取姓名
             member.LoginPwd = dt.Rows[i][2].ToString();//获取初始密码
             member.QQNum = "1";
             member.Email = "*****@*****.**";
             member.PhotoPath = "../../HeadImg/final.png";
             member.JoinTime = DateTime.Now;
             member.Department = 10007;
             member.TechnicalLevel = 10003;
             member.IsAdmin = false;
             member.IsDelete = false;
             member.SecretShow = 7;
             OperateContext.Current.BLLSession.IMemberInformationBLL.Add(member);
             MODEL.T_OgnizationAct OrganizationAct = new MODEL.T_OgnizationAct() { OrganizationId =10004 , RoleActor =member.StuNum,IsDel = false };
             OperateContext.Current.BLLSession.IOgnizationActBLL.Add(OrganizationAct);
             //对每一个新成员赋予实习生的角色
             MODEL.T_RoleAct ra = new MODEL.T_RoleAct();
             ra.RoleId = 10009;
             ra.RoleActor = member.StuNum;
             ra.IsDel = false;
             OperateContext.Current.BLLSession.IRoleActBLL.Add(ra);
         }
         scope.Complete();
     }
     //删除掉存储在网站目录下的文件
     //事实上,调用File.Delete并没有删除文件,它只是让操作系统认为文件不存在,
     //文件在磁盘上的空间被标记成空的,以便用于再次使用。但是文件的数据没有被移除,您可以非常容易恢复。
     //被删除的文件直到相应的空间被重写才会真消失,这也许要很长时间。
     if (System.IO.File.Exists(savePath))
     {
         System.IO.File.Delete(savePath);
     }
     return Content("<script>alert('批量新增成功!');window.location='/PersonalManger/CheckMember/Index';</script>");
 }
示例#2
0
        public ActionResult BatchAdd(HttpPostedFileBase filebase)
        {
            HttpPostedFileBase file = Request.Files["files"];
            string             FileName;
            string             savePath;

            if (file == null || file.ContentLength <= 0)
            {
                return(Content("<script>alert('文件不能为空!');window.location='/PersonalManger/CheckMember/AddStu';</script>"));
            }
            else
            {
                string filename   = Path.GetFileName(file.FileName);
                int    filesize   = file.ContentLength;                         //获取上传文件的大小单位为字节byte
                string fileEx     = Path.GetExtension(filename);                //获取上传文件的扩展名
                string NoFileName = Path.GetFileNameWithoutExtension(filename); //获取无扩展文件名
                int    MaxSize    = 8000 * 1024;                                //定义上传文件的最大空间大小为8M
                string FileType   = ".xls,.xlsx";                               //定义上传文件的类型字符串
                FileName = NoFileName + fileEx;
                if (!FileType.Contains(fileEx))
                {
                    return(Content("<script>alert('文件类型不对,只能导入xls和xlsx格式的文件!');window.location='/PersonalManger/CheckMember/AddStu';</script>"));
                }
                if (filesize >= MaxSize)
                {
                    return(Content("<script>alert('上传文件超过8M,请小于8M!');window.location='/PersonalManger/CheckMember/AddStu';</script>"));
                }
                string path = AppDomain.CurrentDomain.BaseDirectory + "Excel\\";
                savePath = Path.Combine(path, FileName);
                file.SaveAs(savePath);
            }
            FileStream fs = new FileStream(savePath, FileMode.Open, FileAccess.ReadWrite, FileShare.None);//创建文件流
            DataTable  dt = new XlsStreamToDT(fs, 15).Xls2DT();

            using (var scope = new TransactionScope())
            {
                MODEL.T_MemberInformation member;
                for (int i = 1; i < dt.Rows.Count; i++)
                {
                    member                = new MODEL.T_MemberInformation();
                    member.StuNum         = dt.Rows[i][0].ToString(); //获取学号
                    member.StuName        = dt.Rows[i][1].ToString(); //获取姓名
                    member.LoginPwd       = dt.Rows[i][2].ToString(); //获取初始密码
                    member.QQNum          = "1";
                    member.Email          = "*****@*****.**";
                    member.PhotoPath      = "../../HeadImg/final.png";
                    member.JoinTime       = DateTime.Now;
                    member.Department     = 10007;
                    member.TechnicalLevel = 10003;
                    member.IsAdmin        = false;
                    member.IsDelete       = false;
                    member.SecretShow     = 7;
                    OperateContext.Current.BLLSession.IMemberInformationBLL.Add(member);
                    MODEL.T_OgnizationAct OrganizationAct = new MODEL.T_OgnizationAct()
                    {
                        OrganizationId = 10004, RoleActor = member.StuNum, IsDel = false
                    };
                    OperateContext.Current.BLLSession.IOgnizationActBLL.Add(OrganizationAct);
                    //对每一个新成员赋予实习生的角色
                    MODEL.T_RoleAct ra = new MODEL.T_RoleAct();
                    ra.RoleId    = 10009;
                    ra.RoleActor = member.StuNum;
                    ra.IsDel     = false;
                    OperateContext.Current.BLLSession.IRoleActBLL.Add(ra);
                }
                scope.Complete();
            }
            //删除掉存储在网站目录下的文件
            //事实上,调用File.Delete并没有删除文件,它只是让操作系统认为文件不存在,
            //文件在磁盘上的空间被标记成空的,以便用于再次使用。但是文件的数据没有被移除,您可以非常容易恢复。
            //被删除的文件直到相应的空间被重写才会真消失,这也许要很长时间。
            if (System.IO.File.Exists(savePath))
            {
                System.IO.File.Delete(savePath);
            }
            return(Content("<script>alert('批量新增成功!');window.location='/PersonalManger/CheckMember/Index';</script>"));
        }