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 com.steering.mes.mcp.entity; 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.ZGMil.Entity; namespace Core.StlMes.Client.ZGMil.Cost { public partial class FrmTqnqc14 : FrmBase { public FrmTqnqc14() { InitializeComponent(); } private void FrmTqnqc14_Load(object sender, EventArgs e) { EntityHelper.ShowGridCaption(ultraGrid1.DisplayLayout.Bands[0]); Query(); } public override void ToolBar_Click(object sender, string ToolbarKey) { switch (ToolbarKey) { case "DoQuery": Query(); break; case "doImport": Import(); break; case "doSave": break; case "doUpdate": Update(); break; case "doDelete": delete(); break; case "Close": Close(); break; } } //导入 private void Import() { List list = null; var openFileDialog = new OpenFileDialog { Filter = "精整工序基础信息|*.xls;*.xlsx" }; if (openFileDialog.ShowDialog() == DialogResult.OK) { list = ReadExcelToRtdTqnqc14Entity(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.FrmTqnqc14"; ccp.MethodName = "DoAddTqnqc14"; ccp.ServerParams = new object[] { list.Select(JSONFormat.Format).ToList(), userName }; ccp = ExecuteNonQuery(ccp, CoreInvokeType.Internal); if (ccp.ReturnCode != -1) { MessageUtil.ShowTips(ccp.ReturnInfo); if (ccp.ReturnInfo.Equals("导入成功")) { // Query(); } } } private void delete() { this.ultraGrid1.UpdateData(); IQueryable checkMagRows = this.ultraGrid1.Rows.AsQueryable().Where(" CHK = 'True'"); if (checkMagRows.Count() == 0) { MessageUtil.ShowTips("请选择需要删除的数据!"); return; } ArrayList parm = new ArrayList(); foreach (UltraGridRow uRow in checkMagRows) { Tqnqc14Entity OfflineResult = (Tqnqc14Entity)uRow.ListObject; string OfflineResultTity = JSONFormat.Format(OfflineResult); parm.Add(OfflineResultTity); } CoreClientParam ccp = new CoreClientParam(); ccp.ServerName = "com.steering.mes.cost.FrmTqnqc14"; 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 Update() { this.ultraGrid1.UpdateData(); IQueryable checkMagRows = this.ultraGrid1.Rows.AsQueryable().Where(" CHK = 'True'"); if (checkMagRows.Count() == 0) { MessageUtil.ShowTips("请选择需要修改的数据!"); return; } ArrayList parm = new ArrayList(); foreach (UltraGridRow uRow in checkMagRows) { Tqnqc14Entity entity = (Tqnqc14Entity)uRow.ListObject; entity.DuMaker = UserInfo.GetUserName(); string OfflineResultTity = JSONFormat.Format(entity); parm.Add(OfflineResultTity); } CoreClientParam ccp = new CoreClientParam(); ccp.ServerName = "com.steering.mes.cost.FrmTqnqc14"; 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); } } } private void Query() { string backlog = ""; if (chkJugeHeatNo.Checked == true) { backlog = txtJugeNo.Text; } List listSource = EntityHelper.GetData( "com.steering.mes.cost.FrmTqnqc14.doQuery", new object[] { backlog }, this.ob); tqnqc14EntityBindingSource.DataSource = listSource; //GridHelper.RefreshAndAutoSize(ultraGrid1); } public List ReadExcelToRtdTqnqc14Entity(string fileName) { //定义要返回的datatable对象 List Tqnqc14Entitys = 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) { Tqnqc14Entity entity = new Tqnqc14Entity(); NPOI.SS.UserModel.IRow row = sheet.GetRow(i); if (row == null) continue; //没有数据的行默认是null        if (row.Cells.Count < 4) { throw new Exception("无足够列"); } string jzCode = GetCellValue(row.Cells[0]); string subcod = GetCellValue(row.Cells[1]); string costCenterAc = GetCellValue(row.Cells[2]); string wholeBacklog = GetCellValue(row.Cells[3]); string zjWholeBacklog = "精整"+ GetCellValue(row.Cells[3]); string subBacklog = GetCellValue(row.Cells[4]); //string jzName = //string backlog = GetCellValue(row.Cells[2]); //string subbacklog = GetCellValue(row.Cells[3]); if (costCenterAc == "" || costCenterAc == null) { throw new Exception("成本中心为空"); } if (jzCode == "" || jzCode == null) { throw new Exception("工序代码为空"); } if (wholeBacklog == "" || wholeBacklog == null) { throw new Exception("工序名称为空"); } if (zjWholeBacklog == "" || zjWholeBacklog == null) { throw new Exception("机组为空"); } if (subBacklog == "" || subBacklog == null) { throw new Exception("标准成本工序"); } decimal unitPrice; decimal timeNum; decimal gdPrice; decimal.TryParse(GetCellValue(row.Cells[5]), out gdPrice); decimal.TryParse(GetCellValue(row .Cells[6]), out unitPrice); decimal.TryParse(GetCellValue(row.Cells[7]), out timeNum); if (gdPrice == null || gdPrice < 0) { throw new Exception("gd价格异常"); } if (unitPrice == null || unitPrice < 0) { throw new Exception("价格异常"); } if (timeNum == null || timeNum < 0) { throw new Exception("次数异常"); } entity.RpCode = jzCode; entity.CostCenterAc = costCenterAc; entity.WholeBacklog = wholeBacklog; entity.SubBacklog = subBacklog; entity.TimeNum = timeNum; entity.UnitPrice = unitPrice; entity.JzWholeBacklog = zjWholeBacklog; entity.SubCode = subcod; entity.GdPrice = gdPrice; Tqnqc14Entitys.Add(entity); } } } } return Tqnqc14Entitys; } 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; } } }