using System; using System.Collections.Generic; using System.ComponentModel; using System.Collections; using System.Data; using System.Drawing; using System.IO; using System.Linq; using System.Reflection; using System.Text; using System.Windows.Forms; using Core.Mes.Client.Comm.Control; using Core.Mes.Client.Comm.Format; using Core.Mes.Client.Comm.Tool; using CoreFS.CA06; using Infragistics.Win; using Infragistics.Win.UltraWinGrid; using NPOI.SS.UserModel; using Core.StlMes.Client.PnCost.Models; namespace Core.StlMes.Client.PnCost { public partial class Modecoefficient : FrmBase { #region 定义变量 public Modecoefficient() { InitializeComponent(); EntityHelper.ShowGridCaption(ultraGrid1.DisplayLayout.Bands[0]); } private DataTable dtgang; #endregion #region 方法 public override void ToolBar_Click(object sender, string ToolbarKey) { base.ToolBar_Click(sender, ToolbarKey); switch (ToolbarKey) { case "Query": Query(); break; case "doSave": doSave(); break; case "doAdd": doAdd(); break; case "doDelete": this.doDelete(); break; } } private void doDelete() { this.ultraGrid1.UpdateData(); IQueryable checkMagRows = this.ultraGrid1.Rows.AsQueryable().Where(" XUANZE = 'True'"); if (checkMagRows.Count() == 0) { MessageUtil.ShowTips("请选择需要删除的数据!"); return; } ArrayList parm = new ArrayList(); foreach (UltraGridRow uRow in checkMagRows) { ModecoefficientEntity modecoefficientEntity = (ModecoefficientEntity)uRow.ListObject; string ModecoefficientEntity = JSONFormat.Format(modecoefficientEntity); parm.Add(ModecoefficientEntity); } CoreClientParam ccp = new CoreClientParam(); ccp.ServerName = "com.steering.mes.cost.FrmModecoefficient"; ccp.MethodName = "doDelete"; ccp.ServerParams = new object[] { parm }; ccp = ob.ExecuteNonQuery(ccp, CoreInvokeType.Internal); if (ccp.ReturnCode != -1) { if (ccp.ReturnInfo.Equals("操作成功")) { // DoQueryInList(); Query(); MessageUtil.ShowTips(ccp.ReturnInfo); } else { MessageUtil.ShowTips(ccp.ReturnInfo); } } } /// private void doAdd() { List list = null; var openFileDialog = new OpenFileDialog { Filter = "工艺路径|*.xls;*.xlsx" }; if (openFileDialog.ShowDialog() == DialogResult.OK) { list = ReadExcelToSingnhjxhEntity(openFileDialog.FileName); } if (list == null) return; if (list.Count <= 0) { MessageBox.Show("Excel无数据"); return; } string userName = UserInfo.GetUserName(); var ccp = new CoreClientParam(); ccp.ServerName = "com.steering.mes.cost.FrmModecoefficient"; ccp.MethodName = "DoAddModecoefficient"; ccp.ServerParams = new object[] { list.Select(JSONFormat.Format).ToList() }; ccp = ExecuteNonQuery(ccp, CoreInvokeType.Internal); if (ccp.ReturnCode != -1) { MessageUtil.ShowTips(ccp.ReturnInfo); if (ccp.ReturnInfo.Equals("导入成功")) { Query(); } } } public List ReadExcelToSingnhjxhEntity(string fileName) { //定义要返回的datatable对象 List modecoefficientEntitys = new List(); try { if (!File.Exists(fileName)) { return null; } //根据指定路径读取文件 using (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); if (sheet != null) { //最后一列的标号 int rowCount = sheet.LastRowNum; for (int i = 1; i <= rowCount; ++i) { ModecoefficientEntity entity = new ModecoefficientEntity(); NPOI.SS.UserModel.IRow row = sheet.GetRow(i); if (row == null) continue; //没有数据的行默认是null        if (row.Cells.Count < 3) { throw new Exception("无足够列"); } //WHOLE_BACKLOG_NAME string ModelDesc = GetCellValue(row.Cells[0]); string Upsetting = GetCellValue(row.Cells[1]); string Testing = GetCellValue(row.Cells[2]); string Threading = GetCellValue(row.Cells[3]); string Phosphating = GetCellValue(row.Cells[5]); string Screwing = GetCellValue(row.Cells[6]); string Hydraulic = GetCellValue(row.Cells[7]); string Pathway = GetCellValue(row.Cells[8]); string Lengthweighing = GetCellValue(row.Cells[9]); string Logopainting = GetCellValue(row.Cells[10]); string Wrap = GetCellValue(row.Cells[11]); string Baling = GetCellValue(row.Cells[12]); decimal Couplings; decimal Threadcoefficient; decimal.TryParse(GetCellValue(row.Cells[13]), out Couplings); decimal.TryParse(GetCellValue(row.Cells[4]), out Threadcoefficient); if (Couplings == null) { throw new Exception("接箍金额为空"); } if(Threadcoefficient == null){ throw new Exception("系数为空"); } entity.ModelDesc = ModelDesc; entity.Upsetting = Upsetting; entity.Testing = Testing; entity.Threading = Threading; entity.Phosphating = Phosphating; entity.Screwing = Screwing; entity.Hydraulic = Hydraulic; entity.Pathway = Pathway; entity.Lengthweighing = Lengthweighing; entity.Logopainting = Logopainting; entity.Wrap = Wrap; entity.Baling = Baling; entity.Couplings = Couplings; entity.Threadcoefficient = Threadcoefficient; modecoefficientEntitys.Add(entity); } } } } return modecoefficientEntitys; } catch (Exception ex) { MessageBox.Show("EXCEL格式错误:" + ex.Message); return null; } } private string GetCellValue(ICell cell) { string value = ""; switch (cell.CellType) { case CellType.Blank: value = ""; break; case CellType.Numeric: short format = cell.CellStyle.DataFormat; value = cell.NumericCellValue.ToString2(); break; case CellType.String: value = cell.StringCellValue; break; } return value; } /// /// 保存 /// private void doSave() { try { this.ultraGrid1.UpdateData(); IQueryable checkMagRows = this.ultraGrid1.Rows.AsQueryable().Where(" XUANZE = 'True'"); if (checkMagRows.Count() == 0) { MessageUtil.ShowTips("请选择需要修改的数据!"); return; } ArrayList parm = new ArrayList(); foreach (UltraGridRow uRow in checkMagRows) { ModecoefficientEntity entity = (ModecoefficientEntity)uRow.ListObject; // entity.RecRevisor = UserInfo.GetUserName(); string JgxpriceEntity = JSONFormat.Format(entity); parm.Add(JgxpriceEntity); } CoreClientParam ccp = new CoreClientParam(); ccp.ServerName = "com.steering.mes.cost.FrmModecoefficient"; ccp.MethodName = "doSave"; ccp.ServerParams = new object[] { parm }; ccp = ob.ExecuteNonQuery(ccp, CoreInvokeType.Internal); if (ccp.ReturnCode != -1) { if (ccp.ReturnInfo.Equals("操作成功")) { // DoQueryInList(); Query(); MessageUtil.ShowTips(ccp.ReturnInfo); } else { MessageUtil.ShowTips(ccp.ReturnInfo); } } } catch { return; } } /// 查询方法 /// /// 查询方法 /// private void Query() { List listSource = EntityHelper.GetData( "com.steering.mes.cost.FrmModecoefficient.Query", new object[] { cicText.Text }, this.ob); modecoefficientEntityBindingSource.DataSource = listSource; GridHelper.RefreshAndAutoSize(ultraGrid1); } /// 保存方法 /// /// 保存方法 /// private void Save() { } #endregion #region #endregion } }