public ActionResult ImportBomRecord(HttpPostedFileBase bomFile) { if (bomFile == null) { //TODO: Bom Import 优化上传失败界面 throw new Exception("No file is uploaded to system"); } var appData = Server.MapPath("~/TmpFile/"); var filename = Path.Combine(appData, DateTime.Now.ToString("yyyyMMddHHmmss") + "_" + Path.GetFileName(bomFile.FileName)); bomFile.SaveAs(filename); string ex = Path.GetExtension(filename); List <BomImportModel> records = new List <BomImportModel>(); if (ex.Equals(".csv")) { CsvConfiguration configuration = new CsvConfiguration(); configuration.Delimiter = Settings.Default.csvDelimiter; configuration.HasHeaderRecord = true; configuration.SkipEmptyRecords = true; configuration.RegisterClassMap <BomCsvModelMap>(); configuration.TrimHeaders = true; configuration.TrimFields = true; try { using (TextReader treader = System.IO.File.OpenText(filename)) { CsvReader csvReader = new CsvReader(treader, configuration); records = csvReader.GetRecords <BomImportModel>().ToList(); } } catch (Exception e) { //ViewBag.TextExpMsg = "<-------------Read Csv File Exception!,Please Check.------------->" + e; ViewBag.TextExpMsg = "<-------------读取CSV文件异常,请查看原因:------------->" + e; } List <Dictionary <string, string> > CreateErrorDic = new List <Dictionary <string, string> >(); List <Dictionary <string, string> > UpdateErrorDic = new List <Dictionary <string, string> >(); List <Dictionary <string, string> > DeleteErrorDic = new List <Dictionary <string, string> >(); List <Dictionary <string, string> > ActionNullErrorDic = new List <Dictionary <string, string> >(); List <Dictionary <string, string> > OtherErrorDic = new List <Dictionary <string, string> >(); if (records.Count > 0) { int AllQty = records.Count; int CreateSuccessQty = 0; int CreateFailureQty = 0; int UpdateSuccessQty = 0; int UpdateFailureQty = 0; int DeleteSuccessQty = 0; int DeleteFailureQty = 0; int ActionNullQty = 0; int OtherQty = 0; IBomService ps = new BomService(Settings.Default.db); foreach (BomImportModel record in records) { if (string.IsNullOrWhiteSpace(record.Action)) { ActionNullQty++; Dictionary <string, string> ActionNullErrorList = new Dictionary <string, string>(); ActionNullErrorList.Add("ID", record.ID); ActionNullErrorList.Add("PartNr", record.PartNr); ActionNullErrorList.Add("ValidFrom", record.ValidFrom.ToString()); ActionNullErrorList.Add("ValidTo", record.ValidTo.ToString()); ActionNullErrorList.Add("VersionId", record.VersionId); ActionNullErrorList.Add("BomDesc", record.BomDesc); ActionNullErrorList.Add("Action", record.Action); ActionNullErrorDic.Add(ActionNullErrorList); ViewData["actionNullErrorDic"] = ActionNullErrorDic; } else { //新建 BOM bom = new BOM() { id = record.ID, partNr = record.PartNr, validFrom = record.ValidFrom, validTo = record.ValidTo, versionId = record.VersionId, bomDesc = record.BomDesc }; if (record.Action.Trim().ToLower().Equals("create")) { try { ps.Create(bom); CreateSuccessQty++; } catch (Exception) { CreateFailureQty++; Dictionary <string, string> CreateErrorList = new Dictionary <string, string>(); CreateErrorList.Add("ID", record.ID); CreateErrorList.Add("PartNr", record.PartNr); CreateErrorList.Add("ValidFrom", record.ValidFrom.ToString()); CreateErrorList.Add("ValidTo", record.ValidTo.ToString()); CreateErrorList.Add("VersionId", record.VersionId); CreateErrorList.Add("BomDesc", record.BomDesc); CreateErrorList.Add("Action", record.Action); CreateErrorDic.Add(CreateErrorList); ViewData["createErrorDic"] = CreateErrorDic; } } else if (record.Action.Trim().ToLower().Equals("update")) { //更新 try { bool result = ps.Update(bom); if (result) { UpdateSuccessQty++; } else { UpdateFailureQty++; Dictionary <string, string> UpdateErrorList = new Dictionary <string, string>(); UpdateErrorList.Add("ID", record.ID); UpdateErrorList.Add("PartNr", record.PartNr); UpdateErrorList.Add("ValidFrom", record.ValidFrom.ToString()); UpdateErrorList.Add("ValidTo", record.ValidTo.ToString()); UpdateErrorList.Add("VersionId", record.VersionId); UpdateErrorList.Add("BomDesc", record.BomDesc); UpdateErrorList.Add("Action", record.Action); UpdateErrorDic.Add(UpdateErrorList); ViewData["updateErrorDic"] = UpdateErrorDic; } } catch (Exception) { UpdateFailureQty++; Dictionary <string, string> UpdateErrorList = new Dictionary <string, string>(); UpdateErrorList.Add("ID", record.ID); UpdateErrorList.Add("PartNr", record.PartNr); UpdateErrorList.Add("ValidFrom", record.ValidFrom.ToString()); UpdateErrorList.Add("ValidTo", record.ValidTo.ToString()); UpdateErrorList.Add("VersionId", record.VersionId); UpdateErrorList.Add("BomDesc", record.BomDesc); UpdateErrorList.Add("Action", record.Action); UpdateErrorDic.Add(UpdateErrorList); ViewData["updateErrorDic"] = UpdateErrorDic; } } else if (record.Action.Trim().ToLower().Equals("delete")) { try { //删除 bool result = ps.Delete(bom); if (result) { DeleteSuccessQty++; } else { DeleteFailureQty++; Dictionary <string, string> DeleteErrorList = new Dictionary <string, string>(); DeleteErrorList.Add("ID", record.ID); DeleteErrorList.Add("PartNr", record.PartNr); DeleteErrorList.Add("ValidFrom", record.ValidFrom.ToString()); DeleteErrorList.Add("ValidTo", record.ValidTo.ToString()); DeleteErrorList.Add("VersionId", record.VersionId); DeleteErrorList.Add("BomDesc", record.BomDesc); DeleteErrorList.Add("Action", record.Action); DeleteErrorDic.Add(DeleteErrorList); ViewData["deleteErrorDic"] = DeleteErrorDic; } } catch (Exception) { DeleteFailureQty++; Dictionary <string, string> DeleteErrorList = new Dictionary <string, string>(); DeleteErrorList.Add("ID", record.ID); DeleteErrorList.Add("PartNr", record.PartNr); DeleteErrorList.Add("ValidFrom", record.ValidFrom.ToString()); DeleteErrorList.Add("ValidTo", record.ValidTo.ToString()); DeleteErrorList.Add("VersionId", record.VersionId); DeleteErrorList.Add("BomDesc", record.BomDesc); DeleteErrorList.Add("Action", record.Action); DeleteErrorDic.Add(DeleteErrorList); ViewData["deleteErrorDic"] = DeleteErrorDic; } } else { //错误 忽略 Dictionary <string, string> OtherErrorList = new Dictionary <string, string>(); OtherErrorList.Add("ID", record.ID); OtherErrorList.Add("PartNr", record.PartNr); OtherErrorList.Add("ValidFrom", record.ValidFrom.ToString()); OtherErrorList.Add("ValidTo", record.ValidTo.ToString()); OtherErrorList.Add("VersionId", record.VersionId); OtherErrorList.Add("BomDesc", record.BomDesc); OtherErrorList.Add("Action", record.Action); OtherErrorDic.Add(OtherErrorList); ViewData["otherErrorDic"] = OtherErrorDic; } } } OtherQty = AllQty - CreateSuccessQty - CreateFailureQty - UpdateSuccessQty - UpdateFailureQty - DeleteSuccessQty - DeleteFailureQty - ActionNullQty; Dictionary <string, int> Qty = new Dictionary <string, int>(); Qty.Add("AllQty", AllQty); Qty.Add("CreateSuccessQty", CreateSuccessQty); Qty.Add("CreateFailureQty", CreateFailureQty); Qty.Add("UpdateSuccessQty", UpdateSuccessQty); Qty.Add("UpdateFailureQty", UpdateFailureQty); Qty.Add("DeleteSuccessQty", DeleteSuccessQty); Qty.Add("DeleteFailureQty", DeleteFailureQty); Qty.Add("ActionNullQty", ActionNullQty); Qty.Add("OtherQty", OtherQty); ViewData["Qty"] = Qty; } else { //ViewBag.NotCheckedData = "No Data Checked. Please Check Delimiter or Column Name."; ViewBag.NotCheckedData = "没有检测到数据。请检查分隔符和列名。"; } } else { //ViewBag.NotCsv = "Your File is not .Csv File, Please Check FileName."; ViewBag.NotCsv = "你上传的文件不是.CSV格式。请检查文件名。"; } if (ViewBag.NotCsv == null) { //ViewBag.NotCsv = "CSV File is OK."; ViewBag.NotCsv = "上传CSV文件正确!"; } if (ViewBag.NotCheckedData == null) { //ViewBag.NotCheckedData = "Check Data is OK."; ViewBag.NotCheckedData = "检查数据完成!"; } return(View()); }