Ankit Kumar

Ankit Kumar

  • NA
  • 159
  • 48.3k

Data export is not working in asp .net MVC

Aug 21 2017 6:02 AM
Dear Friends
 
i am trying to export list data to excel but neither i am getting any error nor my my task is getting accomplished, no excel file is being downloaded.
someone please help.
 
code is given below
 
public ActionResult lnkExportData()
{
EmployeeHome EmpHome = new EmployeeHome();
List<EmployeeHome> objEmployeeList = new List<EmployeeHome>();
EmpHome.BindEmployee(1, "", "", "", "");
var list = EmpHome.ListLanguageEmployee;
var Records = list.Select(x => new { UserName = x.UserName, Name = x.ImageFilePath + ' ' + x.LastName + '-' + '(' + x.EmpCode + ')', Designation = x.Designation, CellularNumber = x.CellPhone, EmailID = x.EmailId, JoiningDate = String.Format("{0:dd-MMM-yyyy}", x.JoiningDate), Status = x.Status == 1 ? "Active" : "Inactive" }).ToList();
var gv = new GridView();
gv.DataSource = Records;
gv.DataBind();
Response.ClearContent();
Response.Buffer = true;
Response.AddHeader("content-disposition", "attachment; filename=DemoExcel.xls");
Response.ContentType = "application/ms-excel";
Response.Charset = "";
StringWriter objStringWriter = new StringWriter();
HtmlTextWriter objHtmlTextWriter = new HtmlTextWriter(objStringWriter);
gv.RenderControl(objHtmlTextWriter);
Response.Output.Write(objStringWriter.ToString());
Response.Flush();
Response.End();
return View();
}
 

Answers (8)