using System; using System.Collections.Generic; using System.Data; using System.Data.OleDb; using System.IO; using System.Linq; using System.Web.UI; using BLL; using Newtonsoft.Json.Linq; namespace FineUIPro.Web.HJGL.MaterialManage { public partial class EMaterialRegistIn : PageBase { #region 定义变量 /// /// 上传预设的虚拟路径 /// private string initPath = Const.ExcelUrl; /// /// 材料到货登记记录 /// public List eMaterialRegists = 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; this.drpWME_ID.DataTextField = "ConsumablesName"; this.drpWME_ID.DataValueField = "ConsumablesName"; var totalWeldMaterials = from x in Funs.DB.Base_Consumables select x; this.drpWME_ID.DataSource = totalWeldMaterials; this.drpWME_ID.DataBind(); if (eMaterialRegists != null) { eMaterialRegists.Clear(); } errorInfos = string.Empty; } } #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 (eMaterialRegists != null) { eMaterialRegists.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); //PageContext.RegisterStartupScript(Window1.GetShowReference(String.Format("PersonDataAudit.aspx?FileName={0}&ProjectId={1}", this.hdFileName.Text, Request.Params["ProjectId"], "审核 - "))); 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 { eMaterialRegists.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 questionType = from x in Funs.DB.Base_QualityQuestionType select x; for (int i = 0; i < ir; i++) { Model.HJGL_EMaterialRegist eMaterialRegist = new Model.HJGL_EMaterialRegist { EMaterialRegistId = Guid.NewGuid().ToString(), CompileDate = DateTime.Now, ProjectId = this.CurrUser.LoginProjectId }; if (!string.IsNullOrEmpty(pds.Rows[i][0].ToString().Trim())) { eMaterialRegist.EMaterialRegistCode = pds.Rows[i][0].ToString().Trim(); } else { result += "第" + (i + 2).ToString() + "行," + "运单号或车号" + "," + "此项为必填项!" + "|"; } if (!string.IsNullOrEmpty(pds.Rows[i][1].ToString().Trim())) { eMaterialRegist.SystemCode = pds.Rows[i][1].ToString().Trim(); } else { result += "第" + (i + 2).ToString() + "行," + "系统编码" + "," + "此项为必填项!" + "|"; } if (!string.IsNullOrEmpty(pds.Rows[i][2].ToString().Trim())) { eMaterialRegist.PartACode = pds.Rows[i][2].ToString().Trim(); } else { result += "第" + (i + 2).ToString() + "行," + "甲方编码" + "," + "此项为必填项!" + "|"; } if (!string.IsNullOrEmpty(pds.Rows[i][3].ToString().Trim())) { var mat = Funs.DB.Base_Consumables.FirstOrDefault(x => x.ConsumablesName == pds.Rows[i][3].ToString().Trim()); if (mat != null) { eMaterialRegist.WMT_ID = pds.Rows[i][3].ToString().Trim() ; } else { result += "第" + (i + 2).ToString() + "行," + "牌号" + pds.Rows[i][3].ToString().Trim() + "," + "不存在!" + "|"; } } else { result += "第" + (i + 2).ToString() + "行," + "牌号" + "," + "此项为必填项!" + "|"; } if (!string.IsNullOrEmpty(pds.Rows[i][4].ToString().Trim())) { eMaterialRegist.Condition = pds.Rows[i][4].ToString().Trim(); } else { result += "第" + (i + 2).ToString() + "行," + "技术条件(描述)" + "," + "此项为必填项!" + "|"; } if (!string.IsNullOrEmpty(pds.Rows[i][5].ToString().Trim())) { eMaterialRegist.Name = pds.Rows[i][5].ToString().Trim(); } else { result += "第" + (i + 2).ToString() + "行," + "名称" + "," + "此项为必填项!" + "|"; } if (!string.IsNullOrEmpty(pds.Rows[i][6].ToString().Trim())) { eMaterialRegist.SpecificationsModel = pds.Rows[i][6].ToString().Trim(); } else { result += "第" + (i + 2).ToString() + "行," + "规格型号" + "," + "此项为必填项!" + "|"; } if (!string.IsNullOrEmpty(pds.Rows[i][7].ToString().Trim())) { eMaterialRegist.Standard = pds.Rows[i][7].ToString().Trim(); } else { result += "第" + (i + 2).ToString() + "行," + "标准规范" + "," + "此项为必填项!" + "|"; } if (!string.IsNullOrEmpty(pds.Rows[i][8].ToString().Trim())) { eMaterialRegist.Material = pds.Rows[i][8].ToString().Trim(); } else { result += "第" + (i + 2).ToString() + "行," + "材质" + "," + "此项为必填项!" + "|"; } if (!string.IsNullOrEmpty(pds.Rows[i][9].ToString().Trim())) { eMaterialRegist.SpecialInstructions = pds.Rows[i][9].ToString().Trim(); } if (!string.IsNullOrEmpty(pds.Rows[i][10].ToString().Trim())) { eMaterialRegist.Unit = pds.Rows[i][10].ToString().Trim(); } else { result += "第" + (i + 2).ToString() + "行," + "单位" + "," + "此项为必填项!" + "|"; } if (!string.IsNullOrEmpty(pds.Rows[i][11].ToString().Trim())) { eMaterialRegist.Amount = pds.Rows[i][11].ToString().Trim(); } else { result += "第" + (i + 2).ToString() + "行," + "到货量" + "," + "此项为必填项!" + "|"; } if (!string.IsNullOrEmpty(pds.Rows[i][12].ToString().Trim())) { eMaterialRegist.FourLocation = pds.Rows[i][12].ToString().Trim(); } else { result += "第" + (i + 2).ToString() + "行," + "四号定位" + "," + "此项为必填项!" + "|"; } if (!string.IsNullOrEmpty(pds.Rows[i][13].ToString().Trim())) { eMaterialRegist.Manufacturer = pds.Rows[i][13].ToString().Trim(); } else { result += "第" + (i + 2).ToString() + "行," + "制造厂家" + "," + "此项为必填项!" + "|"; } if (!string.IsNullOrEmpty(pds.Rows[i][14].ToString().Trim())) { eMaterialRegist.DesignUnit = pds.Rows[i][14].ToString().Trim(); } if (!string.IsNullOrEmpty(pds.Rows[i][15].ToString().Trim())) { eMaterialRegist.RealArriveAmount = pds.Rows[i][15].ToString().Trim(); } if (!string.IsNullOrEmpty(pds.Rows[i][16].ToString().Trim())) { eMaterialRegist.OutProve = pds.Rows[i][16].ToString().Trim(); } if (!string.IsNullOrEmpty(pds.Rows[i][17].ToString().Trim())) { eMaterialRegist.OutPrices = pds.Rows[i][17].ToString().Trim(); } ///加入用户视图 eMaterialRegists.Add(eMaterialRegist); } if (!string.IsNullOrEmpty(result)) { eMaterialRegists.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 (eMaterialRegists.Count > 0) { this.Grid1.Hidden = false; this.Grid1.DataSource = eMaterialRegists; 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)) { List eMaterialRegists = new List(); if (Grid1.Rows.Count > 0) { foreach (JObject mergedRow in Grid1.GetMergedData()) { int i = mergedRow.Value("index"); GridRow row = Grid1.Rows[i]; JObject values = mergedRow.Value("values"); //System.Web.UI.WebControls.DropDownList handtype = (System.Web.UI.WebControls.DropDownList)(row.FindControl("drpHandleType")); System.Web.UI.WebControls.HiddenField EMaterialRegistId = (System.Web.UI.WebControls.HiddenField)(row.FindControl("EMaterialRegistId")); Model.HJGL_EMaterialRegist eMaterialRegist = new Model.HJGL_EMaterialRegist { EMaterialRegistId = EMaterialRegistId.Value, CompileDate = DateTime.Now, ProjectId = this.CurrUser.LoginProjectId }; eMaterialRegist.EMaterialRegistCode = values.Value("EMaterialRegistCode"); eMaterialRegist.UnitName = values.Value("UnitName"); eMaterialRegist.SystemCode = values.Value("SystemCode"); eMaterialRegist.PartACode = values.Value("PartACode"); eMaterialRegist.Condition = values.Value("Condition"); eMaterialRegist.Name = values.Value("Name"); eMaterialRegist.SpecificationsModel = values.Value("SpecificationsModel"); eMaterialRegist.Standard = values.Value("Standard"); eMaterialRegist.Material = values.Value("Material"); eMaterialRegist.SpecialInstructions = values.Value("SpecialInstructions"); eMaterialRegist.Unit = values.Value("Unit"); eMaterialRegist.Amount = values.Value("Amount"); eMaterialRegist.FourLocation = values.Value("FourLocation"); eMaterialRegist.Manufacturer = values.Value("Manufacturer"); eMaterialRegist.DesignUnit = values.Value("DesignUnit"); eMaterialRegist.RealArriveAmount = values.Value("RealArriveAmount"); eMaterialRegist.OutProve = values.Value("OutProve"); eMaterialRegist.OutPrices = values.Value("OutPrices"); eMaterialRegist.Supply = values.Value("Supply"); eMaterialRegist.CompileMan = this.CurrUser.UserId; var mat = Funs.DB.Base_Consumables.FirstOrDefault(x => x.ConsumablesName == values.Value("WMT_ID")); if (mat != null) { eMaterialRegist.WMT_ID = mat.ConsumablesId; } eMaterialRegists.Add(eMaterialRegist); Funs.DB.HJGL_EMaterialRegist.InsertOnSubmit(eMaterialRegist); BLL.HJGL_EMInventoryRecordsService.UpdateEMInventoryRecords(this.CurrUser.LoginProjectId, eMaterialRegist.Unit, eMaterialRegist.WMT_ID, "", "", int.Parse( eMaterialRegist.Amount) ); } //setHandelMan(); } Funs.DB.SubmitChanges(); 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("导入完成!插入" + eMaterialRegists.Count.ToString() + "条。", MessageBoxIcon.Success); PageContext.RegisterStartupScript(ActiveWindow.GetHidePostBackReference()); } else { Alert.ShowInTop("请先将错误数据修正,再重新导入保存!", MessageBoxIcon.Warning); } } #endregion #region 关闭弹出窗口 /// /// 关闭导入弹出窗口 /// /// /// protected void Window2_Close(object sender, WindowCloseEventArgs e) { } #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.HJGLEMaterialRegistTemplateUrl; 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(); } } protected string ConvertType(object state) { if (state != null) { if (state.ToString() == "1") { return "建筑工程"; } else if (state.ToString() == "2") { return "安装工程"; } } return ""; } protected string ConvertProjectName(object state) { if (state != null) { var type = Funs.DB.Base_QualityQuestionType.Where(u => u.QualityQuestionTypeId == state.ToString()).FirstOrDefault(); if (type != null) { return type.QualityQuestionType; } } return ""; } #endregion } }