C# MVC Action To Export Data To CSV/EXCEL Document

 var gv = new GridView();
 StringWriter objStringWriter = new StringWriter();
  HtmlTextWriter objHtmlTextWriter;
 gv.DataSource = Dt;  (Set DataTable or List Data)
                    gv.DataBind();
                    Response.ClearContent();
                    Response.Buffer = true;
                    Response.AddHeader("content-disposition", "attachment; filename=schoollist.xls");
                    Response.ContentType = "application/ms-excel";
                    Response.Charset = "";
                    objStringWriter = new StringWriter();
                    objHtmlTextWriter = new HtmlTextWriter(objStringWriter);
                    gv.RenderControl(objHtmlTextWriter);
                    Response.Output.Write(objStringWriter.ToString());
                    Response.Flush();
                    Response.End();

                    return View();

Comments

Popular posts from this blog