public IActionResult ImportPuchasingOrderFromExcel(IFormFile formFile) { IWorkbook workbook = null; if (formFile == null) { return(Error("请添加要导入文件")); } else { string fileName = ContentDispositionHeaderValue.Parse(formFile.ContentDisposition).FileName.Trim('"'); fileName = _hostingEnvironment.WebRootPath + @"\CacheFile\" + fileName; using (FileStream fs = System.IO.File.Create(fileName)) { formFile.CopyTo(fs); fs.Flush(); fs.Close(); } FileStream fileStream = new FileStream(fileName, FileMode.Open, FileAccess.Read); if (fileName.IndexOf(".xlsx") > 0) { workbook = new XSSFWorkbook(fileStream); } else if (fileName.IndexOf(".xls") > 0) { workbook = new HSSFWorkbook(fileStream); } ISheet sheet = workbook.GetSheetAt(0); IRow row; Purchasing purchasing = new Purchasing(); purchasing.PurchasingNumber = _commonService.GetOrderSerialNumber("JR"); purchasing.State = PurchasingState.InitialStatus; purchasing.PurchasingOrderNumber = sheet.GetRow(0).GetCell(1)?.ToString().Trim(); //暂时不用读取时间 //purchasing.CreatedTime = Convert.ToDateTime(sheet.GetRow(1).GetCell(1).ToString().Trim()); var supplier = _purchasingService.GetSupplierByName(sheet.GetRow(2).GetCell(1).ToString().Trim()); if (supplier == null) { return(Error(string.Format("不存在名为:{0}的供应商", sheet.GetRow(2).GetCell(1).ToString().Trim()))); } else { purchasing.SupplierId = supplier.Id; } var wareHouse = _wareHouseService.GetWareHouseByName(sheet.GetRow(3).GetCell(1).ToString().Trim()); if (wareHouse == null) { return(Error(string.Format("不存在名为:{0}的仓库", sheet.GetRow(3).GetCell(1).ToString().Trim()))); } else { purchasing.WareHouseId = wareHouse.Id; } purchasing.Mark = sheet.GetRow(4).GetCell(1).ToString().Trim(); _purchasingService.AddPurchasing(purchasing); //日志 #region 新增采购订单 var mark = "新增采购订单:[" + purchasing.PurchasingNumber + "]"; _logService.InsertOrderTableLog("Purchasing", purchasing.Id, "新增采购订单", Convert.ToInt32(purchasing.State), mark); #endregion StringBuilder errStr = new StringBuilder(); int errorCount = 0; int succCount = 0; if (sheet.LastRowNum >= 5) { for (int i = 5; i <= sheet.LastRowNum; i++) { row = sheet.GetRow(i); var productCode = row.GetCell(0).ToString().Trim(); try { var product = _productService.GetProductByCode(productCode); if (product == null) { errorCount++; errStr.Append(string.Format("不存在编码为{0}的商品;", productCode)); } var purchasProduct = new PurchasingProducts(); purchasProduct.PurchasingId = purchasing.Id; purchasProduct.ProductId = product.Id; purchasProduct.Quantity = Convert.ToInt32(row.GetCell(1).ToString().Trim()); purchasProduct.Price = Convert.ToDecimal(row.GetCell(3).ToString().Trim()) * Convert.ToInt32(row.GetCell(2).ToString().Trim()); _purchasingService.AddPurchasingOrderProduct(purchasProduct); succCount++; } catch (Exception ex) { errorCount++; errStr.Append(string.Format("采购订单添加编码为{0}的商品时出现异常;", productCode)); _logService.Error("采购订单" + purchasing.PurchasingNumber + "商品" + productCode + "导入错误:" + ex.Message); } } System.IO.File.Delete(fileName); fileStream.Close(); } if (errorCount > 0) { string err = "Excel导入采购订单商品时出现错误,错误信息:" + errStr.ToString(); _logService.Error(err); return(Error(err)); } else { return(Success("导入成功")); } } }