Esempio n. 1
0
        public void importAdmin()
        {
            string fileName = "admin20140611084350.xls";

            fileName = Path.Combine(Gc.UploadPath, "attachment", "admin", fileName);
            //Excel导出入到DataTable
            DataTable dt = UtilExcelOle.ExcelToDataTableBySheet(fileName, "Admin");

            if (dt != null)
            {
                Dictionary <string, string> dic = new Dictionary <string, string>()
                {
                    { "编号", "ID" },
                    { "部门名称", "Department_Name" },
                    { "用户名称", "Username" },
                    { "密码", "Password" },
                    { "真实姓名", "Realname" },
                    { "扮演角色", "RoletypeShow" },
                    { "视野", "SeescopeShow" },
                    { "登录次数", "LoginTimes" },
                    { "创建时间", "CommitTime" },
                    { "更新时间", "UpdateTime" }
                };

                UtilDataTable.ReplaceColumnName(dt, dic);

                //检查数据格式是否正确
                //1、检查Excel中是否有用户名重复数据
                if (UtilDataTable.HasRepeat(dt, "Username"))
                {
                    Console.WriteLine("Excel中有重复用户名");
                    return;
                }
                //2、检查Excel中用户名是否存在与数据中相同数据
                List <Admin> admins  = db.Admin.ToList();
                DataTable    tableDt = UtilDataTable.ToDataTable <Admin>(admins);
                if (UtilDataTable.HasRepeat(tableDt, dt, "Username"))
                {
                    Console.WriteLine("Excel中与数据库有重复用户名");
                    //return;
                }
                //循环插入数据
                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    Admin admin = new Admin();
                    UtilDataTable.ToObject(admin, dt.Rows[i], dt.Columns);
                    Department dep = db.Department.Where(e => e.Department_Name.Equals(admin.Department_Name)).SingleOrDefault();
                    admin.Department_ID = dep.ID;
                    admin.Seescope      = EnumSeescope.SeescopeByShow(admin.SeescopeShow);
                    admin.Roletype      = EnumRoletype.RoletypeByShow(admin.RoletypeShow);
                    //admin.CommitTime = DateTime.Now;
                    //admin.UpdateTime = DateTime.Now;
                    db.Admin.Add(admin);
                }
                db.SaveChanges();
            }
        }
        /// <summary>
        /// 导入:系统管理员
        /// </summary>
        public static JObject importAdmin(string fileName)
        {
            //Excel导出入到DataTable
            DataTable dt = UtilExcelOle.ExcelToDataTableBySheet(fileName, "Admin");

            if (dt != null)
            {
                Dictionary <string, string> dic = new Dictionary <string, string>()
                {
                    { "编号", "ID" },
                    { "部门名称", "Department_Name" },
                    { "用户名称", "Username" },
                    { "密码", "Password" },
                    { "真实姓名", "Realname" },
                    { "扮演角色", "RoletypeShow" },
                    { "视野", "SeescopeShow" },
                    { "登录次数", "LoginTimes" },
                    { "创建时间", "CommitTime" },
                    { "更新时间", "UpdateTime" }
                };

                UtilDataTable.ReplaceColumnName(dt, dic);

                //检查Excel中是否有用户名重复数据
                if (UtilDataTable.HasRepeat(dt, "Username"))
                {
                    Console.WriteLine("Excel中有重复用户名");
                    return(new JObject(
                               new JProperty("success", true),
                               new JProperty("data", false)
                               ));
                }
                //循环插入数据
                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    Admin admin = new Admin();
                    UtilDataTable.ToObject(admin, dt.Rows[i], dt.Columns);
                    Department dep = db.Department.Where(e => e.Department_Name.Equals(admin.Department_Name)).SingleOrDefault();
                    admin.Department_ID = dep.ID;
                    admin.Seescope      = EnumSeescope.SeescopeByShow(admin.SeescopeShow);
                    admin.Roletype      = EnumRoletype.RoletypeByShow(admin.RoletypeShow);
                    db.Admin.Add(admin);
                }
                db.SaveChanges();
            }
            return(new JObject(
                       new JProperty("success", true),
                       new JProperty("data", true)
                       ));
        }