using BLL; using MiniExcelLibs; using Model; using System; using System.Collections.Generic; using System.Data; using System.IO; using System.Linq; namespace FineUIPro.Web.HJGL.BaseInfo { public partial class MaterialCodeLibIn : PageBase { #region 定义项 /// /// 上传预设的虚拟路径 /// private string initPath = Const.ExcelUrl;//"File\\Excel\\DataIn\\"; /// /// 错误集合 /// 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("确定要下载模板吗?", 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.MaterialCodeLibTemplateUrl; string filePath = Const.MaterialCodeLibTemplateUrl; 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 codeLib = new List(); List codeLib_update = new List(); var materialcodelist =MaterialCodeLibService.GetMaterialCodeLibList(); if (this.fuAttachUrl.HasFile == false) { ShowNotify("请选择Excel文件!", MessageBoxIcon.Warning); return; } string IsXls = Path.GetExtension(this.fuAttachUrl.FileName).ToString().Trim().ToLower(); if (IsXls != ".xlsx") { ShowNotify("只能选择Excel文件!", MessageBoxIcon.Warning); return; } if (codeLib != null) { codeLib.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 rows = MiniExcel.Query(fileName).ToList(); rows = rows.Where(x => x.MaterialCode != "" && x.MaterialName != "").ToList(); if (rows.Count==0) { ShowNotify("没有数据!", MessageBoxIcon.Warning); return; } for (int i = 0; i < rows.Count; i++) { HJGL_MaterialCodeLib item = new HJGL_MaterialCodeLib(); #region 数据验证和赋值 if (rows[i].MaterialCode != null && !string.IsNullOrEmpty(rows[i].MaterialCode)) { item.MaterialCode = rows[i].MaterialCode; } else { errorInfos += (i + 2) + "Line, [元件编码] 不能为空
"; } item.MaterialDef = rows[i].MaterialDef; item.MaterialSpec = rows[i].MaterialSpec; // item.MaterialMade = dv[i]["材质"].ToString(); item.MaterialUnit = rows[i].MaterialUnit; item.MaterialName = rows[i].MaterialName; //item.PipeGrade = dv[i]["管道等级"].ToString(); //item.ProjectId = CurrUser.LoginProjectId; //= SQLHelper.GetNewID(typeof(Model.Editor_CostReport)); if (!codeLib.Select(x => x.MaterialCode).Contains(item.MaterialCode)) { if (materialcodelist.Select(x => x.MaterialCode).Contains(item.MaterialCode)) { codeLib_update.Add(item); } else { codeLib.Add(item); } } #endregion } // DataRow[] dv = ds.Tables[0].Select("元件编码 <>'' and 类型 <>''"); //导入数据库 //if (dv.Length > 0) //{ // for (int i = 0; i < dv.Length; i++) // { // HJGL_MaterialCodeLib item = new HJGL_MaterialCodeLib(); // #region 数据验证和赋值 // if (dv[i]["元件编码"] != null && !string.IsNullOrEmpty(dv[i]["元件编码"].ToString())) // { // item.MaterialCode = dv[i]["元件编码"].ToString(); // } // else // { // errorInfos += (i + 2) + "Line, [元件编码] 不能为空
"; // } // item.MaterialDef = dv[i]["材料描述"].ToString(); // item.MaterialSpec = dv[i]["规格"].ToString(); // // item.MaterialMade = dv[i]["材质"].ToString(); // item.MaterialUnit = dv[i]["单位"].ToString(); // item.MaterialName = dv[i]["类型"].ToString(); // //item.PipeGrade = dv[i]["管道等级"].ToString(); // //item.ProjectId = CurrUser.LoginProjectId; // //= SQLHelper.GetNewID(typeof(Model.Editor_CostReport)); // if (!codeLib.Select(x => x.MaterialCode).Contains(item.MaterialCode)) // { // if (materialcodelist.Select(x => x.MaterialCode).Contains(item.MaterialCode)) // { // codeLib_update.Add(item); // } // else // { // codeLib.Add(item); // } // } // #endregion // } //} //else //{ // ShowNotify("没有数据!", MessageBoxIcon.Warning); // return; //} if (!string.IsNullOrEmpty(errorInfos)) { Alert.ShowInTop(errorInfos, MessageBoxIcon.Warning); return; } codeLib = (from x in codeLib select new HJGL_MaterialCodeLib { MaterialCode=x.MaterialCode, MaterialDef=x.MaterialDef, MaterialSpec=x.MaterialSpec, MaterialUnit=x.MaterialUnit, MaterialName=x.MaterialName, }).DistinctBy(temp=> new { temp.MaterialCode, temp.MaterialDef, temp.MaterialSpec, temp.MaterialUnit, temp.MaterialName }).ToList(); codeLib_update = (from x in codeLib_update select new HJGL_MaterialCodeLib { MaterialCode = x.MaterialCode, MaterialDef = x.MaterialDef, MaterialSpec = x.MaterialSpec, MaterialUnit = x.MaterialUnit, MaterialName = x.MaterialName, }).DistinctBy(temp => new { temp.MaterialCode, temp.MaterialDef, temp.MaterialSpec, temp.MaterialUnit, temp.MaterialName }).ToList(); foreach (var item in codeLib_update) { var mewCostReport = BLL.MaterialCodeLibService.GetMaterialCodeLib(item.MaterialCode); if (mewCostReport == null) { BLL.MaterialCodeLibService.AddMaterialCodeLib(item); } else { BLL.MaterialCodeLibService.UpdateMaterialCodeLib(item); } } MaterialCodeLibService.AddBulkMaterialCodeLib(codeLib); ShowNotify("数据导入成功!", MessageBoxIcon.Success); PageContext.RegisterStartupScript(ActiveWindow.GetHidePostBackReference()); } #endregion } }