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(); } }
public JObject exportAdmin(Dictionary <String, object> condition) { var admins = db.Admin.OrderByDescending(e => e.ID); string attachment_url = ""; if (admins != null) { var query = admins.AsEnumerable(); foreach (Admin row in query) { if (row.Roletype != null) { row.RoletypeShow = EnumRoletype.RoletypeShow(Convert.ToChar(row.Roletype)); } if (row.Seescope != null) { row.SeescopeShow = EnumSeescope.SeescopeShow(Convert.ToChar(row.Seescope)); } row.Department_Name = row.Department.Department_Name; } DataTable dt = UtilDataTable.ToDataTable(query); dt.TableName = "Admin"; UtilDataTable.DeleteColumns(dt, "Seescope", "Roletype", "Department_ID"); Dictionary <string, string> dic = new Dictionary <string, string>() { { "ID", "编号" }, { "Department_Name", "部门名称" }, { "Username", "用户名称" }, { "Password", "密码" }, { "Realname", "真实姓名" }, { "RoletypeShow", "扮演角色" }, { "SeescopeShow", "视野" }, { "LoginTimes", "登录次数" }, { "CommitTime", "创建时间" }, { "UpdateTime", "更新时间" } }; UtilDataTable.ReplaceColumnName(dt, dic); string fileName = "admin" + UtilDateTime.NowS() + ".xls"; attachment_url = Gc.UploadUrl + "/attachment/admin/" + fileName; fileName = Path.Combine(Gc.UploadPath, "attachment", "admin", fileName); UtilExcelOle.DataTableToExcel(fileName, dt); } return(new JObject( new JProperty("success", true), new JProperty("data", attachment_url) )); }
/// <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) )); }
public void exportAdmin() { //获取系统管理员数据 List <Admin> admins = db.Admin.OrderByDescending(e => e.ID).Take(50).ToList(); if (admins != null) { var query = admins.AsEnumerable(); foreach (Administrator row in query) { if (row.Roletype != null) { row.RoletypeShow = EnumRoletype.RoletypeShow(Convert.ToChar(row.Roletype)); } if (row.Seescope != null) { row.SeescopeShow = EnumSeescope.SeescopeShow(Convert.ToChar(row.Seescope)); } row.Department_Name = row.Department.Department_Name; } //管理员数据导出到excel DataTable dt = UtilDataTable.ToDataTable(query); dt.TableName = "Admin"; UtilDataTable.DeleteColumns(dt, "Seescope", "Roletype", "Department_ID"); Dictionary <string, string> dic = new Dictionary <string, string>() { { "ID", "编号" }, { "Department_Name", "部门名称" }, { "Username", "用户名称" }, { "Password", "密码" }, { "Realname", "真实姓名" }, { "RoletypeShow", "扮演角色" }, { "SeescopeShow", "视野" }, { "LoginTimes", "登录次数" }, { "CommitTime", "创建时间" }, { "UpdateTime", "更新时间" } }; UtilDataTable.ReplaceColumnName(dt, dic); string fileName = Path.Combine(Gc.UploadPath, "attachment", "admin", UtilDateTime.NowS() + ".xls"); UtilExcelNpoi.DataTableToExcel(fileName, dt, "Admin"); Console.WriteLine("导出文件:" + fileName); } }
public ExtServiceAdmin queryPageAdmin(Dictionary <String, object> condition) { int StartPoint = 1, EndPoint = 15; if (condition.ContainsKey("start")) { StartPoint = Convert.ToInt16(condition["start"]) + 1; } if (condition.ContainsKey("limit")) { EndPoint = Convert.ToInt16(condition["limit"]); EndPoint = StartPoint + EndPoint - 1; } UtilDictionary.Removes(condition, "start", "limit"); if (adminService == null) { adminService = new ServiceAdmin(); } string WhereClause = FiltertoCondition(condition); int RowCount = adminService.Count(WhereClause);//总行记录数 if (RowCount > 0) { List <Admin> listAdmins = adminService.QueryPage(StartPoint, EndPoint, WhereClause).ToList <Admin>(); if (EndPoint > RowCount) { EndPoint = RowCount; } this.Stores = new List <Object>(); foreach (Admin admin in listAdmins) { admin.RoletypeShow = EnumRoletype.RoletypeShow(Convert.ToChar(admin.Roletype)); admin.SeescopeShow = EnumSeescope.SeescopeShow(Convert.ToChar(admin.Seescope)); admin.Department_Name = admin.Department.Department_Name; this.Stores.Add((Admin)ClearInclude(admin)); } } this.TotalCount = RowCount; this.Success = true; return(this); }