public ActionResult ImportPartRecord(HttpPostedFileBase partFile) { if (partFile == null) { //TODO: Parts 上传, 如果没有路径,在此处进行友好处理 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(partFile.FileName)); partFile.SaveAs(filename); string ex = Path.GetExtension(filename); List <PartImportModel> records = new List <PartImportModel>(); if (ex.Equals(".csv")) { CsvConfiguration configuration = new CsvConfiguration(); configuration.Delimiter = Settings.Default.csvDelimiter; configuration.HasHeaderRecord = true; configuration.SkipEmptyRecords = true; configuration.RegisterClassMap <PartCsvModelMap>(); configuration.TrimHeaders = true; configuration.TrimFields = true; try { using (TextReader treader = System.IO.File.OpenText(filename)) { CsvReader csvReader = new CsvReader(treader, configuration); records = csvReader.GetRecords <PartImportModel>().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> > ActionNullErrorDic = new List <Dictionary <string, string> >(); List <Dictionary <string, string> > OtherErrorDic = new List <Dictionary <string, string> >(); if (records.Count > 0) { IPartService ps = new PartService(Settings.Default.db); int AllQty = records.Count; int CreateSuccessQty = 0; int CreateFailureQty = 0; int UpdateSuccessQty = 0; int UpdateFailureQty = 0; int ActionNullQty = 0; int OtherQty = 0; foreach (PartImportModel record in records) { if (string.IsNullOrWhiteSpace(record.Action)) { ActionNullQty++; Dictionary <string, string> ActionNullErrorList = new Dictionary <string, string>(); ActionNullErrorList.Add("partNr", record.PartNr); ActionNullErrorList.Add("partType", record.PartType.ToString()); ActionNullErrorList.Add("partDesc", record.PartDesc); ActionNullErrorList.Add("partStatus", record.PartStatus.ToString()); ActionNullErrorList.Add("MOQ", record.MOQ.ToString()); ActionNullErrorList.Add("SPQ", record.SPQ.ToString()); ActionNullErrorList.Add("Action", record.Action); ActionNullErrorDic.Add(ActionNullErrorList); ViewData["actionNullErrorDic"] = ActionNullErrorDic; } else { //新建 Part part = new Part() { partNr = record.PartNr, partType = record.PartType, partDesc = record.PartDesc, partStatus = record.PartStatus, moq = record.MOQ, spq = record.SPQ }; if (record.Action.Trim().ToLower().Equals("create")) { try { bool result = ps.Create(part); if (result) { CreateSuccessQty++; } else { CreateFailureQty++; Dictionary <string, string> CreateErrorList = new Dictionary <string, string>(); CreateErrorList.Add("partNr", record.PartNr); CreateErrorList.Add("partType", record.PartType.ToString()); CreateErrorList.Add("partDesc", record.PartDesc); CreateErrorList.Add("partStatus", record.PartStatus.ToString()); CreateErrorList.Add("MOQ", record.MOQ.ToString()); CreateErrorList.Add("SPQ", record.SPQ.ToString()); CreateErrorList.Add("Action", record.Action); CreateErrorDic.Add(CreateErrorList); ViewData["createErrorDic"] = CreateErrorDic; } } catch (Exception) { CreateFailureQty++; //ViewBag.CreateExpMsg = "<-------------Create Part Exception!,Maybe partNr is Exist,Please Check.------------->"; ViewBag.CreateExpMsg = "<-------------创建零件异常,可能PartNr列已经存在,请仔细检查。------------->"; } } else if (record.Action.Trim().ToLower().Equals("update")) { //更新 try { bool result = ps.Update(part); if (result) { UpdateSuccessQty++; } else { UpdateFailureQty++; Dictionary <string, string> UpdateErrorList = new Dictionary <string, string>(); UpdateErrorList.Add("partNr", record.PartNr); UpdateErrorList.Add("partType", record.PartType.ToString()); UpdateErrorList.Add("partDesc", record.PartDesc); UpdateErrorList.Add("partStatus", record.PartStatus.ToString()); UpdateErrorList.Add("MOQ", record.MOQ.ToString()); UpdateErrorList.Add("SPQ", record.SPQ.ToString()); UpdateErrorList.Add("Action", record.Action); UpdateErrorDic.Add(UpdateErrorList); ViewData["updateErrorDic"] = UpdateErrorDic; } } catch (Exception e) { UpdateFailureQty++; //ViewBag.UpdateExpMsg = "<-------------Update Part Exception!,Please Check.------------->" + e; ViewBag.UpdateExpMsg = "<-------------更新零件异常,可能ID或PartNr不存在,请仔细检查。------------->" + e; } } else { //错误 不做任何操作,只需要记录 Dictionary <string, string> OtherErrorList = new Dictionary <string, string>(); OtherErrorList.Add("partNr", record.PartNr); OtherErrorList.Add("partType", record.PartType.ToString()); OtherErrorList.Add("partDesc", record.PartDesc); OtherErrorList.Add("partStatus", record.PartStatus.ToString()); OtherErrorList.Add("MOQ", record.MOQ.ToString()); OtherErrorList.Add("SPQ", record.SPQ.ToString()); OtherErrorList.Add("Action", record.Action); OtherErrorDic.Add(OtherErrorList); ViewData["otherErrorDic"] = OtherErrorDic; } } } OtherQty = AllQty - CreateSuccessQty - CreateFailureQty - UpdateSuccessQty - UpdateFailureQty - 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("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()); }