using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; using System.Collections; using CoreFS.CA06; using Core.Mes.Client.Comm.Tool; using Core.Mes.Client.Comm.Server; using com.steering.pss.plnsaleord.order.model; using System.Globalization; using Core.Mes.Client.Comm; namespace Core.StlMes.Client.PlnSaleOrd.PopupWindow { public partial class FrmImportSetOrder : FrmBase { /// /// 模板路径 /// string modelPatch = ""; /// /// 模板路径 /// public string ModelPatch { get { return modelPatch; } set { modelPatch = value; } } public FrmImportSetOrder() { InitializeComponent(); } public FrmImportSetOrder(OpeBase openbase) { this.ob = openbase; InitializeComponent(); } private void FrmImportSetOrder_Load(object sender, EventArgs e) { Init(); } /// /// 初始化 /// private void Init() { ultraNumRowStart.Value = 2; ultraNumRowEnd.Value = 2; if (!System.IO.File.Exists(ModelPatch))//检察文件是否存在 { return; } try { System.IO.File.Move(ModelPatch, ModelPatch); } catch { MessageUtil.ShowTips("文件已经被打开,请先关闭!"); return; } uC_EmbedExcelIntoWinFormNoDLL1.AppExcel_Open(ModelPatch, true); this.ultraNumRowEnd.Value = getExcelRowsCount(); } /// /// 获取excl的行数 /// private int getExcelRowsCount() { //获取行数(上限5000) int colOrderNo = 0; int j = 4999; string[,] arr = null; string[,] head = ObjectToString(uC_EmbedExcelIntoWinFormNoDLL1.GetRangeArray_Value("A" + 1, "BZ" + 1)); if (head == null) { return 1; } for (int i = 0; i < head.GetLength(1); i++) { if (head[0, i].Trim().Equals("合同号")) { colOrderNo = i; break; } } arr = ObjectToString(uC_EmbedExcelIntoWinFormNoDLL1.GetRangeArray_Value( ExcelHelper.SeqToXlsCol[colOrderNo + 1] + 2, ExcelHelper.SeqToXlsCol[colOrderNo + 1] + 5001)); if (arr == null) { return 1; } for (j = 4999; j >= 0; j--) { if (!arr[j, 0].Equals("")) { break; } } return j + 2; } private void ultraToolbarsManager1_ToolClick(object sender, Infragistics.Win.UltraWinToolbars.ToolClickEventArgs e) { switch (e.Tool.Key) { case "Save": doSave(); break; case "Open": doOpen(); break; case "Close": doClose(); break; } } /// /// 保存 /// private void doSave() { try { ArrayList messageErr = new ArrayList(); int startRow = (int)ultraNumRowStart.Value; int endRow = (int)ultraNumRowEnd.Value; if (endRow - startRow < 0) { MessageUtil.ShowTips("没有任何行数据!"); return; } string[,] head = ObjectToString(uC_EmbedExcelIntoWinFormNoDLL1.GetRangeArray_Value("A" + 1, "BZ" + 1)); if (head == null) { return; } string[] colhead = new string[head.GetLength(1)]; for (int i = 0; i < colhead.Length; i++) { colhead[i] = head[0, i]; } #region 获取合同号等所在列 int colNum = 0; int colRollMonth = -1; int colOrderPk = -1; int colCheckMoth = -1; int colPriority = -1; int colPriorityMemo = -1; for (int i = head.GetLength(1) - 1; i >= 0; i--) { if (!head[0, i].Equals("") && colNum == 0) { colNum = i; } if (head[0, i].Trim().Equals("轧制月份")) { colRollMonth = i; } if (head[0, i].Trim().Equals("交货编码PK")) { colOrderPk = i; } if (head[0, i].Trim().Equals("考核年月")) { colCheckMoth = i; } if (head[0, i].Trim().Equals("合同紧急度")) { colPriority = i; } if (head[0, i].Trim().Equals("紧急度备注")) { colPriorityMemo = i; } } if (colOrderPk == -1) { MessageUtil.ShowError("未找到交货编码PK的列"); return; } #endregion ArrayList list = new ArrayList(); string[,] arr = ObjectToString(uC_EmbedExcelIntoWinFormNoDLL1.GetRangeArray_Value( "A" + startRow, ExcelHelper.SeqToXlsCol[colNum + 1] + endRow)); if (arr == null) { return; } WaitFromOpen(); DataTable tableBaseInfo = ServerHelper.GetData("com.steering.pss.plnsaleord.order.CorePlnSaleOrder.queryBaseinfoAll",new object[] { }, this.ob); for (int i = 0; i < arr.GetLength(0); i++) { ArrayList param = new ArrayList(); PlnSaleordSetEntity entity = new PlnSaleordSetEntity(); string orderPk = arr[i, colOrderPk].Trim(); string ordLnDlyPk = ""; string plnDivideId = ""; if (orderPk.Equals("")) { messageErr.Add("第" + (i + startRow).ToString() + "条数据PK码为空"); continue; } if (orderPk.Contains('-')) { int lenPk = orderPk.LastIndexOf('-'); ordLnDlyPk = orderPk.Substring(0, lenPk); plnDivideId = orderPk.Substring(lenPk + 1, orderPk.Length - lenPk - 1); } else { ordLnDlyPk = orderPk; } if (ordLnDlyPk.Equals("")) { messageErr.Add("第" + (i + startRow).ToString() + "条数据PK码有误。"); continue; } if (colRollMonth != -1) { DateTime date = new DateTime(); string rollMonth = arr[i, colRollMonth].ToString(); if (DateTime.TryParseExact(rollMonth, "yyyyMM", null, DateTimeStyles.None, out date)) { entity.RollMonth = date.ToString("yyyy-MM"); } } if(colCheckMoth != -1) { entity.CheckMoth = arr[i, colCheckMoth].Trim(); } if (colPriority != -1) { tableBaseInfo.DefaultView.RowFilter = "SORTCODE='2006' and BASENAME='" + arr[i, colPriority].Trim() + "'"; DataTable dtPrior = tableBaseInfo.DefaultView.ToTable(); if (dtPrior != null) { entity.Priority = dtPrior.Rows[0]["BASECODE"].ToString2(); } } if (colPriorityMemo != -1) { entity.PriorityMemo = arr[i, colPriorityMemo].Trim(); } entity.OrdLnDlyPk = ordLnDlyPk; entity.PlnDivideId = plnDivideId; list.Add(entity); } int succed = ServerHelper.SetData("com.steering.pss.plnsaleord.order.CoreOrderStatusSet.saveOrderSetlog", new object[] { list }, this.ob); WaitFromColse(); if (succed > 0) { if (messageErr.Count == 0) { MessageUtil.ShowTips("导入成功,共" + list.Count + "条。"); } else { MessageUtil.ShowTips(list.Count + "条导入成功," + messageErr.Count + "条导入失败。\n" + messageErr[0]); } } else { MessageUtil.ShowTips("导入失败,共" + list.Count + "条。"); } } catch (Exception ex) { WaitFromColse(); if (!(ex is MESException)) { MessageUtil.ShowError(ex.Message); } } } /// /// 打开excel文件 /// private void doOpen() { try { OpenFileDialog dialog = new OpenFileDialog(); uC_EmbedExcelIntoWinFormNoDLL1.AppExcel_Exit(); if (dialog.ShowDialog() == DialogResult.OK) { ModelPatch = dialog.FileName; if (!System.IO.File.Exists(ModelPatch))//检察文件是否存在 { return; } try { System.IO.File.Move(ModelPatch, ModelPatch); } catch { MessageUtil.ShowTips("文件已经被打开,请先关闭!"); return; } uC_EmbedExcelIntoWinFormNoDLL1.AppExcel_Open(ModelPatch, true); this.ultraNumRowEnd.Value = getExcelRowsCount(); } } catch (Exception ex) { MessageBox.Show(ex.Message); } } /// /// 关闭 /// private void doClose() { uC_EmbedExcelIntoWinFormNoDLL1.AppExcel_Exit(); } /// /// object二维数组转string二维数组 /// /// /// private string[,] ObjectToString(object[,] obj) { int rows = obj.GetLength(0); //获取m_objRangeArrayValue的行数 int columns = obj.GetLength(1); //获取m_objRangeArrayValue的列数 object[,] param = new object[rows, columns]; //转化为起始为0的数组 Array.Copy(obj, param, rows * columns); //临时结果,学习用 string[,] str = new string[param.GetLength(0), param.GetLength(1)]; for (int i = 0; i < str.GetLength(0); i++) { for (int j = 0; j < str.GetLength(1); j++) { if (param[i, j] == null) { str[i, j] = ""; } else { str[i, j] = param[i, j].ToString(); } } } return str; } /// /// 等待窗口 /// private void WaitFromOpen() { this.Cursor = Cursors.WaitCursor; //控制鼠标的样式为等待 if (Constant.WaitingForm == null) { Constant.WaitingForm = new WaitingForm(); } Constant.WaitingForm.ShowToUser = true; Constant.WaitingForm.Show(); Constant.WaitingForm.Update(); } /// /// 关闭等待 /// private void WaitFromColse() { this.Cursor = Cursors.Default; Constant.WaitingForm.ShowToUser = false; Constant.WaitingForm.Close(); Constant.WaitingForm = null; } private void FrmImportSetOrder_FormClosed(object sender, FormClosedEventArgs e) { uC_EmbedExcelIntoWinFormNoDLL1.AppExcel_Exit(); } } }