public ActionResult Export()
{
StringBuilder sb = new StringBuilder();
//static file name, can be changes as per requirement
string sFileName = "filename.xls";
//Bind data list from edmx
var Data = objEntities.citymaster.Take(10).ToList();
if (Data != null && Data.Any())
{
sb.Append("<table style='1px solid black; font-size:12px;'>");
sb.Append("<tr>");
sb.Append("<td style='width:120px;'><b>City Name</b></td>");
sb.Append("<td style='width:300px;'><b>City Code</b></td>");
sb.Append("</tr>");
foreach (var result in Data)
{
sb.Append("<tr>");
sb.Append("<td>" + result.sCityName + "</td>");
sb.Append("<td>" + result.sCityCode + "</td>");
sb.Append("</tr>");
}
}
HttpContext.Response.AddHeader("content-disposition", "attachment; filename=" + sFileName);
this.Response.ContentType = "application/vnd.ms-excel";
byte[] buffer = System.Text.Encoding.UTF8.GetBytes(sb.ToString());
return File(buffer, "application/vnd.ms-excel");
}