using Core.Mes.Client.Comm.Control; using Core.Mes.Client.Comm.Format; using Core.Mes.Client.Comm.Tool; 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 Core.StlMes.Client.Lims.Port.数据管理.实体类; namespace Core.StlMes.Client.Lims.Port { public partial class FrmChemicalOutsourcing : FrmBase { public FrmChemicalOutsourcing() { InitializeComponent(); } private void FrmChemicalOutsourcing_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]); } /// /// 重写基类方法 /// /// /// 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"Export": exportData(); break; case "Close": close(); break; } } /// /// 导出 /// private void exportData() { GridHelper.ulGridToExcel(ultraGrid2, "外购管成品成分导出"); } /// /// 查询 /// private void doQueryData() { string stoveNo = ""; string judgeNo = ""; string strRegStart = ""; string strRegEnd = ""; if (this.chkFurnace.Checked && this.txtFurnace.Text.Trim() != "") { stoveNo = this.txtFurnace.Text.Trim(); } if(this.chkJudgeNo.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(judgeNo); parm.Add(strRegStart); parm.Add(strRegEnd); List listSource = EntityHelper.GetData( "com.steering.mes.port.vrp.FrmChemicalOutsourcing.doQueryData", new object[] { parm }, this.ob); ydmBcInlistEntityBindingSource.DataSource = listSource; } /// /// 查询成分数据 /// private void doQueryChemical(string judgeNo) { List listSource = EntityHelper.GetData( "com.steering.mes.port.vrp.FrmChemicalOutsourcing.doQueryChemical", new object[] { judgeNo }, this.ob); bool isnull = false; if (listSource.Count() == 0) { isnull = true; listSource = new List() { new QcmJhyElementsEntity() }; } qcmJhyElementsEntityBindingSource.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; } YdmBcInlistEntity ydmTity = (YdmBcInlistEntity)uRow.ListObject; string ydmBcEntity = JSONFormat.Format(ydmTity); ArrayList parm = new ArrayList(); foreach (UltraGridRow row in checkMagRows) { if (row.Cells["SampleNo"].Text.Trim().Equals("")) { MessageUtil.ShowTips("试样号不能为空!"); return; } if (!doNumber(row, ultraGrid2)) { m = m + 1; } QcmJhyElementsEntity entity = (QcmJhyElementsEntity)row.ListObject; string qcmEntity = JSONFormat.Format(entity); parm.Add(qcmEntity); } if(m > 0) { return; } if (MessageUtil.ShowYesNoAndQuestion("是否新增该化学成分数据?") == DialogResult.No) { return; } CoreClientParam ccp = new CoreClientParam(); ccp.ServerName = "com.steering.mes.port.vrp.FrmChemicalOutsourcing"; ccp.MethodName = "doAdd"; ccp.ServerParams = new object[] { parm, ydmBcEntity,UserInfo.GetUserName() }; 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("")) { MessageUtil.ShowTips("试样号不能为空!"); return; } if (!doNumber(row, ultraGrid3)) { n = n + 1; } QcmJhyElementsEntity entity = (QcmJhyElementsEntity)row.ListObject; string qcmEntity = JSONFormat.Format(entity); parm.Add(qcmEntity); } if(n > 0) { return; } if (MessageUtil.ShowYesNoAndQuestion("是否新增该化学成分数据?") == DialogResult.No) { return; } CoreClientParam ccp = new CoreClientParam(); ccp.ServerName = "com.steering.mes.port.vrp.FrmChemicalOutsourcing"; ccp.MethodName = "doAdd"; ccp.ServerParams = new object[] { parm }; 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 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 != "CHK") { if (!StringUtil.IsDouble(row.Cells[i].Text.ToString3())) { MessageUtil.ShowTips("元素值应该为小数!"); 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; YdmBcInlistEntity ydmBcEntity = (YdmBcInlistEntity)row.ListObject; string ydmEntity = JSONFormat.Format(ydmBcEntity); ArrayList parm = new ArrayList(); foreach(UltraGridRow uRow in checkMagRows) { QcmJhyElementsEntity entity = (QcmJhyElementsEntity)uRow.ListObject; string qcmEntity = JSONFormat.Format(entity); parm.Add(qcmEntity); } if (MessageUtil.ShowYesNoAndQuestion("是否修改该化成分数据?") == DialogResult.No) { return; } CoreClientParam ccp = new CoreClientParam(); ccp.ServerName = "com.steering.mes.port.vrp.FrmChemicalOutsourcing"; ccp.MethodName = "doUpdate"; ccp.ServerParams = new object[] { parm, ydmEntity }; 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; YdmBcInlistEntity ydmBcEntity = (YdmBcInlistEntity)row.ListObject; string ydmEntity = JSONFormat.Format(ydmBcEntity); ArrayList parm = new ArrayList(); foreach (UltraGridRow uRow in checkMagRows) { QcmJhyElementsEntity entity = (QcmJhyElementsEntity)uRow.ListObject; string qcmEntity = JSONFormat.Format(entity); parm.Add(qcmEntity); } if (MessageUtil.ShowYesNoAndQuestion("是否删除该化学成分数据?") == DialogResult.No) { return; } CoreClientParam ccp = new CoreClientParam(); ccp.ServerName = "com.steering.mes.port.vrp.FrmChemicalOutsourcing"; ccp.MethodName = "doDelete"; ccp.ServerParams = new object[] { parm, ydmEntity }; 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["JudgeStoveNo"].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) { YdmBcInlistEntity matGpEntity = e.Row.ListObject as YdmBcInlistEntity; 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 qcmChemAuditResultEntitys = new List(); Type type = typeof(QcmJhyElementsEntity); //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 = "JudgeStoveNo"; else if (cellValue == "样号") cellValue = "SampleNo"; else cellValue = 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(); QcmJhyElementsEntity SteerChemAuditResultEntity = new QcmJhyElementsEntity(); 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.JudgeStoveNo)) { //if (string.IsNullOrWhiteSpace(SteerChemAuditResultEntity.SampleNo)) //{ // SteerChemAuditResultEntity.SampleNo = "601"; //} qcmChemAuditResultEntitys.Add(SteerChemAuditResultEntity); } } } } return qcmChemAuditResultEntitys; } 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) { qcmJhyElementsEntityBindingSource1.DataSource = ReadExcelToSteerChemAuditResultEntity(openFileDialog.FileName, true, chcColumnRow.Checked).OrderBy(p => p.JudgeStoveNo); 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 chkJudgeNo_CheckedChanged(object sender, EventArgs e) { this.txtJudgeNo.Enabled = this.chkJudgeNo.Checked; } } }