public IActionResult CreateHST(Rules r) { _ctx.Rules.Add(r); _ctx.SaveChanges(); return(RedirectToAction("Index")); }
public IActionResult UpLoadExcel(IFormFile fExcel, CancellationToken cancellationToken) { ResetDatabase(); Excels fileToImport = new Excels { ExcelUploadedDate = DateTime.Now }; if (fExcel != null && CheckGetExtentionsFileIsSupported(fExcel)) { ViewBag.Error = null; string path = Path.Combine(Directory.GetCurrentDirectory(), "wwwroot", "Excels", fExcel.FileName); using (var file = new FileStream(path, FileMode.Create)) { fExcel.CopyTo(file); } fileToImport.ExcelFileName = fExcel.FileName; _ctx.Add(fileToImport); _ctx.SaveChanges(); /// Save excel file history // import data using (var stream = new MemoryStream()) { fExcel.CopyTo(stream); using (ExcelPackage package = new ExcelPackage(stream)) { ExcelWorksheet workSheet = package.Workbook.Worksheets["DynamicReport"]; if (workSheet != null) { // List to ADD database List <RawData> rawDatas = new List <RawData>(); int totalRows = workSheet.Dimension.Rows; for (int i = 6; i < totalRows; i++) { var dataString = workSheet.Cells[i, 46].Value; string dateString = dataString != null?dataString.ToString().Split('-')[0] + "/" + dataString.ToString().Split('-')[1] + "/" + dataString.ToString().Split('-')[2] : DateTime.MaxValue.ToString("dd/MM/yyyy"); rawDatas.Add(new RawData { OrderNo = workSheet.Cells[i, 1].Value.ToString(), JobNo = workSheet.Cells[i, 41].Value.ToString(), DeliveryCustCode = workSheet.Cells[i, 14].Value.ToString(), DeliveryAddress = workSheet.Cells[i, 17].Value != null ? workSheet.Cells[i, 17].Value.ToString() : String.Empty, ServiceLevel = workSheet.Cells[i, 24].Value != null ? workSheet.Cells[i, 24].Value.ToString() : "servicesLevel-unknown-" + i, TruckId = workSheet.Cells[i, 40].Value != null ? workSheet.Cells[i, 40].Value.ToString() : "trucks-unknown-" + i, TruckType = workSheet.Cells[i, 43].Value != null ? workSheet.Cells[i, 43].Value.ToString() : String.Empty, TransportAgent = workSheet.Cells[i, 45].Value != null ? workSheet.Cells[i, 45].Value.ToString() : String.Empty, AtdcompleteDate = DateTime.ParseExact(dateString, "dd/MM/yyyy", CultureInfo.InvariantCulture), DriverName = workSheet.Cells[i, 50].Value != null ? workSheet.Cells[i, 50].Value.ToString() : String.Empty, DriverPhone = workSheet.Cells[i, 51].Value != null ? workSheet.Cells[i, 51].Value.ToString() : String.Empty, }); } _ctx.RawData.AddRange(rawDatas); _ctx.SaveChanges(); // Insert data for per table ImportDataToPerTable(); ViewBag.ImportSuccess = "Import dữ liệu thành công !"; return(View("Index", _ctx.Excels.ToList())); } else { ViewBag.Error = "Không tìm thấy sheet cần thiết của hệ thống để import dữ liệu! Vui lòng kiểm tra tên của Sheet theo yêu cầu của hệ thống !"; return(View("Index", _ctx.Excels.ToList())); } } } } else { ViewBag.Error = "Vui lòng chọn file excel hoặc định dạng file của bạn không được hỗ trợ. Lưu ý những file được hỗ trợ bao gồm : .xlsx, .csv "; return(View("Index", _ctx.Excels.ToList())); } }