public ActionResult Download(Int16 SchemeId = 0) { string UserId = User.Identity.GetUserId(); var emp = emsMgr.GetBulkUploadEmployeeDetail(UserId, SchemeId); var da = admin.GetExcelColoumData(SchemeId, UserId); Response.Clear(); Response.Buffer = true; Response.AddHeader("content-disposition", "attachment;filename=Leave.csv"); Response.Charset = ""; Response.ContentType = "application/text"; StringBuilder sb = new StringBuilder(); //dt.Columns.Add(new DataColumn("EmpId", Type.GetType("System.String"))); sb.Append("UserName" + ','); sb.Append("EmpId" + ','); sb.Append("EmpName" + ','); sb.Append("Designation" + ','); sb.Append("Department" + ','); foreach (var item in da) { sb.Append(item.EngagementType + ','); } sb.Length--; //append new line sb.Append("\r\n"); foreach (var itemRow in emp) { //append new line sb.Append(itemRow.UserName + ','); sb.Append(itemRow.EmployeeId + ','); sb.Append(itemRow.Name + ','); sb.Append(itemRow.DesignationName + ','); sb.Append(itemRow.Department + ','); foreach (var itemDyCol in da) { sb.Append("" + ','); } sb.Length--; sb.Append("\r\n"); } Response.Output.Write(sb.ToString()); Response.Flush(); Response.End(); return(View()); }