public ActionResult ImportFile(string tableName) { BaseResponse <Tuple <DataTable, int> > result = new BaseResponse <Tuple <DataTable, int> >(); DataTable excelAsTable = new DataTable(); try { string extenstion = Request.Form.Files[0].Name.Split(".").LastOrDefault(); Stream file = Request.Form.Files[0].OpenReadStream(); using (Stream stream = new MemoryStream()) { IExcelDataReader reader = null; //Configuration is set to true for use excel header as datatable header and not as an value of row. var conf = new ExcelDataSetConfiguration { ConfigureDataTable = _ => new ExcelDataTableConfiguration { UseHeaderRow = true } }; //Must check file extension to adjust the reader to the excel file type if (extenstion == "xls") { reader = ExcelReaderFactory.CreateBinaryReader(file); } else if (extenstion == "xlsx") { reader = ExcelReaderFactory.CreateOpenXmlReader(file); } else if (extenstion == "csv") { reader = ExcelReaderFactory.CreateCsvReader(file); } if (reader != null) { //Fill DataSet DataSet content = reader.AsDataSet(conf); excelAsTable = content.Tables[0]; result = _iHomeServices.InsertFile("*****@*****.**", tableName, excelAsTable); } } } catch (Exception ex) { result.Status = false; result.Message = ex.Message; result.Exception = ex; } if (result.Status) { return(PartialView("_TableData", result.Data)); } else { return(Json(new { Status = false, message = result.Message })); } }