public int Upload(string fileName, out string returnMsg, bool isInitial = false) { ExcelPackage package; IList <Item> listOfItems = new List <Item>(); returnMsg = string.Empty; //string strorageconn = System.Configuration.ConfigurationSettings.AppSettings.Get("StorageConnectionString"); //CloudStorageAccount storageacc = CloudStorageAccount.Parse(strorageconn); //CloudBlobClient blobClient = storageacc.CreateCloudBlobClient(); //CloudBlobContainer container = blobClient.GetContainerReference("itemimages"); if (isInitial) { var excelFile = Resource.仓库物品资料汇总表; var templateStream = new MemoryStream(excelFile); package = new ExcelPackage(templateStream); } else { if (string.IsNullOrEmpty(fileName)) { return(-2); } var fi = new FileInfo(fileName); package = new ExcelPackage(fi); } var workbook = package.Workbook; var worksheet = workbook.Worksheets["物品资料汇总表"]; if (worksheet == null) { return(0); } InitalImagesToLocal(_resourceName, worksheet); var end = worksheet.Dimension.End; for (var row = 9; row < end.Row; row++) { var itemObject = new Item { ItemId = Guid.NewGuid(), DefaultPositionName = "Stage", UpdateDate = DateTime.Now }; for (var col = 1; col <= worksheet.Dimension.End.Column; col++) { var readValue = worksheet.Cells[row, col].Value == null ? "" : worksheet.Cells[row, col].Value.ToString(); switch (col) { case 1: if (readValue == "") { break; } else { itemObject.SerialNumber = int.Parse(readValue); //序号 } break; case 3: itemObject.Code = readValue; // 编号 break; case 4: itemObject.Status = readValue; //状态 if (readValue.Contains("Active")) { itemObject.Status = "现行Active"; } else if (readValue.Contains("Discountinue")) { itemObject.Status = "中止Discountinue"; } else if (readValue.Contains("Cancel")) { itemObject.Status = "取消Cancel"; } else if (readValue.Contains("Deactive")) { itemObject.Status = "下线Deactive"; } else { itemObject.Status = ""; } break; case 5: itemObject.Category = readValue; //物品类别 break; case 6: itemObject.ProjectCategory = readValue; //所属转项类 break; case 7: itemObject.SubCategory = readValue; //物品种类 break; case 8: itemObject.BigCategory = readValue; //物品大类 break; case 9: itemObject.SmallCategory = readValue; //物品小类 break; case 10: itemObject.DetailCategory = readValue; //物品细类 break; case 11: itemObject.AdjustCategory = readValue; //调整类 break; case 12: itemObject.Attribute = readValue; //物品属性 break; case 13: itemObject.Property = readValue; //资产类型 break; case 14: itemObject.ChineseName = readValue.Replace("\n", " "); //中文名 break; case 15: itemObject.EnglishName = readValue.Replace("\n", " "); //英文名 break; case 16: break; case 17: itemObject.Brand = readValue; //品牌 break; case 18: itemObject.Model = readValue; //型号 break; case 19: itemObject.Specification = readValue; //规格 break; case 20: itemObject.Dimension = readValue; //尺寸 break; case 21: itemObject.Length = readValue; //长 break; case 22: itemObject.Width = readValue; //宽 break; case 23: itemObject.Height = readValue; //高 break; case 25: itemObject.Unit = readValue; //单位 break; case 26: if (readValue == "") { itemObject.Price = 0; } else { itemObject.Price = double.Parse(readValue); //单价 } break; case 27: itemObject.Package = readValue; //包装数量 break; case 28: itemObject.PackageLength = readValue; //包装长 break; case 29: itemObject.PackageWidth = readValue; //包装宽 break; case 30: itemObject.PackageHeight = readValue; //包装高 break; case 31: itemObject.Detail = readValue; //详细信息链接 break; case 35: if (readValue == "") { itemObject.Max = 0; } else { itemObject.Max = double.Parse(readValue); //库存上限 } break; case 36: if (readValue == "") { itemObject.Min = 0; } else { itemObject.Min = double.Parse(readValue); //库存下限 } break; case 38: itemObject.FirstSupplier = readValue.TrimEnd().TrimStart(); // 一级供应商 break; case 41: itemObject.SecondSupplier = readValue.TrimEnd().TrimStart(); // 二级供应商 break; case 44: itemObject.ThirdSupplier = readValue.TrimEnd().TrimStart(); // 三级供应商 break; case 48: itemObject.CostCategory = readValue; break; case 50: itemObject.ArrangeOrder = readValue; break; case 51: itemObject.ArrangePosition = readValue; break; } } if (itemObject.SerialNumber > 0) { listOfItems.Add(itemObject); } } try { var existingItems = _itemRepository.GetAll(); var existingPositions = _positionRepository.GetAll(); if (!existingItems.Any()) { _itemRepository.CleanUp(); _itemRepository.AddRange(listOfItems); } else { foreach (var item in listOfItems) { var existingItem = existingItems.FirstOrDefault(x => x.Code == item.Code); if (existingItem != null) { existingItem.Status = item.Status; existingItem.Category = item.Category; existingItem.ProjectCategory = item.ProjectCategory; existingItem.SubCategory = item.SubCategory; existingItem.BigCategory = item.BigCategory; existingItem.SmallCategory = item.SmallCategory; existingItem.DetailCategory = item.DetailCategory; existingItem.AdjustCategory = item.AdjustCategory; existingItem.Attribute = item.Attribute; existingItem.Property = item.Property; existingItem.ChineseName = item.ChineseName; existingItem.EnglishName = item.EnglishName; existingItem.Brand = item.Brand; existingItem.Model = item.Model; existingItem.Specification = item.Specification; existingItem.Dimension = item.Dimension; existingItem.Length = item.Length; existingItem.Width = item.Width; existingItem.Height = item.Height; existingItem.Unit = item.Unit; existingItem.Price = item.Price; existingItem.Package = item.Package; existingItem.PackageLength = item.PackageLength; existingItem.PackageWidth = item.PackageWidth; existingItem.PackageHeight = item.PackageHeight; existingItem.Detail = item.Detail; existingItem.Max = item.Max; existingItem.Min = item.Min; existingItem.CostCategory = item.CostCategory; existingItem.ArrangeOrder = item.ArrangeOrder; existingItem.ArrangePosition = item.ArrangePosition; existingItem.Comments = item.Comments; existingItem.FirstSupplier = item.FirstSupplier; existingItem.SecondSupplier = item.SecondSupplier; existingItem.ThirdSupplier = item.ThirdSupplier; } else { var positionObject = new Position() { ItemId = item.ItemId, Code = item.Code, PositionName = "Stage" }; _positionRepository.Add(positionObject); _itemRepository.Add(item); } } } _itemRepository.Save(); _positionRepository.Save(); return(1); } catch (Exception) { return(0); } }