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; using Core.Mes.Client.Comm.Server; using Infragistics.Win.UltraWinEditors; namespace Core.StlMes.Client.ZGMil.Cost { public partial class FrmTqnqc21 : FrmBase { public FrmTqnqc21() { 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 = ReadExcelToRtdTqnqc21Entity(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.FrmTqnqc21"; ccp.MethodName = "DoAddTqnqc21"; 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) { Tqnqc21Entity Tqnqc21Result = (Tqnqc21Entity)uRow.ListObject; if (Tqnqc21Result.WholeBacklogCode.Equals("") || Tqnqc21Result.WholeBacklogCode == null) { MessageUtil.ShowTips("编号为空!"); } string Tqnqc17Results = JSONFormat.Format(Tqnqc21Result); parm.Add(Tqnqc17Results); } CoreClientParam ccp = new CoreClientParam(); ccp.ServerName = "com.steering.mes.cost.FrmTqnqc21"; 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) { Tqnqc21Entity entity = (Tqnqc21Entity)uRow.ListObject; entity.RecRevisor = UserInfo.GetUserName(); string Tqnqc21ResultTity = JSONFormat.Format(entity); parm.Add(Tqnqc21ResultTity); } CoreClientParam ccp = new CoreClientParam(); ccp.ServerName = "com.steering.mes.cost.FrmTqnqc21"; 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 cb = ""; string jz = ""; if (chk_cb.Checked == true) { cb = text_name.Text; } List listSource = EntityHelper.GetData( "com.steering.mes.cost.FrmTqnqc21.doQuery", new object[] { cb }, this.ob); tqnqc21EntityBindingSource.DataSource = listSource; // GridHelper.RefreshAndAutoSize(ultraGrid1); } public List ReadExcelToRtdTqnqc21Entity(string fileName) { //定义要返回的datatable对象 List Tqnqc21Entitys = 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) { Tqnqc21Entity entity = new Tqnqc21Entity(); NPOI.SS.UserModel.IRow row = sheet.GetRow(i); if (row == null) continue; //没有数据的行默认是null        if (row.Cells.Count < 4) { throw new Exception("无足够列"); } string wholeBacklog = GetCellValue(row.Cells[0]); string fcbxMc = GetCellValue(row.Cells[1]); string wholeBacklogCode = GetCellValue(row.Cells[3]); decimal unitPrice; decimal.TryParse(GetCellValue(row.Cells[2]), out unitPrice); entity.WholeBacklog = wholeBacklog; entity.FcbxMc = fcbxMc; entity.WholeBacklogCode = wholeBacklogCode; entity.UnitPrice = unitPrice; entity.RecCreator = UserInfo.GetUserName(); Tqnqc21Entitys.Add(entity); } } } } return Tqnqc21Entitys; } 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; } public static DataTable GetComBaseInfo(string sortCode, OpeBase ob) { return ServerHelper.GetData("com.steering.mes.signature.FrmMilControlLog.getComBaseInfo", new object[] { sortCode }, ob); } public static void SetComboItemHeight(UltraComboEditor cmb) { foreach (ValueListItem item in cmb.Items) { if (System.Text.RegularExpressions.Regex.IsMatch(item.DisplayText, @"[\u4e00-\u9fa5]+")) { item.Appearance.FontData.SizeInPoints = 9.0F; } else { item.Appearance.FontData.SizeInPoints = 10.5F; } } } } }