public ActionResult ViewImports() { using (var db = new ReportDataContext()) { var viewModel = new ViewImportData(); var reportdata = db.Report.ToList(); var masterGrid = reportdata.GroupBy(x => x.AccountNumber).Select(y => y.First()).ToList(); viewModel.MasterGrid = masterGrid; return(PartialView("~/Views/Partials/ViewImports.cshtml", viewModel)); } }
public ReturnObject ImportAsset() { ReturnObject obj = new ReturnObject(); obj.status = -1; obj.value = 0; ViewImportData entity = new ViewImportData(); IFormFile formFiles = Request.Form.Files.First(); entity.FileImport = formFiles; listWarning = new List <string>(); listError = new List <string>(); if (entity.FileImport != null) { IFormFile file = entity.FileImport; if (file.Length > 0) { entity.fileName = file.FileName; string sFileExtension = Path.GetExtension(file.FileName).ToLower(); ISheet sheet; using (var stream = new FileStream(file.FileName, FileMode.Create)) { int countUpdate = 0; int countInsert = 0; int countError = 0; file.CopyTo(stream); stream.Position = 0; XSSFWorkbook hssfwb = new XSSFWorkbook(stream); XSSFWorkbook hssfwbCheck = hssfwb; if (hssfwb.NumberOfSheets > 0) { sheet = hssfwb.GetSheetAt(0); var header = sheet.GetRow(5); dict.Clear(); if (header != null) { Dictionary <string, int> dictHeaders = new Dictionary <string, int>(); var lstLines = new List <string>(); for (int j = 0; j < header.Cells.Count; j++) { string key = header.Cells[j].StringCellValue.Trim().ToUpper(); if (dictMap.ContainsKey(key)) { dict.Add(dictMap[key], j); } } } //Lấy dữ liệu từ dòng thứ 1 for (int rowIndex = 6; rowIndex < sheet.PhysicalNumberOfRows; rowIndex++) { // Lấy row hiện tại var nowRow = sheet.GetRow(rowIndex); if (nowRow.GetCell(1) != null && (nowRow.GetCell(1).CellType.ToString() != "Blank")) { countInsert++; //Kiểm tra lỗi KiemTraLoi(ref nowRow); countError = listError.Count; //Nếu không có lỗi mới thực hiện tiếp if (countError <= 0) { var now = DateTime.Now.ToString("dd-MMM-yyyy"); Asset asset = new Asset(); asset.AssetCode = GetString(nowRow, "AssetCode").TrimEnd(); asset.AssetName = GetString(nowRow, "AssetName").TrimEnd(); asset.Description = GetString(nowRow, "Description").TrimEnd(); asset.Unit = GetString(nowRow, "Unit").TrimEnd(); asset.Price = GetInt(nowRow, "Price"); asset.Quantity = GetInt(nowRow, "Quantity"); asset.LocationID = GetString(nowRow, "LocationID").TrimEnd(); asset.CategoryID = GetString(nowRow, "CategoryID").TrimEnd(); entity.lsAsset.Add(asset); } } else { listError.Add("File không có dữ liệu hoặc dữ liệu không đúng định dạng"); break; } } if (listError.Count == 0) { DaoAsset daoAsset = new DaoAsset(tWG_ACHECKContext); int count = daoAsset.ImportData(entity.lsAsset); if (count > 0) { obj.value = count; obj.status = 1; } } } System.IO.File.Delete(stream.Name); entity.countError = countError; entity.countUpdate = countUpdate; entity.countInsert = countInsert; entity.listError = listError; entity.listWarning = listWarning; obj.message = JsonConvert.SerializeObject(listError).ToString(); } } } return(obj); }