Hi mahesh213,
Refer below code and modify as per your need.
Model
public class Generation
{
public List<Memberdetails> Memberdetails { get; set; }
}
public class Memberdetails
{
public string MemberType { get; set; }
public string MemberCategory { get; set; }
public int TotalSum { get; set; }
public int MemberCount { get; set; }
}
Namespaces
using Newtonsoft.Json;
using System.Collections.Generic;
using System.Data;
using System.Data.OleDb;
using System.IO;
Controller
public class HomeController : Controller
{
// GET: Home
public ActionResult Index()
{
Generation model = new Generation();
model.Memberdetails = new List<Memberdetails>();
return View(model);
}
public ActionResult Upload()
{
Generation generation = new Generation();
DataSet ds = new DataSet();
if (Request.Files.Count > 0)
{
HttpPostedFileBase postedFile = Request.Files[0];
string fileName = Path.GetFileName(postedFile.FileName);
string path = Server.MapPath("~/Uploads/");
if (!Directory.Exists(path))
{
Directory.CreateDirectory(path);
}
postedFile.SaveAs(path + fileName);
string filePath = string.Empty;
filePath = path + Path.GetFileName(postedFile.FileName);
string extension = Path.GetExtension(postedFile.FileName);
postedFile.SaveAs(filePath);
string conString = string.Empty;
switch (extension)
{
case ".xls": //Excel 97-03.
conString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source={0};Extended Properties='Excel 8.0;HDR=YES'";
break;
case ".xlsx": //Excel 07 and above.
conString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source={0};Extended Properties='Excel 8.0;HDR=YES'";
break;
}
conString = string.Format(conString, filePath);
using (OleDbConnection connExcel = new OleDbConnection(conString))
{
using (OleDbCommand cmdExcel = new OleDbCommand())
{
using (OleDbDataAdapter odaExcel = new OleDbDataAdapter())
{
cmdExcel.Connection = connExcel;
//Get the name of First Sheet.
connExcel.Open();
DataTable dtExcelSchema;
dtExcelSchema = connExcel.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, null);
string sheetName = dtExcelSchema.Rows[0]["TABLE_NAME"].ToString();
connExcel.Close();
//Read Data from First Sheet.
connExcel.Open();
cmdExcel.CommandText = "SELECT * From [" + sheetName + "]";
odaExcel.SelectCommand = cmdExcel;
odaExcel.Fill(ds);
connExcel.Close();
}
}
}
List<Memberdetails> details = new List<Memberdetails>();
int i = 1;
foreach (DataRow dr in ds.Tables[0].Rows)
{
Memberdetails detail = new Memberdetails();
if (dr["MemberType"] == DBNull.Value)
{
return Json(JsonConvert.SerializeObject(new { Error = "Row" + i + " member type value is blank" }, Formatting.Indented), JsonRequestBehavior.AllowGet);
}
else
{
detail.MemberCategory = dr["MemberCategory"].ToString();
detail.MemberType = dr["MemberType"].ToString();
detail.TotalSum = dr["TotalSum"] == DBNull.Value ? 0 : Convert.ToInt32(dr["TotalSum"]);
detail.MemberCount = dr["MemberCount"] == DBNull.Value ? 0 : Convert.ToInt32(dr["MemberCount"]);
details.Add(detail);
}
i++;
}
generation.Memberdetails = details;
}
return Json(JsonConvert.SerializeObject(generation, Formatting.Indented), JsonRequestBehavior.AllowGet);
}
}
View
@model _151188_Validation_Empty_Excel_Cell_MVC.Models.Generation
@{
Layout = null;
}
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width" />
<title>Index</title>
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>
<script type="text/javascript">
$(function () {
$('#fuUpload').on('change', function () {
var formData = new FormData();
var files = $(this).get(0).files[0];
formData.append(files.name, files);
$.ajax({
url: "/Home/Upload",
type: 'POST',
cache: false,
contentType: false,
processData: false,
data: formData,
success: function (response) {
if (JSON.parse(response).Error != '') {
alert(JSON.parse(response).Error);
} else {
var dataSet = JSON.parse(response).Memberdetails;
$('#tblDetails tbody').empty();
var rows = '';
$.each(dataSet, function () {
rows += "<tr>";
rows += "<td>" + $(this)[0].MemberType + "</td>";
rows += "<td>" + $(this)[0].MemberCategory + "</td>";
rows += "<td>" + $(this)[0].TotalSum + "</td>";
rows += "<td>" + $(this)[0].MemberCount + "</td></tr>";;
});
$('#tblDetails tbody').append(rows);
}
}
});
});
});
</script>
</head>
<body>
Select File:<input type="file" name="postedFile" id="fuUpload" />
<hr />
<table id="tblDetails">
<thead>
<tr>
<th>MemberType</th>
<th>MemberCategory</th>
<th>TotalSum</th>
<th>MemberCount</th>
</tr>
</thead>
<tbody>
@foreach (var item in Model.Memberdetails)
{
<tr>
<td>@item.MemberType</td>
<td>@item.MemberCategory</td>
<td>@item.TotalSum</td>
<td>@item.MemberCount</td>
</tr>
}
</tbody>
</table>
</body>
</html>
Screenshot