using BLL; using System; using System.Collections.Generic; using System.Data; using System.Data.OleDb; using System.IO; using System.Linq; namespace FineUIPro.Web.Person { public partial class PersonPlanIn : PageBase { #region 定义变量 /// /// 上传预设的虚拟路径 /// private string initPath = Const.ExcelUrl; /// /// 人员集合 /// public static List personPlanViews = new List(); /// /// 错误集合 /// public static string errorInfos = string.Empty; #endregion #region 加载页面 /// /// 加载页面 /// /// /// protected void Page_Load(object sender, EventArgs e) { if (!IsPostBack) { this.hdFileName.Text = string.Empty; this.hdCheckResult.Text = string.Empty; if (personPlanViews != null) { personPlanViews.Clear(); } errorInfos = string.Empty; this.Grid1.Columns[6].HeaderText = DateTime.Now.Year.ToString(); } } #endregion #region 审核 /// /// 审核 /// /// /// protected void btnAudit_Click(object sender, EventArgs e) { try { if (this.fuAttachUrl.HasFile == false) { ShowNotify("请您选择Excel文件!", MessageBoxIcon.Warning); return; } string IsXls = Path.GetExtension(this.fuAttachUrl.FileName).ToString().Trim().ToLower(); if (IsXls != ".xls") { ShowNotify("只可以选择Excel文件!", MessageBoxIcon.Warning); return; } if (personPlanViews != null) { personPlanViews.Clear(); } if (!string.IsNullOrEmpty(errorInfos)) { errorInfos = string.Empty; } string rootPath = Server.MapPath("~/"); string initFullPath = rootPath + initPath; if (!Directory.Exists(initFullPath)) { Directory.CreateDirectory(initFullPath); } this.hdFileName.Text = BLL.Funs.GetNewFileName() + IsXls; string filePath = initFullPath + this.hdFileName.Text; this.fuAttachUrl.PostedFile.SaveAs(filePath); ImportXlsToData(rootPath + initPath + this.hdFileName.Text); } catch (Exception ex) { ShowNotify("'" + ex.Message + "'", MessageBoxIcon.Warning); } } #region 读Excel提取数据 /// /// 从Excel提取数据--》Dataset /// /// Excel文件路径名 private void ImportXlsToData(string fileName) { try { personPlanViews.Clear(); string oleDBConnString = String.Empty; oleDBConnString = "Provider=Microsoft.Jet.OLEDB.4.0;"; oleDBConnString += "Data Source="; oleDBConnString += fileName; oleDBConnString += ";Extended Properties=Excel 8.0;"; OleDbConnection oleDBConn = null; OleDbDataAdapter oleAdMaster = null; DataTable m_tableName = new DataTable(); DataSet ds = new DataSet(); oleDBConn = new OleDbConnection(oleDBConnString); oleDBConn.Open(); m_tableName = oleDBConn.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, null); if (m_tableName != null && m_tableName.Rows.Count > 0) { m_tableName.TableName = m_tableName.Rows[0]["TABLE_NAME"].ToString().Trim(); } string sqlMaster; sqlMaster = " SELECT * FROM [" + m_tableName.TableName + "]"; oleAdMaster = new OleDbDataAdapter(sqlMaster, oleDBConn); oleAdMaster.Fill(ds, "m_tableName"); oleAdMaster.Dispose(); oleDBConn.Close(); oleDBConn.Dispose(); AddDatasetToSQL(ds.Tables[0]); } catch (Exception ex) { throw ex; } } #endregion #region 将Dataset的数据导入数据库 /// /// 将Dataset的数据导入数据库 /// /// 数据集 /// 数据集行数 /// private bool AddDatasetToSQL(DataTable pds) { string result = string.Empty; int ic, ir; ic = pds.Columns.Count; ir = pds.Rows.Count; if (pds != null && ir > 0) { var users = from x in Funs.DB.Sys_User select x; var departs = from x in Funs.DB.Base_Depart select x; var projects = from x in Funs.DB.Base_Project select x; for (int i = 0; i < ir; i++) { Model.View_Person_PersonPlan newSysUser = new Model.View_Person_PersonPlan { PersonPlanId = SQLHelper.GetNewID(typeof(Model.Person_PersonPlan)), DepartName = pds.Rows[i][0].ToString().Trim(), UserName = pds.Rows[i][1].ToString().Trim(), Major = pds.Rows[i][2].ToString().Trim(), ProjectName = pds.Rows[i][3].ToString().Trim(), PersonTypeName = pds.Rows[i][4].ToString().Trim(), Years = DateTime.Now.Year, Month1 = pds.Rows[i][5].ToString().Trim() == "√" ? true : false, Month2 = pds.Rows[i][6].ToString().Trim() == "√" ? true : false, Month3 = pds.Rows[i][7].ToString().Trim() == "√" ? true : false, Month4 = pds.Rows[i][8].ToString().Trim() == "√" ? true : false, Month5 = pds.Rows[i][9].ToString().Trim() == "√" ? true : false, Month6 = pds.Rows[i][10].ToString().Trim() == "√" ? true : false, Month7 = pds.Rows[i][11].ToString().Trim() == "√" ? true : false, Month8 = pds.Rows[i][12].ToString().Trim() == "√" ? true : false, Month9 = pds.Rows[i][13].ToString().Trim() == "√" ? true : false, Month10 = pds.Rows[i][14].ToString().Trim() == "√" ? true : false, Month11 = pds.Rows[i][15].ToString().Trim() == "√" ? true : false, Month12 = pds.Rows[i][16].ToString().Trim() == "√" ? true : false, Remark = pds.Rows[i][17].ToString().Trim() }; string departName = newSysUser.DepartName; if (!string.IsNullOrEmpty(departName)) { var depart = departs.FirstOrDefault(e => e.DepartName == departName); if (depart == null) { result += "第" + (i + 2).ToString() + "行," + "部门" + "," + "[" + departName + "]不在部门表中!" + "|"; } else { newSysUser.DepartId = depart.DepartId; } } else { result += "第" + (i + 2).ToString() + "行," + "部门" + "," + "此项为必填项!" + "|"; } string userName = newSysUser.UserName; if (!string.IsNullOrEmpty(userName)) { var user = users.FirstOrDefault(e => e.UserName == userName); if (user == null) { result += "第" + (i + 2).ToString() + "行," + "姓名" + "," + "[" + userName + "]不在用户表中!" + "|"; } else { newSysUser.UserId = user.UserId; } } else { result += "第" + (i + 2).ToString() + "行," + "姓名" + "," + "此项为必填项!" + "|"; } if (string.IsNullOrEmpty(newSysUser.Major)) { result += "第" + (i + 2).ToString() + "行," + "专业" + "," + "此项为必填项!" + "|"; } else { newSysUser.Major = newSysUser.Major; } string projectName = newSysUser.ProjectName; if (!string.IsNullOrEmpty(projectName)) { var project = projects.FirstOrDefault(e => e.ProjectName == projectName); if (project == null) { result += "第" + (i + 2).ToString() + "行," + "项目名称" + "," + "[" + projectName + "]不在项目列表中!" + "|"; } else { newSysUser.ProjectId = project.ProjectId; } } else { result += "第" + (i + 2).ToString() + "行," + "项目名称" + "," + "此项为必填项!" + "|"; } if (string.IsNullOrEmpty(newSysUser.PersonTypeName)) { result += "第" + (i + 2).ToString() + "行," + "人员类型" + "," + "此项为必填项!" + "|"; } else { string personTypeName = newSysUser.PersonTypeName; if (personTypeName == "正式员工") { newSysUser.PersonType = "1"; } else if (personTypeName == "外聘员工") { newSysUser.PersonType = "2"; } else if (personTypeName == "中部人才") { newSysUser.PersonType = "3"; } } //加入用户视图 personPlanViews.Add(newSysUser); } if (!string.IsNullOrEmpty(result)) { personPlanViews.Clear(); result = result.Substring(0, result.LastIndexOf("|")); errorInfos = result; Alert alert = new Alert { Message = result, Target = Target.Self }; alert.Show(); } else { errorInfos = string.Empty; if (personPlanViews.Count > 0) { this.Grid1.Hidden = false; this.Grid1.Columns[6].HeaderText = DateTime.Now.Year.ToString(); this.Grid1.DataSource = personPlanViews; this.Grid1.DataBind(); Alert.ShowInTop("审核完成,请点击保存!", MessageBoxIcon.Success); } else { Alert.ShowInTop("导入数据为空!", MessageBoxIcon.Warning); } } } else { Alert.ShowInTop("导入数据为空!", MessageBoxIcon.Warning); } return true; } #endregion #endregion #region 保存 /// /// 保存 /// /// /// protected void btnSave_Click(object sender, EventArgs e) { if (string.IsNullOrEmpty(errorInfos)) { int a = personPlanViews.Count(); int insertCount = 0; int updateCount = 0; for (int i = 0; i < a; i++) { Model.Person_PersonPlan newPersonPlan = new Model.Person_PersonPlan { DepartId = personPlanViews[i].DepartId, UserId = personPlanViews[i].UserId, Major = personPlanViews[i].Major, ProjectId = personPlanViews[i].ProjectId, PersonType = personPlanViews[i].PersonType, Years = DateTime.Now.Year, Month1 = personPlanViews[i].Month1, Month2 = personPlanViews[i].Month2, Month3 = personPlanViews[i].Month3, Month4 = personPlanViews[i].Month4, Month5 = personPlanViews[i].Month5, Month6 = personPlanViews[i].Month6, Month7 = personPlanViews[i].Month7, Month8 = personPlanViews[i].Month8, Month9 = personPlanViews[i].Month9, Month10 = personPlanViews[i].Month10, Month11 = personPlanViews[i].Month11, Month12 = personPlanViews[i].Month12, Remark = personPlanViews[i].Remark }; var getUser = Funs.DB.Person_PersonPlan.FirstOrDefault(x => x.Years == DateTime.Now.Year && x.ProjectId == personPlanViews[i].ProjectId && x.DepartId == personPlanViews[i].DepartId && x.UserId == personPlanViews[i].UserId); if (getUser == null) { newPersonPlan.PersonPlanId = SQLHelper.GetNewID(typeof(Model.Person_PersonPlan)); BLL.Person_PersonPlanService.AddPersonPlan(newPersonPlan); insertCount++; } else { getUser.DepartId = newPersonPlan.DepartId; getUser.UserId = newPersonPlan.UserId; getUser.Major = newPersonPlan.Major; getUser.ProjectId = newPersonPlan.ProjectId; getUser.PersonType = newPersonPlan.PersonType; getUser.Years = DateTime.Now.Year; getUser.Month1 = newPersonPlan.Month1; getUser.Month2 = newPersonPlan.Month2; getUser.Month3 = newPersonPlan.Month3; getUser.Month4 = newPersonPlan.Month4; getUser.Month5 = newPersonPlan.Month5; getUser.Month6 = newPersonPlan.Month6; getUser.Month7 = newPersonPlan.Month7; getUser.Month8 = newPersonPlan.Month8; getUser.Month9 = newPersonPlan.Month9; getUser.Month10 = newPersonPlan.Month10; getUser.Month11 = newPersonPlan.Month11; getUser.Month12 = newPersonPlan.Month12; getUser.Remark = newPersonPlan.Remark; Funs.DB.SubmitChanges(); updateCount++; } } string rootPath = Server.MapPath("~/"); string initFullPath = rootPath + initPath; string filePath = initFullPath + this.hdFileName.Text; if (filePath != string.Empty && File.Exists(filePath)) { File.Delete(filePath);//删除上传的XLS文件 } ShowNotify("导入完成!插入" + insertCount.ToString() + "条,更新" + updateCount.ToString() + "条记录。", MessageBoxIcon.Success); PageContext.RegisterStartupScript(ActiveWindow.GetHidePostBackReference()); } else { Alert.ShowInTop("请先将错误数据修正,再重新导入保存!", MessageBoxIcon.Warning); } } #endregion #region 关闭弹出窗口 /// /// 关闭导入弹出窗口 /// /// /// protected void Window2_Close(object sender, WindowCloseEventArgs e) { if (Session["personPlans"] != null) { personPlanViews = Session["personPlans"] as List; } if (personPlanViews.Count > 0) { this.Grid1.Hidden = false; this.Grid1.DataSource = personPlanViews; this.Grid1.DataBind(); } } #endregion #region 下载模板 /// /// 下载模板按钮 /// /// /// protected void btnDownLoad_Click(object sender, EventArgs e) { PageContext.RegisterStartupScript(Confirm.GetShowReference("确定下载导入模板吗?", String.Empty, MessageBoxIcon.Question, PageManager1.GetCustomEventReference(false, "Confirm_OK"), PageManager1.GetCustomEventReference("Confirm_Cancel"))); } /// /// 下载导入模板 /// /// /// protected void PageManager1_CustomEvent(object sender, CustomEventArgs e) { if (e.EventArgument == "Confirm_OK") { string rootPath = Server.MapPath("~/"); string filePath = Const.PersonPlanTemplateUrl; string uploadfilepath = rootPath + filePath; string fileName = Path.GetFileName(filePath); FileInfo info = new FileInfo(uploadfilepath); long fileSize = info.Length; Response.ClearContent(); Response.AddHeader("Content-Disposition", "attachment;filename=" + System.Web.HttpUtility.UrlEncode(fileName, System.Text.Encoding.UTF8)); Response.ContentType = "excel/plain"; Response.ContentEncoding = System.Text.Encoding.UTF8; Response.AddHeader("Content-Length", fileSize.ToString().Trim()); Response.TransmitFile(uploadfilepath, 0, fileSize); Response.End(); } } #endregion } }