8
Answers

Data export is not working in asp .net MVC

Ankit Kumar

Ankit Kumar

7y
268
1
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)
0
Reena Sajith

Reena Sajith

NA 88 0 18y
Hi, why r u assigning the string line with first and second line at a time /Read the 1st line of recycle.txt string line = sr.ReadLine(); //Read the 2nd line of recycle.txt line = sr.ReadLine(); //Read the 1st line of objectRef.txt string line2 = sr2.ReadLine(); //Read the 2nd line of objectRef.txt line2 = sr2.ReadLine(); here line,line2 will have the second lines from both the textfiles what is the logic