using Core.Mes.Client.Comm.Control; using Core.Mes.Client.Comm.Format; using Core.Mes.Client.Comm.Tool; using Core.StlMes.Client.Lims.Data.Steer.Entity; using CoreFS.CA06; using Infragistics.Win.UltraWinGrid; using System; using System.Collections; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.IO; using System.Linq; using System.Reflection; using System.Text; using System.Windows.Forms; using Infragistics.Win; using System.Text.RegularExpressions; namespace Core.StlMes.Client.Lims.Data.Steer { public partial class FrmSteelChemicalOutsourcing : FrmBase { private string[] arr = null; public FrmSteelChemicalOutsourcing() { InitializeComponent(); } private void FrmSteelChemicalOutsourcing_Load(object sender, EventArgs e) { DateTime now = DateTime.Now; DateTime dt1 = new DateTime(now.Year, now.Month, 1);//当月第一天 DateTime dt2 = dt1.AddMonths(1).AddDays(-1);//当月最后一天 this.RegStartTime.Value = DateTime.Parse(dt1.ToString("yyyy-MM-dd") + " 00:00:00"); this.RegEndTime.Value = DateTime.Parse(dt2.ToString("yyyy-MM-dd") + " 23:59:59"); EntityHelper.ShowGridCaption(ultraGrid1.DisplayLayout.Bands[0]); EntityHelper.ShowGridCaption(ultraGrid2.DisplayLayout.Bands[0]); EntityHelper.ShowGridCaption(ultraGrid3.DisplayLayout.Bands[0]); arr = this.ValidDataPurviewIds; } /// /// 重写基类方法 /// /// /// public override void ToolBar_Click(object sender, string ToolbarKey) { switch (ToolbarKey) { case "Query": doQueryData(); break; case "Add": doAdd(); break; case"Update": doUpdate(); break; case "Delete": doDelete(); break; case "GetElements": GetElements(); break; case"Export": exportData(); break; case "Close": close(); break; } } /// /// 导出 /// private void exportData() { GridHelper.ulGridToExcel(ultraGrid2, "外购坯成分导出"); } /// /// 查询 /// private void doQueryData() { string stoveNo = ""; string strRegStart = ""; string strRegEnd = ""; string judgeNo = ""; if (this.chkFurnace.Checked && this.txtFurnace.Text.Trim() != "") { stoveNo = this.txtFurnace.Text.Trim(); } if(this.chkJudge.Checked && this.txtJudgeNo.Text.Trim() != "") { judgeNo = this.txtJudgeNo.Text.Trim(); } ArrayList parm = new ArrayList(); if (chkRegTime.Checked) { strRegStart = RegStartTime.Value.ToString(); strRegEnd = RegEndTime.Value.ToString(); } parm.Add(stoveNo); parm.Add(strRegStart); parm.Add(strRegEnd); parm.Add(judgeNo); List listSource = EntityHelper.GetData( "com.steering.lims.data.steer.FrmSteelChemicalOutsourcing.doQueryData", new object[] { parm, arr }, this.ob); matGpMEntityBindingSource.DataSource = listSource; } /// /// 查询成分数据 /// private void doQueryChemical(string stoveNo) { List listSource = EntityHelper.GetData( "com.steering.lims.data.steer.FrmSteelChemicalOutsourcing.doQueryChemical", new object[] { stoveNo }, this.ob); bool isnull = false; if(listSource.Count() == 0) { isnull = true; listSource = new List() { new SteerChemAuditResultEntity() }; } SteerChemAuditResultEntitybindingSource.DataSource = listSource; if (isnull) { ultraGrid2.Rows[0].Cells["CHK"].Value = "True"; } } /// /// 新增 /// private void doAdd() { if (tabMain.SelectedTab.Index == 0) { int m = 0; this.ultraGrid2.UpdateData(); UltraGridRow uRow = this.ultraGrid1.ActiveRow; if (uRow == null) { return; } IQueryable checkMagRows = this.ultraGrid2.Rows.AsQueryable().Where(" CHK = 'True'"); if (checkMagRows.Count() == 0) { MessageUtil.ShowTips("请选择录入的化学成分数据!"); return; } ArrayList parm = new ArrayList(); foreach (UltraGridRow row in checkMagRows) { if (!row.Cells["SampleNo"].Text.Trim().Equals("")) { if (row.Cells["SampleNo"].Text.Trim().Length >= 3) { if (!row.Cells["SampleNo"].Text.Trim().Substring(0, 3).Equals("601")) { MessageUtil.ShowTips("试样号必须601开头!"); return; } } else { MessageUtil.ShowTips("试样号必须601开头!"); return; } } else { MessageUtil.ShowTips("试样号不能为空!"); return; } if (!doNumber(row, ultraGrid2)) { m = m + 1; } SteerChemAuditResultEntity entity = (SteerChemAuditResultEntity) row.ListObject; string steelEntity = JSONFormat.Format(entity); parm.Add(steelEntity); } if(m > 0) { return; } if (MessageUtil.ShowYesNoAndQuestion("是否新增该化成分数据?") == DialogResult.No) { return; } CoreClientParam ccp = new CoreClientParam(); ccp.ServerName = "com.steering.lims.data.steer.FrmSteelChemicalOutsourcing"; ccp.MethodName = "doAdd"; ccp.ServerParams = new object[] {parm, uRow.Cells["GRADENAME"].Text, uRow.Cells["StoveNo"].Text}; ccp = ob.ExecuteNonQuery(ccp, CoreInvokeType.Internal); if (ccp.ReturnCode != -1) { if (ccp.ReturnInfo.Equals("新增成功!")) { doQueryData(); MessageUtil.ShowTips(ccp.ReturnInfo); } else { MessageUtil.ShowTips(ccp.ReturnInfo); } } } else { this.ultraGrid3.UpdateData(); int n = 0; IQueryable checkMagRows = this.ultraGrid3.Rows.AsQueryable().Where(" CHK = 'True'"); if (checkMagRows.Count() == 0) { MessageUtil.ShowTips("请选择录入的化学成分数据!"); return; } ArrayList parm = new ArrayList(); foreach (UltraGridRow row in checkMagRows) { if (!row.Cells["SampleNo"].Text.Trim().Equals("")) { if (row.Cells["SampleNo"].Text.Trim().Length >= 3) { if (!row.Cells["SampleNo"].Text.Trim().Substring(0, 3).Equals("601")) { MessageUtil.ShowTips("试样号必须601开头!"); return; } } else { MessageUtil.ShowTips("试样号必须601开头!"); return; } } else { MessageUtil.ShowTips("试样号不能为空!"); return; } if (!doNumber(row, ultraGrid3)) { n = n + 1; } SteerChemAuditResultEntity entity = (SteerChemAuditResultEntity)row.ListObject; string steelEntity = JSONFormat.Format(entity); parm.Add(steelEntity); } if (n > 0) { return; } if (MessageUtil.ShowYesNoAndQuestion("是否新增该化成分数据?") == DialogResult.No) { return; } CoreClientParam ccp = new CoreClientParam(); ccp.ServerName = "com.steering.lims.data.steer.FrmSteelChemicalOutsourcing"; ccp.MethodName = "doAdd"; ccp.ServerParams = new object[] { parm }; ccp = ob.ExecuteNonQuery(ccp, CoreInvokeType.Internal); if (ccp.ReturnCode != -1) { if (ccp.ReturnInfo.Contains("新增成功!")) { doQueryData(); MessageUtil.ShowTips(ccp.ReturnInfo); } else { MessageUtil.ShowTips(ccp.ReturnInfo); } } } } /// /// 验证是否为小数 /// /// private bool doNumber(UltraGridRow row,UltraGrid ug) { for (int i = 1; i < ug.DisplayLayout.Bands[0].Columns.Count;i++) { if (row.Cells[i].Text != "" && row.Cells[i].Column.Key != "SampleNo" && row.Cells[i].Column.Key != "HeatNo" &&row.Cells[i].Column.Key != "CHK" ) { if (!StringUtil.IsDouble(row.Cells[i].Text.ToString3())) { MessageUtil.ShowTips("元素值应该为小数!" + row.Cells[i].Column.Key); return false; } } } return true; } /// /// 修改 /// private void doUpdate() { this.ultraGrid2.UpdateData(); this.ultraGrid1.UpdateData(); IQueryable checkMagRows = this.ultraGrid2.Rows.AsQueryable().Where(" CHK = 'True'"); if (checkMagRows.Count() == 0) { MessageUtil.ShowTips("请选择需要修改的化学成分数据!"); return; } UltraGridRow row = this.ultraGrid1.ActiveRow; MatGpMEntity matGp = (MatGpMEntity)row.ListObject; string matGpEntity = JSONFormat.Format(matGp); ArrayList parm = new ArrayList(); foreach(UltraGridRow uRow in checkMagRows) { SteerChemAuditResultEntity entity = (SteerChemAuditResultEntity)uRow.ListObject; string steelEntity = JSONFormat.Format(entity); parm.Add(steelEntity); } if (MessageUtil.ShowYesNoAndQuestion("是否修改该化成分数据?") == DialogResult.No) { return; } CoreClientParam ccp = new CoreClientParam(); ccp.ServerName = "com.steering.lims.data.steer.FrmSteelChemicalOutsourcing"; ccp.MethodName = "doUpdate"; ccp.ServerParams = new object[] { parm, matGpEntity }; ccp = ob.ExecuteNonQuery(ccp, CoreInvokeType.Internal); if (ccp.ReturnCode != -1) { if (ccp.ReturnInfo.Equals("修改成功!")) { doQueryData(); MessageUtil.ShowTips(ccp.ReturnInfo); } else { MessageUtil.ShowTips(ccp.ReturnInfo); } } } /// /// 删除 /// private void doDelete() { this.ultraGrid2.UpdateData(); this.ultraGrid1.UpdateData(); IQueryable checkMagRows = this.ultraGrid2.Rows.AsQueryable().Where(" CHK = 'True'"); if (checkMagRows.Count() == 0) { MessageUtil.ShowTips("请选择需要修改的化学成分数据!"); return; } UltraGridRow row = this.ultraGrid1.ActiveRow; MatGpMEntity matGp = (MatGpMEntity)row.ListObject; string matGpEntity = JSONFormat.Format(matGp); ArrayList parm = new ArrayList(); foreach (UltraGridRow uRow in checkMagRows) { SteerChemAuditResultEntity entity = (SteerChemAuditResultEntity)uRow.ListObject; string steelEntity = JSONFormat.Format(entity); parm.Add(steelEntity); } if (MessageUtil.ShowYesNoAndQuestion("是否删除该化成分数据?") == DialogResult.No) { return; } CoreClientParam ccp = new CoreClientParam(); ccp.ServerName = "com.steering.lims.data.steer.FrmSteelChemicalOutsourcing"; ccp.MethodName = "doDelete"; ccp.ServerParams = new object[] { parm, matGpEntity }; ccp = ob.ExecuteNonQuery(ccp, CoreInvokeType.Internal); if (ccp.ReturnCode != -1) { if (ccp.ReturnInfo.Equals("删除成功!")) { doQueryData(); MessageUtil.ShowTips(ccp.ReturnInfo); } else { MessageUtil.ShowTips(ccp.ReturnInfo); } } } private void GetElements() { //this.ultraGrid2.UpdateData(); this.ultraGrid1.UpdateData(); UltraGridRow row = this.ultraGrid1.ActiveRow; MatGpMEntity matGp = (MatGpMEntity)row.ListObject; //string matGpEntity = JSONFormat.Format(matGp); CoreClientParam ccp = new CoreClientParam(); ccp.ServerName = "com.steering.lims.data.steer.FrmSteelChemicalOutsourcing"; ccp.MethodName = "GetElements"; ccp.ServerParams = new object[] { matGp.StoveNo }; ccp = ob.ExecuteNonQuery(ccp, CoreInvokeType.Internal); if (ccp.ReturnCode != -1) { if (ccp.ReturnInfo.Equals("操作成功!")) { doQueryData(); MessageUtil.ShowTips(ccp.ReturnInfo); } else { MessageUtil.ShowTips(ccp.ReturnInfo); } } } /// /// 关闭 /// private void close() { this.Close(); } private void chkFurnace_CheckedChanged(object sender, EventArgs e) { this.txtFurnace.Enabled = this.chkFurnace.Checked; } private void ultraGrid1_AfterRowActivate(object sender, EventArgs e) { UltraGridRow uRow = this.ultraGrid1.ActiveRow; if(uRow == null) { return; } doQueryChemical(uRow.Cells["StoveNo"].Text); } private void ultraGrid2_KeyDown(object sender, KeyEventArgs e) { if (e.KeyData == Keys.Right) { if (ultraGrid2.ActiveCell.Column.Index + 1== ultraGrid2.DisplayLayout.Bands[0].Columns.Count) { return; } ultraGrid2.ActiveColScrollRegion.ScrollColIntoView(ultraGrid2.DisplayLayout.Bands[0].Columns[ultraGrid2.ActiveCell.Column.Index + 1], true); ultraGrid2.PerformAction(UltraGridAction.EnterEditMode); } if (e.KeyData == Keys.Left) { if (ultraGrid2.ActiveCell.Column.Index - 1 == 0) { return; } ultraGrid2.ActiveColScrollRegion.ScrollColIntoView(ultraGrid2.DisplayLayout.Bands[0].Columns[ultraGrid2.ActiveCell.Column.Index - 1], true); ultraGrid2.PerformAction(UltraGridAction.EnterEditMode); } } private void ultraGrid1_InitializeRow(object sender, InitializeRowEventArgs e) { MatGpMEntity matGpEntity = e.Row.ListObject as MatGpMEntity; if (!matGpEntity.ActCount.ToString().Equals("0")) { e.Row.RowSelectorAppearance.BackColor = Color.LightGreen; } } private void txtFurnace_KeyDown(object sender, KeyEventArgs e) { if (e.KeyValue == 13) { doQueryData(); } } /// /// 将excel文件内容读取到DataTable数据表中 /// /// 文件完整路径名 /// 第一行是否是DataTable的列名:true=是,false=否 /// DataTable数据表 public static DataSet ReadExcelToDataTable(string fileName, bool isFirstRowColumn = true, bool isFirstRowTitle = true) { //定义要返回的datatable对象 DataSet ds = new DataSet(); //excel工作表 //NPOI.SS.UserModel.ISheet sheet = null; //数据开始行(排除标题行) int startRow = 0; try { if (!File.Exists(fileName)) { return null; } //根据指定路径读取文件 FileStream fs = new FileStream(fileName, FileMode.Open, FileAccess.Read); //根据文件流创建excel数据结构 NPOI.SS.UserModel.IWorkbook workbook = NPOI.SS.UserModel.WorkbookFactory.Create(fs); //IWorkbook workbook = new HSSFWorkbook(fs); for (int k = 0; k < workbook.NumberOfSheets; k++) { NPOI.SS.UserModel.ISheet sheet = workbook.GetSheetAt(k); DataTable data = new DataTable(sheet.SheetName); if (sheet != null) { NPOI.SS.UserModel.IRow firstRow = sheet.GetRow(isFirstRowTitle ? 1 : 0); //一行最后一个cell的编号 即总的列数 int cellCount = firstRow.LastCellNum; //如果第一行是标题列名 if (isFirstRowColumn) { for (int i = firstRow.FirstCellNum; i < cellCount; ++i) { NPOI.SS.UserModel.ICell cell = firstRow.GetCell(i); if (cell != null) { string cellValue = cell.StringCellValue; if (cellValue==null) break; DataColumn column = new DataColumn(cellValue); data.Columns.Add(column); } } startRow = sheet.FirstRowNum + 1 + (isFirstRowTitle ? 1 : 0); } else { startRow = sheet.FirstRowNum; } //最后一列的标号 int rowCount = sheet.LastRowNum; for (int i = startRow; i <= rowCount; ++i) { NPOI.SS.UserModel.IRow row = sheet.GetRow(i); if (row == null) continue; //没有数据的行默认是null        DataRow dataRow = data.NewRow(); for (int j = row.FirstCellNum; j < data.Columns.Count; ++j) { if (row.GetCell(j) != null) //同理,没有数据的单元格都默认是null dataRow[j] = row.GetCell(j).ToString(); } data.Rows.Add(dataRow); } ds.Tables.Add(data); } } return ds; } catch (Exception ex) { throw ex; } } public static List ReadExcelToSteerChemAuditResultEntity(string fileName, bool isFirstRowColumn = true, bool isFirstRowTitle = true) { //定义要返回的datatable对象 List SteerChemAuditResultEntitys = new List(); Type type = typeof(SteerChemAuditResultEntity); //excel工作表 //NPOI.SS.UserModel.ISheet sheet = null; //数据开始行(排除标题行) int startRow = 0; try { if (!File.Exists(fileName)) { return null; } //根据指定路径读取文件 FileStream fs = new FileStream(fileName, FileMode.Open, FileAccess.Read); //根据文件流创建excel数据结构 NPOI.SS.UserModel.IWorkbook workbook = NPOI.SS.UserModel.WorkbookFactory.Create(fs); //IWorkbook workbook = new HSSFWorkbook(fs); for (int k = 0; k < workbook.NumberOfSheets; k++) { NPOI.SS.UserModel.ISheet sheet = workbook.GetSheetAt(k); Dictionary propertyInfoDic = new Dictionary(); if (sheet != null) { NPOI.SS.UserModel.IRow firstRow = sheet.GetRow(isFirstRowTitle ? 1 : 0); //一行最后一个cell的编号 即总的列数 int cellCount = firstRow.LastCellNum; //如果第一行是标题列名 if (isFirstRowColumn) { for (int i = firstRow.FirstCellNum; i < cellCount; ++i) { NPOI.SS.UserModel.ICell cell = firstRow.GetCell(i); if (cell != null) { string cellValue = cell.StringCellValue; if (cellValue == null) break; if (cellValue == "炉号") cellValue = "HeatNo"; else if (cellValue == "样号") cellValue = "SampleNo"; else cellValue = "Ana" + cellValue; PropertyInfo propertyInfo = type.GetProperty(cellValue); if (propertyInfo == null) continue; propertyInfoDic.Add(i,propertyInfo); } } startRow = sheet.FirstRowNum + 1 + (isFirstRowTitle ? 1 : 0); } else { startRow = sheet.FirstRowNum; } //最后一列的标号 int rowCount = sheet.LastRowNum; for (int i = startRow; i <= rowCount; ++i) { NPOI.SS.UserModel.IRow row = sheet.GetRow(i); if (row == null) continue; //没有数据的行默认是null        //DataRow dataRow = data.NewRow(); SteerChemAuditResultEntity SteerChemAuditResultEntity = new SteerChemAuditResultEntity(); foreach (KeyValuePair propertyInfoKV in propertyInfoDic) { if (row.GetCell(propertyInfoKV.Key) == null) continue; propertyInfoKV.Value.SetValue(SteerChemAuditResultEntity,row.GetCell(propertyInfoKV.Key).ToString() ,null); } if (!string.IsNullOrWhiteSpace(SteerChemAuditResultEntity.HeatNo)) { if (string.IsNullOrWhiteSpace(SteerChemAuditResultEntity.SampleNo)) { SteerChemAuditResultEntity.SampleNo = "601"; } SteerChemAuditResultEntitys.Add(SteerChemAuditResultEntity); } } } } return SteerChemAuditResultEntitys; } catch (Exception ex) { MessageBox.Show("EXCEL格式错误:" + ex.Message); return null; } } private void btnOpen_Click(object sender, EventArgs e) { var openFileDialog = new OpenFileDialog { Filter = "外购熔炼成分|*.xls;*.xlsx" }; if (openFileDialog.ShowDialog() == DialogResult.OK) { SteerChemAuditResultEntitybindingSourceExcel.DataSource = ReadExcelToSteerChemAuditResultEntity(openFileDialog.FileName, true, chcColumnRow.Checked).OrderBy(p=>p.HeatNo); foreach (UltraGridRow row in ultraGrid3.Rows) { row.Cells["CHK"].Value = true; } } } private void tabMain_SelectedTabChanged(object sender, Infragistics.Win.UltraWinTabControl.SelectedTabChangedEventArgs e) { if (toolMenu==null|| toolMenu.Toolbars==null ) return; if (toolMenu.Toolbars[0].Tools.Exists("Query")) { toolMenu.Toolbars[0].Tools["Query"].InstanceProps.Visible = tabMain.SelectedTab.Index == 0 ? DefaultableBoolean.True : DefaultableBoolean.False; } if (toolMenu.Toolbars[0].Tools.Exists("Update")) { toolMenu.Toolbars[0].Tools["Update"].InstanceProps.Visible = tabMain.SelectedTab.Index == 0 ? DefaultableBoolean.True : DefaultableBoolean.False; } if (toolMenu.Toolbars[0].Tools.Exists("Delete")) { toolMenu.Toolbars[0].Tools["Delete"].InstanceProps.Visible = tabMain.SelectedTab.Index == 0 ? DefaultableBoolean.True : DefaultableBoolean.False; } if (toolMenu.Toolbars[0].Tools.Exists("Export")) { toolMenu.Toolbars[0].Tools["Export"].InstanceProps.Visible = tabMain.SelectedTab.Index == 0 ? DefaultableBoolean.True : DefaultableBoolean.False; } } private void chkRegTime_CheckedChanged(object sender, EventArgs e) { this.RegStartTime.Enabled = this.RegEndTime.Enabled = this.chkRegTime.Checked; } private void chkJudge_CheckedChanged(object sender, EventArgs e) { this.txtJudgeNo.Enabled = this.chkJudge.Checked; } private void ultraGrid2_KeyPress(object sender, KeyPressEventArgs e) { string strKey = this.ultraGrid2.ActiveCell.Column.ToString(); if (strKey != "CHK" && strKey != "SampleNo") { //string vaa = ultraGrid2.ActiveRow.Cells[strKey].Value.ToString(); //bool str = IsValidNumber(ultraGrid2.ActiveRow.Cells[strKey].Value.ToString()); //e.Handled = !IsValidNumber(ultraGrid2.ActiveRow.Cells[strKey].Value.ToString()); if (!(e.KeyChar == '\b') && ultraGrid2.ActiveRow.Cells[strKey].Value.ToString().Length >= 6) { e.Handled = true; } if (!(Char.IsNumber(e.KeyChar) || e.KeyChar == '\b' || e.KeyChar == '.')) { e.Handled = true; } if (e.KeyChar == '.') { char[] arrary = ultraGrid2.ActiveRow.Cells[strKey].Text.ToCharArray(); if (arrary.Length == 0) { ultraGrid2.ActiveRow.Cells[strKey].Value = "0."; ultraGrid2.ActiveRow.Cells[strKey].SelStart = ultraGrid2.ActiveRow.Cells[strKey].Value.ToString().Length; e.Handled = true; } else { for (int i = 0; i < arrary.Length; i++) { if (arrary[i] == '.') e.Handled = true; } } } if (e.KeyChar == '+') { char[] arrary = ultraGrid2.ActiveRow.Cells[strKey].Text.ToCharArray(); if (arrary.Length == 0) { ultraGrid2.ActiveRow.Cells[strKey].Value = "+"; ultraGrid2.ActiveRow.Cells[strKey].SelStart = ultraGrid2.ActiveRow.Cells[strKey].Value.ToString().Length; e.Handled = true; } else { for (int i = 0; i < arrary.Length; i++) { if (arrary[i] == '+') e.Handled = true; } } } if (e.KeyChar == '-') { char[] arrary = ultraGrid2.ActiveRow.Cells[strKey].Text.ToCharArray(); if (arrary.Length == 0) { ultraGrid2.ActiveRow.Cells[strKey].Value = "-"; ultraGrid2.ActiveRow.Cells[strKey].SelStart = ultraGrid2.ActiveRow.Cells[strKey].Value.ToString().Length; e.Handled = true; } else { for (int i = 0; i < arrary.Length; i++) { if (arrary[i] == '-') e.Handled = true; } } } } } } }