public ActionResult ImportFromXls(ImportFileViewModel model) { try { if (model.ImportFile != null) { var file = model.ImportFile; if (file != null && file.ContentLength > 0) { var fileBytes = new byte[file.ContentLength]; file.InputStream.Read(fileBytes, 0, file.ContentLength); //do stuff with the bytes string fileName = file.FileName; string filePath = Path.Combine(Request.PhysicalApplicationPath, "Files\\", fileName); System.IO.File.WriteAllBytes(filePath, fileBytes); //File Uploaded HSSFWorkbook hssfWorkbook; string filefullpath = filePath; //StreamReader streamReader = new StreamReader(model.ImportFile.InputStream); using (FileStream fileStream = new FileStream(filefullpath, FileMode.Open, FileAccess.Read)) { hssfWorkbook = new HSSFWorkbook(fileStream); //hssfWorkbook = new HSSFWorkbook(); } var products = new List<ProductXlsModel>(); //the columns var properties = new string[] { "ProductId", "ProductName", "ProductPrice", "CategoryId", "CategoryName" }; ISheet sheet = hssfWorkbook.GetSheet("Products"); for (int row = 1; row <= sheet.LastRowNum; row++) { if (sheet.GetRow(row) != null) //null is when the row only contains empty cells { int productId = Convert.ToInt32(sheet.GetRow(row).GetCell(GetColumnIndex(properties, "ProductId")).NumericCellValue); string productName = sheet.GetRow(row).GetCell(GetColumnIndex(properties, "ProductName")).StringCellValue; decimal productPrice = Convert.ToDecimal(sheet.GetRow(row).GetCell(GetColumnIndex(properties, "ProductPrice")).StringCellValue); int categoryId = Convert.ToInt32(sheet.GetRow(row).GetCell(GetColumnIndex(properties, "CategoryId")).NumericCellValue); string categoryName = sheet.GetRow(row).GetCell(GetColumnIndex(properties, "CategoryName")).StringCellValue; var product = new ProductXlsModel { ProductId = productId, ProductName = productName, ProductPrice = productPrice, CategoryId = categoryId, CategoryName = categoryName }; products.Add(product); } } if (System.IO.File.Exists(filefullpath)) { System.IO.File.Delete(filefullpath); } return RedirectToAction("DataImport", "Product"); //return Content(Boolean.TrueString); //return Json(new { msg = "Product data uploaded successfully.", status = MessageType.success.ToString() }, JsonRequestBehavior.AllowGet); } else { //return Content("Sorry! Could not found this file."); return RedirectToAction("DataImport", "Product"); } } else { //Upload file Null Message return RedirectToAction("DataImport", "Product"); //return Content("Upload file could not found."); //return Json(new { msg = "Upload file could not found.", status = MessageType.success.ToString() }, JsonRequestBehavior.AllowGet); } } catch (Exception ex) { return RedirectToAction("DataImport", "Product"); //return Content("Oop! Error."); //return Json(new { msg = ExceptionHelper.ExceptionMessageFormat(ex, log: false), status = MessageType.error.ToString() }, JsonRequestBehavior.AllowGet); } }
public ActionResult ImportFromCsv(ImportFileViewModel model) { try { if (model.ImportFile != null) { var file = model.ImportFile; if (file != null && file.ContentLength > 0) { var fileBytes = new byte[file.ContentLength]; file.InputStream.Read(fileBytes, 0, file.ContentLength); //do stuff with the bytes string fileName = file.FileName; string filePath = Path.Combine(Request.PhysicalApplicationPath, "Files\\", fileName); System.IO.File.WriteAllBytes(filePath, fileBytes); //File Uploaded //using (var reader = new StreamReader(ImportCsv.InputStream)) //using (var reader = new StreamReader(model.ImportFile.InputStream)) using (var reader = new StreamReader(filePath)) using (var csvReader = new CsvReader(reader)) { csvReader.Read(); //Read CSV File var products = csvReader.GetRecords<ProductCsvModel>().ToList(); } if (System.IO.File.Exists(filePath)) { System.IO.File.Delete(filePath); } return RedirectToAction("DataImport", "Product"); //return Content(Boolean.TrueString); //return Json(new { msg = "Product data uploaded successfully.", status = MessageType.success.ToString() }, JsonRequestBehavior.AllowGet); } else { //return Content("Sorry! Could not found this file."); return RedirectToAction("DataImport", "Product"); } } else { //Upload file Null Message return RedirectToAction("DataImport", "Product"); //return Content("Upload file could not found."); //return Json(new { msg = "Upload file could not found.", status = MessageType.success.ToString() }, JsonRequestBehavior.AllowGet); } } catch (Exception ex) { return RedirectToAction("DataImport", "Product"); //return Content("Oop! Error."); //return Json(new { msg = ExceptionHelper.ExceptionMessageFormat(ex, log: false), status = MessageType.error.ToString() }, JsonRequestBehavior.AllowGet); } }