using BLL; using BLL.Common; using Model; using System; using System.Collections.Generic; using System.Data; using System.IO; using System.Linq; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; namespace FineUIPro.Web.EditorManage { public partial class PunchEditorIn : PageBase { #region 定义项 /// /// 上传预设的虚拟路径 /// private string initPath = Const.ExcelUrl; /// /// 错误集合 /// public static string errorInfos = string.Empty; #endregion #region 加载 /// /// 加载页面 /// /// /// protected void Page_Load(object sender, EventArgs e) { if (!IsPostBack) { } } #endregion #region 模板下载 /// /// 模板下载 /// /// /// protected void btnDownLoad_Click(object sender, EventArgs e) { PageContext.RegisterStartupScript(Confirm.GetShowReference("Are you sure to download the import template?", 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 uploadfilepath = rootPath + Const.PunchTemplateUrl; string filePath = Const.PunchTemplateUrl; 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 #region 导入 /// /// 导入 /// /// /// protected void btnImport_Click(object sender, EventArgs e) { string message = string.Empty; errorInfos = string.Empty; List punchs = new List(); try { if (this.fuAttachUrl.HasFile == false) { ShowNotify("Please select Excel file!", MessageBoxIcon.Warning); return; } string IsXls = Path.GetExtension(this.fuAttachUrl.FileName).ToString().Trim().ToLower(); if (IsXls != ".xls" && IsXls != ".xlsx") { ShowNotify("Only Excel files can be selected!", MessageBoxIcon.Warning); return; } if (punchs != null) { punchs.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); //文件上传服务器后的名称 string fileName = rootPath + initPath + this.hdFileName.Text; //读取Excel DataSet ds = NPOIHelper.ExcelToDataSet(fileName, out errorInfos, true); //验证Excel读取是否有误 if (!string.IsNullOrEmpty(errorInfos)) { ShowNotify(errorInfos, MessageBoxIcon.Warning); return; } //导入数据库 var disciplines = (from x in Funs.DB.Base_Const where x.GroupId == BLL.Const.PunchDetails_Discipline select x).ToList(); var classs = (from x in Funs.DB.Base_Const where x.GroupId == BLL.Const.Resourses_Class select x).ToList(); var users = (from x in Funs.DB.Sys_User select x).ToList(); var project = (from x in Funs.DB.Editor_EProject select x).ToList(); //var actionType = (from x in Funs.DB.Base_Const where x.GroupId == BLL.Const.Punch_ActionType select x).ToList(); string tagno = string.Empty; string p = Request.Params["eProjectId"]; if (ds.Tables.Count > 0) { for (int i = 0; i < ds.Tables[0].Rows.Count; i++) { Editor_Punch punch = new Editor_Punch(); #region 数据验证和赋值 string jobno = ds.Tables[0].Rows[i]["Job No"].ToString(); var epro = project.Where(x => x.ProjectControl_JobNo == jobno).FirstOrDefault(); if (epro != null) { if (epro.EProjectId == p) { punch.EProjectId = epro.EProjectId; if (i == 0) { tagno = BLL.SQLHelper.RunProcNewId("SpGetNewCodeByJobNo3", "dbo.Editor_Punch", "TagNo", jobno + "-"); punch.TagNo = tagno; } else { string[] ts = tagno.Split('-'); string d = (Convert.ToInt32(ts[1]) + i).ToString("000"); string t = ts[0] + "-" + d; punch.TagNo = t; } } else { errorInfos += (i + 2) + "Line, The selected [Job No] and imported [Job No] are different!
"; } } else { errorInfos += (i + 2) + "Line, [Job No] not exist!
"; } string area = ds.Tables[0].Rows[i]["Area"].ToString(); if (area != null && !string.IsNullOrEmpty(area)) { punch.Area = area; } else { errorInfos += (i + 2) + "Line, [Area] cannot be empty!
"; } string discipline = ds.Tables[0].Rows[i]["Discipline"].ToString(); if (discipline != null && !string.IsNullOrEmpty(discipline)) { var dis = disciplines.Where(x => x.ConstText == discipline).FirstOrDefault(); if (dis != null) { punch.DisciplineId = dis.ConstId; punch.DisciplineName = discipline; } else { errorInfos += (i + 2) + "Line, [Discipline] not exist!
"; } } //else //{ // errorInfos += (i + 2) + "Line, [Discipline] cannot be empty!
"; //} punch.Description = ds.Tables[0].Rows[i]["Description"].ToString(); string action = ds.Tables[0].Rows[i]["Action to be taken"].ToString(); if (action != null && !string.IsNullOrEmpty(action)) { punch.ActionType = action; //var t = actionType.Where(x => x.ConstText == action).FirstOrDefault(); //if (t != null) //{ // punch.ActionType = t.ConstValue; //} //else //{ // errorInfos += (i + 5) + "Line, [Action type] not exist!
"; //} } else { errorInfos += (i + 2) + "Line, [Action to be taken] cannot be empty!
"; } string cla = ds.Tables[0].Rows[i]["Priority"].ToString(); if (cla != null && !string.IsNullOrEmpty(cla)) { var cl = classs.Where(x => cla == (x.ConstText.Split(':')[0])).FirstOrDefault(); if (cl != null) { punch.ClassId = cl.ConstId; punch.ClassName = cl.ConstValue; } else { errorInfos += (i + 2) + "Line, [Priority] not exist!
"; } } else { errorInfos += (i + 2) + "Line, [Priority] cannot be empty!
"; } punch.RespUnitPerson = ds.Tables[0].Rows[i]["Responsible Contractor"].ToString(); punch.BYC_Person = ds.Tables[0].Rows[i]["Responsible BYC person"].ToString(); punch.InitiatedByName = ds.Tables[0].Rows[i]["Initiated By Name"].ToString(); //string byc = ds.Tables[0].Rows[i]["Responsible BYC person"].ToString(); //if (byc != null && !string.IsNullOrEmpty(byc)) //{ // var res = users.Where(x => x.Account.ToLower() == byc.ToLower()).FirstOrDefault(); // if (res != null) // { // punch.BYC_Person = res.UserId; // } //} //string initName = ds.Tables[0].Rows[i]["Initiated By Name"].ToString(); //if (initName != null && !string.IsNullOrEmpty(initName)) //{ // var res = users.Where(x => x.Account.ToLower() == initName.ToLower()).FirstOrDefault(); // if (res != null) // { // punch.InitiatedByName = res.UserId; // } //} string initDate = ds.Tables[0].Rows[i]["Initiated By Date"].ToString(); if (initDate != null && !string.IsNullOrEmpty(initDate)) { try { punch.InitiatedByDate = Funs.GetNewDateTime(initDate); } catch (Exception) { errorInfos += (i + 2) + "line,[Initiated By Date]Must be in datetime format
"; } } string planedFinishDate = ds.Tables[0].Rows[i]["Expected Finish Date"].ToString(); if (planedFinishDate != null && !string.IsNullOrEmpty(planedFinishDate)) { try { punch.PlanedFinishDate = Funs.GetNewDateTime(planedFinishDate); } catch (Exception) { errorInfos += (i + 2) + "line,[Expected Finish Date]Must be in datetime format
"; } } punch.ResiedById = CurrUser.UserId; punch.ResiedByName = CurrUser.UserName; //string writer = ds.Tables[0].Rows[i]["Input By"].ToString(); //if (writer != null && !string.IsNullOrEmpty(writer)) //{ // var res = users.Where(x => x.Account.ToLower() == writer.ToLower()).FirstOrDefault(); // if (res != null) // { // punch.ResiedById = res.UserId; // punch.ResiedByName = res.UserName; // } //} //string CTEChecker = ds.Tables[0].Rows[i]["CTE Check Name"].ToString(); //if (CTEChecker != null && !string.IsNullOrEmpty(CTEChecker)) //{ // var checker = users.Where(x => x.Account.ToLower() == CTEChecker.ToLower()).FirstOrDefault(); // if (checker != null) // { // punch.CTECheckerId = checker.UserId; // punch.CTECheckerName = checker.UserName; // } // else // { // errorInfos += (i + 2) + "Line, [CTE Checker] not exist!
"; // } //} //string ownerChecker = ds.Tables[0].Rows[i]["Owner Check Name"].ToString(); //if (ownerChecker != null && !string.IsNullOrEmpty(ownerChecker)) //{ // var owner = users.Where(x => x.Account.ToLower() == ownerChecker.ToLower()).FirstOrDefault(); // if (owner != null) // { // punch.OwnerCheckerId = owner.UserId; // punch.OwnerCheckerName = owner.UserName; // } //} punch.Remark = ds.Tables[0].Rows[i]["Remark"].ToString(); punch.PunchId = SQLHelper.GetNewID(typeof(Model.Editor_Punch)); punchs.Add(punch); #endregion } if (!string.IsNullOrEmpty(errorInfos)) { ShowNotify(errorInfos, MessageBoxIcon.Warning, 10000); return; } if (punchs.Count > 0) { Funs.DB.Editor_Punch.InsertAllOnSubmit(punchs); Funs.DB.SubmitChanges(); } ShowNotify("Import success!", MessageBoxIcon.Success); PageContext.RegisterStartupScript(ActiveWindow.GetHidePostBackReference()); } else { ShowAlert("No data!", MessageBoxIcon.Warning); return; } } catch (Exception ex) { ShowAlert("'" + ex.Message + "'", MessageBoxIcon.Warning); } } #endregion } }