public ActionResult EMList_Edit([Bind(Include = "EM_Position_Code,EM_Position_Full_Name,EM_ID,User_Code,EM_Code,EM_Full_Name,Contract_Code,Signal_Day,Signal_Month,Signal_Year,EM_Status,Note,CreateDate,CreateBy")] EM_List eM_List) { if (ModelState.IsValid) { db.Entry(eM_List).State = EntityState.Modified; db.SaveChanges(); return(RedirectToAction("Index")); } return(View(eM_List)); }
public ActionResult EMList_Edit(int?id) { if (id == null) { return(new HttpStatusCodeResult(HttpStatusCode.BadRequest)); } EM_List eM_List = db.EM_List.Find(id); if (eM_List == null) { return(HttpNotFound()); } return(PartialView(eM_List)); }
public ActionResult EMList_MultiUpload(HttpPostedFileBase file) { try { if (file.ContentLength > 0) { ViewBag.ErrorEMListFileName = ""; ViewBag.CurrentFunctionExcelUpload = "EMList"; string ErrorFileName = ""; string _FileName = Path.GetFileName(file.FileName); string _path = Path.Combine(Server.MapPath("~/App_Data/uploads"), _FileName); string _TemplatePath = Path.Combine(Server.MapPath("~/App_Data/uploads"), "EMList_Template.xlsx"); string _FileExtension = Path.GetExtension(_FileName); if (_FileExtension == ".xlsx") { // lưu file excel lại với tên Usercode_filename.xlsxS string FileNameToSave = Path.Combine(Server.MapPath("~/App_Data/uploads"), Request.Cookies["UserName"].Value.ToString() + "_" + _FileName); file.SaveAs(FileNameToSave); //read file index excel FileStream fsIndex = new FileStream(FileNameToSave, FileMode.Open); XSSFWorkbook wbIndex = new XSSFWorkbook(fsIndex); ISheet sheetIndex = wbIndex.GetSheetAt(0); var rowIndex = sheetIndex.GetRow(0); //read file template excel FileStream fsTemplate = new FileStream(_TemplatePath, FileMode.Open); XSSFWorkbook wbTemplate = new XSSFWorkbook(fsTemplate); ISheet sheetTemplate = wbTemplate.GetSheetAt(0); var rowTemplate = sheetTemplate.GetRow(0); //vòng lặp kiểm tra file upload != file template for (int i = 0; i <= 17; i++) { if (rowTemplate.GetCell(0).StringCellValue != rowIndex.GetCell(0).StringCellValue) { ViewBag.Message = "Sai định dạng template"; return(PartialView()); } } if (sheetIndex.LastRowNum == 0) { ViewBag.Message = "File rỗng!"; return(PartialView()); } bool checkError = true; for (int i = 1; i <= sheetIndex.LastRowNum; i++) { string error = ""; var rowNow = sheetIndex.GetRow(i); // cộng lỗi vào chuổi error //CHECK_IS_NULL kiểm tra khác rổng //CHECK_NUMBER_TYPE kiểm tra dạng sổ //CHECK_ISNULL_AND_NUMBER kiểm khác rống và dạng sô error += Check_Format_Excel(sheetIndex, CHECK_IS_NULL, i, 0); error += Check_Format_Excel(sheetIndex, CHECK_IS_NULL, i, 1); error += Check_Format_Excel(sheetIndex, CHECK_IS_NULL, i, 2); error += Check_Format_Excel(sheetIndex, CHECK_IS_NULL, i, 3); error += Check_Format_Excel(sheetIndex, CHECK_IS_NULL, i, 4); error += Check_Format_Excel(sheetIndex, CHECK_IS_NULL, i, 5); error += Check_Format_Excel(sheetIndex, CHECK_IS_NULL, i, 6); error += Check_Format_Excel(sheetIndex, CHECK_IS_NULL, i, 7); error += Check_Format_Excel(sheetIndex, CHECK_IS_NULL, i, 8); error += Check_Format_Excel(sheetIndex, CHECK_IS_NULL, i, 9); if (error == "") { EM_List EMlist = new EM_List(); EMlist.EM_Position_Code = rowNow.GetCell(0).ToString(); EMlist.EM_Position_Full_Name = rowNow.GetCell(1).ToString(); EMlist.User_Code = rowNow.GetCell(2).ToString(); EMlist.EM_Code = rowNow.GetCell(3).ToString(); EMlist.EM_Full_Name = rowNow.GetCell(4).ToString(); EMlist.Contract_Code = rowNow.GetCell(5).ToString(); EMlist.Signal_Day = Int32.Parse(rowNow.GetCell(6).ToString()); EMlist.Signal_Month = Int32.Parse(rowNow.GetCell(7).ToString()); EMlist.Signal_Year = Int32.Parse(rowNow.GetCell(8).ToString()); EMlist.EM_Status = rowNow.GetCell(9).ToString(); if (rowNow.GetCell(10) != null) { EMlist.Note = rowNow.GetCell(10).ToString(); } //userbasic.User_Code = userbasic.Role + //db.User_Basic.Add(userbasic); //db.SaveChanges(); } else { checkError = false; var RowErrorIndex = sheetTemplate.CreateRow(i); for (int j = 0; j <= 17; j++) { if (rowNow.GetCell(j) != null) { RowErrorIndex.CreateCell(j).SetCellValue(rowNow.GetCell(j).ToString()); } } RowErrorIndex.CreateCell(17).SetCellValue(error); } } if (!checkError) { ErrorFileName = Request.Cookies["UserName"].Value.ToString() + "_EMList_Error.xlsx"; string ErrorPath = Path.Combine(Server.MapPath("~/App_Data/uploads"), ErrorFileName); FileStream fsError = new FileStream(ErrorPath, FileMode.Create); wbTemplate.Write(fsError); fsError.Close(); fsTemplate.Close(); fsIndex.Close(); ViewBag.Message = "Lỗi trong file upload."; ViewBag.ErrorEMListFileName = ErrorFileName; return(PartialView()); } } else { ViewBag.Message = "Sai định dạng file .xlsx"; return(PartialView()); } } ViewBag.Message = "Cập nhật thành công!"; return(PartialView()); } catch { ViewBag.Message = "Cập nhật thất bại!"; return(PartialView()); } }