public static void Main(string[] args) { try { WcappExcel.Import(@"C:\Users\yedan\Desktop\WCAPP测试数据.xls"); } catch (Exception e) { Console.WriteLine(e.Message); } }
public ActionResult ImportExcel(HttpPostedFileBase file) { var dirInfo = new DirectoryInfo(AppDomain.CurrentDomain.BaseDirectory + "ExcelFiles"); if (!dirInfo.Exists) { dirInfo.Create(); } var fileName = AppDomain.CurrentDomain.BaseDirectory + "ExcelFiles/" + Guid.NewGuid() + ".xls"; var fs = new FileStream(fileName, FileMode.Create, FileAccess.Write); file.InputStream.CopyTo(fs); fs.Close(); try { var processes = WcappExcel.Import(fileName); //如果数据库有这条工艺规程 var db = new Context(); foreach (var process in processes) { if (process.No == "" || process.No == null) { continue; } var userId = Session.GetSessionUser().Id; var user = db.Users.SingleOrDefault(x => x.Id == userId);//InitialParams var procedbs = db.Processes.Include("Procedures.Seams.InitialParams").Where(x => x.No == process.No).ToList(); Log.Info(procedbs.Count.ToString()); if (procedbs.Count == 0) //数据库里没有这条数据,直接导入 { process.Author = user; //当前编制者 process.Establish = true; //标记为Excel导入 process.excelImportTime = DateTime.Now; var tasks = db.DispatchMessages.ToList(); foreach (var task in tasks) { if (task.PartNo == process.PartNo) { task.showState = false; } } db.Processes.Add(process); db.SaveChanges(); } else { if (procedbs.Count > 1) { return(Json(new { succeed = false, datas = process.No, error = "此条工艺规程已存在修订版本,请勿重复导入!" }, "text/html")); } else { foreach (var procedb in procedbs) { if (!procedb.Establish)//数据库有此条工艺规程,再判断是否为导入还是手动 { //手动创建的工艺规程编号和导入工艺规程编号一样 return(Json(new { succeed = false, datas = process.No, error = "手动创建的工艺规程编号和导入工艺规程编号一样,不可修改参数!" }, "text/html")); } // procedb.Author = user; if (procedb.Published) { return(Json(new { succeed = false, datas = process.No, error = "该工艺已发布!" }, "text/html")); } procedb.Published = process.Published;//发布 if (procedb.TestState == ProgramTestState.已完成) { return(Json(new { succeed = false, datas = process.No, error = "该工艺焊缝试焊状态已完成!" }, "text/html")); } procedb.TestState = process.TestState;//试焊 foreach (var procedures in process.Procedures) { var proceduredb = procedb.Procedures.SingleOrDefault(x => x.No == procedures.No); if (proceduredb == null) { return(Json(new { succeed = false, datas = process.No, error = "工序号不可修改!" }, "text/html")); } if (proceduredb.TestState == ProgramTestState.已完成) { return(Json(new { succeed = false, datas = process.No, error = "该工艺焊缝试焊状态已完成!" }, "text/html")); } proceduredb.TestState = procedures.TestState; foreach (var seam in procedures.Seams) { var seamdb = proceduredb.Seams.SingleOrDefault(x => x.No == seam.No); if (seamdb == null) { return(Json(new { succeed = false, datas = process.No, error = "焊缝号不可修改!" }, "text/html")); } if (seamdb.TestState == TestState.已完成) { return(Json(new { succeed = false, datas = process.No, error = "该工艺焊缝试焊状态已完成!" }, "text/html")); } seamdb.TestState = seam.TestState; if (!seamdb.InitialParams.Any()) { return(Json(new { succeed = false, datas = process.No, error = "该工艺焊缝无信息!" }, "text/html")); } if (seamdb.InitialParams.Any()) { foreach (var param in seam.InitialParams) { foreach (var smdb in seamdb.InitialParams) { if (smdb.Enum == param.Enum) { smdb.Value = param.Value; } } } } } } var tasks = db.DispatchMessages.ToList(); foreach (var task in tasks) { if (task.PartNo == process.PartNo) { task.showState = true; } } procedb.excelImportTime = DateTime.Now; procedb.Establish = true;//将导入的工艺规程编号字段Establish改为true db.SaveChanges(); } } } } return(Json(new { succeed = true, message = "导入成功!" }, "text/html")); } catch (Exception e) { Log.Error(e.StackTrace); return(Json(new { succeed = false, error = e.Message }, "text/html")); } }