// GET: Upload public ActionResult Index() { UploadObjectInfo info; if (Session["UploadDrugInfo"] != null) { info = (UploadObjectInfo)Session["UploadDrugInfo"]; Session.Remove("UploadDrugInfo"); } else { info = new UploadObjectInfo(); info.Title = "Không đọc được dữ liệu từ Session Info"; } return(View(info)); }
//public ActionResult Upload(HttpPostedFileBase uploadFile) //{ // var strValidations = new StringBuilder(string.Empty); // try // { // if (uploadFile.ContentLength > 0) // { // string filePath = Path.Combine(HttpContext.Server.MapPath("../Uploads"), // Path.GetFileName(uploadFile.FileName)); // uploadFile.SaveAs(filePath); // var ds = new DataSet(); // //A 32-bit provider which enables the use of // string connectionString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + filePath + // ";Extended Properties=Excel 12.0;"; // var maNhaThuoc = this.GetNhaThuoc().MaNhaThuoc; // using (var conn = new OleDbConnection(connectionString)) // { // conn.Open(); // using (var dtExcelSchema = conn.GetSchema("Tables")) // { // string sheetName = dtExcelSchema.Rows[0]["TABLE_NAME"].ToString(); // string query = "SELECT * FROM [" + sheetName + "]"; // var adapter = new OleDbDataAdapter(query, conn); // //DataSet ds = new DataSet(); // adapter.Fill(ds, "Items"); // if (ds.Tables.Count > 0) // { // if (ds.Tables[0].Rows.Count > 0) // { // for (int i = 0; i < ds.Tables[0].Rows.Count; i++) // { // var row = ds.Tables[0].Rows[i]; // var tenbacsy = row[0].ToString().Trim(); // var sodienthoai = row[2].ToString().Trim(); // if (!String.IsNullOrWhiteSpace(tenbacsy)) // { // var bacsy = new BacSy(); // if (!String.IsNullOrWhiteSpace(sodienthoai)) // { // bacsy = unitOfWork.BacSyRespository.GetMany(x => x.NhaThuoc.MaNhaThuoc == maNhaThuoc && x.TenBacSy == tenbacsy && x.DienThoai == sodienthoai).FirstOrDefault(); // } // else // { // bacsy =unitOfWork.BacSyRespository.GetMany(x => x.NhaThuoc.MaNhaThuoc == maNhaThuoc && x.TenBacSy == tenbacsy).FirstOrDefault(); // } // //Check if thuoc already exist // if (bacsy != null) // { // Bindbacsy(ref bacsy, row); // } // else // { // bacsy = new BacSy() // { // MaBacSy = 0, // NhaThuoc = unitOfWork.NhaThuocRepository.GetById(maNhaThuoc), // Created = DateTime.Now, // CreatedBy = unitOfWork.UserProfileRepository.GetById(WebSecurity.GetCurrentUserId) // }; // unitOfWork.BacSyRespository.Insert(Bindbacsy(ref bacsy, row)) ; // } // } // } // unitOfWork.Save(); // } // } // } // } // } // } // catch (Exception ex) // { // ViewBag.Message = ex.Message; // return View("Error"); // } // return RedirectToAction("Index"); //} // [Audit] public ActionResult Upload(HttpPostedFileBase uploadFile) { var strValidations = new StringBuilder(string.Empty); try { if (uploadFile.ContentLength > 0) { string filePath = Path.Combine(HttpContext.Server.MapPath("../Uploads"), Path.GetFileName(uploadFile.FileName)); uploadFile.SaveAs(filePath); int totalupdated = 0; int totaladded = 0; int totalError = 0; string message = "<b>Thông tin bác sỹ ở dòng số {0} bị lỗi:</b><br/> {1}"; UploadObjectInfo info = new UploadObjectInfo(); var maNhaThuoc = this.GetNhaThuoc().MaNhaThuoc; foreach (var worksheet in Workbook.Worksheets(filePath)) { for (int i = 1; i < worksheet.Rows.Count(); i++) { var row = worksheet.Rows[i]; var msg = ValidateDataImport(row); if (!string.IsNullOrEmpty(msg)) { if (msg == Constants.Params.msgOk) { var tenbacsy = row.Cells[0].Text.Trim(); var sodienthoai = row.Cells[2] != null ? row.Cells[2].Text.Trim() : string.Empty; if (!String.IsNullOrWhiteSpace(tenbacsy)) { var bacsy = new BacSy(); if (!String.IsNullOrWhiteSpace(sodienthoai)) { bacsy = unitOfWork.BacSyRespository.GetMany(x => x.NhaThuoc.MaNhaThuoc == maNhaThuoc && x.TenBacSy == tenbacsy && x.DienThoai == sodienthoai).FirstOrDefault(); } else { bacsy = unitOfWork.BacSyRespository.GetMany(x => x.NhaThuoc.MaNhaThuoc == maNhaThuoc && x.TenBacSy == tenbacsy).FirstOrDefault(); } //Check if thuoc already exist if (bacsy != null) { Bindbacsy(ref bacsy, row); totalupdated++; } else { bacsy = new BacSy() { MaBacSy = 0, NhaThuoc = unitOfWork.NhaThuocRepository.GetById(maNhaThuoc), Created = DateTime.Now, CreatedBy = unitOfWork.UserProfileRepository.GetById(WebSecurity.GetCurrentUserId) }; unitOfWork.BacSyRespository.Insert(Bindbacsy(ref bacsy, row)); totaladded++; } } } else { info.ErrorMsg.Add(string.Format(message, i, msg)); totalError++; } } } unitOfWork.Save(); info.Title = "Thông tin upload bác sỹ"; info.TotalUpdated = totalupdated; info.TotalAdded = totaladded; info.TotalError = totalError; Session["UploadMessage"] = info; return(RedirectToAction("index", "Upload")); } } } catch (Exception ex) { ViewBag.Message = ""; ViewBag.FullMessage = ex.Message; return(View("Error")); } return(RedirectToAction("Index")); }
//public ActionResult Upload(HttpPostedFileBase uploadFile) //{ // var strValidations = new StringBuilder(string.Empty); // var maNhaThuoc = this.GetNhaThuoc().MaNhaThuoc; // try // { // if (uploadFile.ContentLength > 0) // { // string filePath = Path.Combine(HttpContext.Server.MapPath("../Uploads"), // Path.GetFileName(uploadFile.FileName)); // uploadFile.SaveAs(filePath); // var ds = new DataSet(); // //A 32-bit provider which enables the use of // string connectionString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + filePath + // ";Extended Properties=Excel 12.0;"; // using (var conn = new OleDbConnection(connectionString)) // { // conn.Open(); // using (var dtExcelSchema = conn.GetSchema("Tables")) // { // string sheetName = dtExcelSchema.Rows[0]["TABLE_NAME"].ToString(); // string query = "SELECT * FROM [" + sheetName + "]"; // var adapter = new OleDbDataAdapter(query, conn); // //DataSet ds = new DataSet(); // adapter.Fill(ds, "Items"); // if (ds.Tables.Count > 0) // { // if (ds.Tables[0].Rows.Count > 0) // { // for (int i = 0; i < ds.Tables[0].Rows.Count; i++) // { // var row = ds.Tables[0].Rows[i]; // var tennhacungcap = row[1].ToString().Trim(); // var sodienthoai = row[3].ToString().Trim(); // if (!String.IsNullOrWhiteSpace(tennhacungcap)) // { // var nhacungcap = new NhaCungCap(); // if (!String.IsNullOrWhiteSpace(sodienthoai)) // { // nhacungcap =unitOfWork.NhaCungCapRespository.GetMany(x => x.NhaThuoc.MaNhaThuoc == maNhaThuoc && x.TenNhaCungCap == tennhacungcap && x.SoDienThoai == sodienthoai).FirstOrDefault(); // } // else // { // nhacungcap = unitOfWork.NhaCungCapRespository.GetMany(x => x.NhaThuoc.MaNhaThuoc == maNhaThuoc && x.TenNhaCungCap == tennhacungcap).FirstOrDefault(); // } // //Check if thuoc already exist // if (nhacungcap != null) // { // BindNhaCungCap(ref nhacungcap, row); // } // else // { // nhacungcap = new NhaCungCap() // { // MaNhaCungCap = 0, // NhaThuoc = unitOfWork.NhaThuocRepository.GetById(maNhaThuoc), // CreatedBy = unitOfWork.UserProfileRepository.GetById(WebSecurity.GetCurrentUserId), // Created = DateTime.Now // }; // unitOfWork.NhaCungCapRespository.Insert(BindNhaCungCap(ref nhacungcap, row)); // } // } // } // unitOfWork.Save(); // } // } // } // } // } // } // catch (Exception ex) // { // ViewBag.Message = ex.Message; // return View("Error"); // } // return RedirectToAction("Index"); //} // [Audit] public ActionResult Upload(HttpPostedFileBase uploadFile) { var strValidations = new StringBuilder(string.Empty); var maNhaThuoc = this.GetNhaThuoc().MaNhaThuoc; try { if (uploadFile.ContentLength > 0) { string filePath = Path.Combine(HttpContext.Server.MapPath("../Uploads"), Path.GetFileName(uploadFile.FileName)); uploadFile.SaveAs(filePath); int totalupdated = 0; int totaladded = 0; int totalError = 0; string message = "<b>Thông tin nhà cung cấp ở dòng số {0} bị lỗi:</b><br/> {1}"; UploadObjectInfo info = new UploadObjectInfo(); foreach (var worksheet in Workbook.Worksheets(filePath)) { for (int i = 1; i < worksheet.Rows.Count(); i++) { var row = worksheet.Rows[i]; var msg = ValidateDataImport(row); if (!string.IsNullOrEmpty(msg)) { if (msg == Constants.Params.msgOk) { var tennhacungcap = sThuoc.Utils.Helpers.RemoveEncoding(row.Cells[1].Text.Trim()); //var sodienthoai = row.Cells[3] != null ? row.Cells[3].Text.Trim() : string.Empty; if (!String.IsNullOrWhiteSpace(tennhacungcap)) { var nhacungcap = unitOfWork.NhaCungCapRespository.GetMany(x => x.NhaThuoc.MaNhaThuoc == maNhaThuoc && x.TenNhaCungCap.Equals(tennhacungcap, StringComparison.OrdinalIgnoreCase)).FirstOrDefault(); //Check if thuoc already exist if (nhacungcap != null) { BindNhaCungCap(ref nhacungcap, row); totalupdated++; } else { nhacungcap = new NhaCungCap() { MaNhaCungCap = 0, NhaThuoc = unitOfWork.NhaThuocRepository.GetById(maNhaThuoc), CreatedBy = unitOfWork.UserProfileRepository.GetById(WebSecurity.GetCurrentUserId), Created = DateTime.Now }; unitOfWork.NhaCungCapRespository.Insert(BindNhaCungCap(ref nhacungcap, row)); totaladded++; } } } else { info.ErrorMsg.Add(string.Format(message, i, msg)); totalError++; } } } unitOfWork.Save(); info.Title = "Thông tin upload nhà cung cấp"; info.TotalUpdated = totalupdated; info.TotalAdded = totaladded; info.TotalError = totalError; Session["UploadMessage"] = info; return(RedirectToAction("index", "Upload")); } } } catch (Exception ex) { ViewBag.Message = ex.Message; ViewBag.FullMessage = ex.Message; return(View("Error")); } return(RedirectToAction("Index")); }
//public ActionResult Upload2(HttpPostedFileBase uploadFile) //{ // var strValidations = new StringBuilder(string.Empty); // try // { // if (uploadFile.ContentLength > 0) // { // string filePath = Path.Combine(HttpContext.Server.MapPath("../Uploads"), // Path.GetFileName(uploadFile.FileName)); // uploadFile.SaveAs(filePath); // var ds = new DataSet(); // //A 32-bit provider which enables the use of // string connectionString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + filePath + // ";Extended Properties=Excel 12.0;"; // var maNhaThuoc = this.GetNhaThuoc().MaNhaThuoc; // using (var conn = new OleDbConnection(connectionString)) // { // conn.Open(); // using (var dtExcelSchema = conn.GetSchema("Tables")) // { // string sheetName = dtExcelSchema.Rows[0]["TABLE_NAME"].ToString(); // string query = "SELECT * FROM [" + sheetName + "]"; // var adapter = new OleDbDataAdapter(query, conn); // //DataSet ds = new DataSet(); // adapter.Fill(ds, "Items"); // if (ds.Tables.Count > 0) // { // if (ds.Tables[0].Rows.Count > 0) // { // for (int i = 0; i < ds.Tables[0].Rows.Count; i++) // { // var row = ds.Tables[0].Rows[i]; // var TenKhachHang = row[1].ToString().Trim(); // var SoDienThoai = row[3].ToString().Trim(); // if (!String.IsNullOrWhiteSpace(TenKhachHang)) // { // var KhachHang = new KhachHang(); // if (!String.IsNullOrWhiteSpace(SoDienThoai)) // { // KhachHang = unitOfWork.KhachHangRepository.GetMany(x => x.NhaThuoc.MaNhaThuoc == maNhaThuoc && x.TenKhachHang.ToUpper() == TenKhachHang.ToUpper() && x.SoDienThoai.ToUpper() == SoDienThoai.ToUpper()).FirstOrDefault(); // } // else // { // KhachHang = unitOfWork.KhachHangRepository.GetMany(x => x.NhaThuoc.MaNhaThuoc == maNhaThuoc && x.TenKhachHang.ToUpper() == TenKhachHang.ToUpper()).FirstOrDefault(); // } // //Check if thuoc already exist // if (KhachHang != null) // { // BindKhachHang(ref KhachHang, row); // } // else // { // KhachHang = new KhachHang() // { // MaKhachHang = 0, // NhaThuoc = unitOfWork.NhaThuocRepository.GetById(maNhaThuoc), // Created = DateTime.Now, // CreatedBy = unitOfWork.UserProfileRepository.GetById(WebSecurity.GetCurrentUserId) // }; // unitOfWork.KhachHangRepository.Insert(BindKhachHang(ref KhachHang, row)); // } // } // } // unitOfWork.Save(); // } // } // } // } // } // } // catch (Exception ex) // { // ViewBag.Message = ex.Message; // return View("Error"); // } // return RedirectToAction("Index"); //} // [Audit] public ActionResult Upload(HttpPostedFileBase uploadFile) { var strValidations = new StringBuilder(string.Empty); try { if (uploadFile.ContentLength > 0) { string filePath = Path.Combine(HttpContext.Server.MapPath("../Uploads"), Path.GetFileName(uploadFile.FileName)); uploadFile.SaveAs(filePath); int totalupdated = 0; int totaladded = 0; int totalError = 0; string message = "<b>Thông tin khách hàng ở dòng số {0} bị lỗi:</b><br/> {1}"; UploadObjectInfo info = new UploadObjectInfo(); //A 32-bit provider which enables the use of var maNhaThuoc = this.GetNhaThuoc().MaNhaThuoc; foreach (var worksheet in Workbook.Worksheets(filePath)) { for (int i = 1; i < worksheet.Rows.Count(); i++) { var row = worksheet.Rows[i]; var msg = ValidateDataImport(row); if (!string.IsNullOrEmpty(msg)) { if (msg == Constants.Params.msgOk) { var TenKhachHang = row.Cells[1].Text.Trim(); //var SoDienThoai = row.Cells[3] != null ? row.Cells[3].Text.Trim() : string.Empty; var KhachHang = unitOfWork.KhachHangRepository.GetMany(x => x.NhaThuoc.MaNhaThuoc == maNhaThuoc && x.TenKhachHang.ToUpper() == TenKhachHang.ToUpper()).FirstOrDefault(); //Check if thuoc already exist if (KhachHang != null) { BindKhachHang(ref KhachHang, row); totalupdated++; } else { KhachHang = new KhachHang() { MaKhachHang = 0, NhaThuoc = unitOfWork.NhaThuocRepository.GetById(maNhaThuoc), Created = DateTime.Now, CreatedBy = unitOfWork.UserProfileRepository.GetById(WebSecurity.GetCurrentUserId) }; unitOfWork.KhachHangRepository.Insert(BindKhachHang(ref KhachHang, row)); totaladded++; } } else { info.ErrorMsg.Add(string.Format(message, i, msg)); totalError++; } } } unitOfWork.Save(); info.Title = "Thông tin upload khách hàng"; info.TotalUpdated = totalupdated; info.TotalAdded = totaladded; info.TotalError = totalError; Session["UploadMessage"] = info; return(RedirectToAction("index", "Upload")); } } } catch (Exception ex) { ViewBag.Message = ex.Message; ViewBag.FullMessage = ex.Message; return(View("Error")); } return(RedirectToAction("Index")); }