private void CheckProductID() { if (string.IsNullOrWhiteSpace(this.productID) || string.IsNullOrWhiteSpace(this.ProductID)) { return; } ProductEntity lProductEntity; if (ProductEntityDictionary.TryGetValue(this.ProductID, out lProductEntity)) { this.AddError("ProductID", "序列号不能重复"); } else { this.RemoveError("ProductID"); } }
private void OnImportCommand() { OpenFileDialog lOpenFile = new OpenFileDialog(); lOpenFile.Filter = "Excel (*.xls)|*.xls"; Dictionary <String, int> lHeaderDictionary = new Dictionary <String, int>(); if (lOpenFile.ShowDialog() == true) { ProductEntityList.Clear(); try { FileStream fs = lOpenFile.File.OpenRead(); Workbook book = Workbook.Open(fs); foreach (KeyValuePair <int, Row> rowPair in book.Worksheets[0].Cells.Rows) { if (rowPair.Key == 0) { try { foreach (KeyValuePair <int, Cell> cellPair in rowPair.Value) { lHeaderDictionary.Add(cellPair.Value.StringValue, cellPair.Key); } } catch (System.Exception ex) { Message.ErrorMessage("表头重复或超出范围!"); break; } continue; } ProductEntity lProductEntity = new ProductEntity(); lProductEntity.Product = new ProductManager.Web.Model.product(); lProductEntity.ProductEntityDictionary = ProductEntityDictionary; lProductEntity.ProjectEntityDictionary = ProjectEntityDictionary; //lProductEntity.ProductPartTypeEntityDictionary = ProductTypeEntityDictionary; int lManufactureNumberColumn = -1; { Cell cell = Cell.EmptyCell; if (lHeaderDictionary.TryGetValue("生产令号", out lManufactureNumberColumn) && -1 != lManufactureNumberColumn && Cell.EmptyCell != (cell = rowPair.Value.GetCell(lManufactureNumberColumn))) { lProductEntity.ManufactureNumber = cell.StringValue; ProjectEntity projectEntityTemp; if (!ProjectEntityDictionary.TryGetValue(lProductEntity.ManufactureNumber, out projectEntityTemp)) { NotifyWindow lNotifyWindow = new NotifyWindow("错误", "第 " + rowPair.Key.ToString() + "行 系统中没有生产令号:" + lProductEntity.ManufactureNumber); lNotifyWindow.Show(); return; } } else { NotifyWindow lNotifyWindow = new NotifyWindow("错误", "第 " + rowPair.Key.ToString() + "行 系统中没有生产令号:" + lProductEntity.ManufactureNumber); lNotifyWindow.Show(); return; } } int lProjectNameColumn = -1; { Cell cell = Cell.EmptyCell; if (lHeaderDictionary.TryGetValue("序列号", out lProjectNameColumn) && -1 != lProjectNameColumn && Cell.EmptyCell != (cell = rowPair.Value.GetCell(lProjectNameColumn))) { lProductEntity.ProductID = cell.StringValue; ProductEntity lProductEntityTemp; if (ProductEntityDictionary.TryGetValue(lProductEntity.ProductID, out lProductEntityTemp)) { NotifyWindow lNotifyWindow = new NotifyWindow("错误", "第 " + rowPair.Key.ToString() + "行 序列号重复:" + lProductEntity.ProductID); lNotifyWindow.Show(); return; } if (CurrentProductEntityDicationary.TryGetValue(lProductEntity.ProductID, out lProductEntityTemp)) { NotifyWindow lNotifyWindow = new NotifyWindow("错误", "第 " + rowPair.Key.ToString() + "行 序列号重复:" + lProductEntity.ProductID); lNotifyWindow.Show(); return; } } } int lModelNumberColumn = -1; { Cell cell = Cell.EmptyCell; if (lHeaderDictionary.TryGetValue("产品类型", out lModelNumberColumn) && -1 != lModelNumberColumn && Cell.EmptyCell != (cell = rowPair.Value.GetCell(lModelNumberColumn))) { lProductEntity.ProductTypeString = cell.StringValue; int lId; if (ProductTypeIDDictionary.TryGetValue(lProductEntity.ProductTypeString, out lId)) { lProductEntity.ProductTypeID = lId; } else { NotifyWindow lNotifyWindow = new NotifyWindow("错误", "第 " + rowPair.Key.ToString() + "行 系统中无该产品类型:" + lProductEntity.ProductTypeString); lNotifyWindow.Show(); return; } } } int lRemarkColumn = -1; { Cell cell = Cell.EmptyCell; if (lHeaderDictionary.TryGetValue("名称", out lRemarkColumn) && -1 != lRemarkColumn && Cell.EmptyCell != (cell = rowPair.Value.GetCell(lRemarkColumn))) { lProductEntity.ProductName = cell.StringValue; } } int lInputTimeColume = -1; { Cell cell = Cell.EmptyCell; if (lHeaderDictionary.TryGetValue("编制日期", out lInputTimeColume) && -1 != lInputTimeColume && Cell.EmptyCell != (cell = rowPair.Value.GetCell(lInputTimeColume))) { lProductEntity.ProductIDCreateData = cell.DateTimeValue; } } int lOutputNumberColume = -1; { Cell cell = Cell.EmptyCell; if (lHeaderDictionary.TryGetValue("出厂编号", out lOutputNumberColume) && -1 != lOutputNumberColume && Cell.EmptyCell != (cell = rowPair.Value.GetCell(lOutputNumberColume))) { lProductEntity.ProductOutputNumber = cell.StringValue; } } int lNote1 = -1; { Cell cell = Cell.EmptyCell; if (lHeaderDictionary.TryGetValue("备注1", out lNote1) && -1 != lNote1 && Cell.EmptyCell != (cell = rowPair.Value.GetCell(lNote1))) { lProductEntity.ProductDescript1 = cell.StringValue; } } int lNote2 = -1; { Cell cell = Cell.EmptyCell; if (lHeaderDictionary.TryGetValue("备注1", out lNote2) && -1 != lNote2 && Cell.EmptyCell != (cell = rowPair.Value.GetCell(lNote2))) { lProductEntity.ProductDescript2 = cell.StringValue; } } lProductEntity.DUpdate(); ProductEntityList.Add(lProductEntity); CurrentProductEntityDicationary.Add(lProductEntity.ProductID, lProductEntity); //ProductContext.projects.Add(lProjectEntity.Project); } } catch (System.Exception ex) { Message.ErrorMessage(ex.Message); } } }