Top Ad unit 728 × 90

random

Export gridview content from code behind in excel using .net

 how Export grid view content from code behind in excel using .net 

There is some time we need in a project's reporting module to export grid view data to execel.by understanding that requirement i decided to write this article especially focusing on beginners and those who want to learn Export grid view content from code behind in excel using asp.net.
  

Now let us create a function to export zip data file in asp.net. Please refer to my following article.

  

For more details about handler you can take reference of MSDN.


Go for Client side Multiple parameter refer my post:
Client side ajax post using multiple parameter



Below is the Code for export to excel :

public static void ExportToExcel(GridView gridObject, string excelSheetName)
        {
            try
            {
                var style = @" ";
                var attachment = "attachment; filename=" + excelSheetName + ".xls";
                HttpContext.Current.Response.ClearContent();
                HttpContext.Current.Response.ClearContent();
                HttpContext.Current.Response.AddHeader("content-disposition", attachment);
                HttpContext.Current.Response.ContentType = "application/ms-excel";
                var sw = new StringWriter();
                var htw = new HtmlTextWriter(sw);

                // Create a form to contain the grid
                var frm = new HtmlForm();
                gridObject.Parent.Controls.Add(frm);
                frm.Attributes["runat"] = "server";
                frm.Controls.Add(gridObject);
                frm.RenderControl(htw);

                HttpContext.Current.Response.Write(style);
                HttpContext.Current.Response.Write("
" + excelSheetName + "
");
                HttpContext.Current.Response.Write(sw.ToString());
                HttpContext.Current.Response.End();
            }
            catch (Exception)
            {
            }
        }



















Export gridview content from code behind in excel using .net Reviewed by Vikas Kumar Singh on December 11, 2017 Rating: 5

1 comment:

All Rights Reserved by DotNetBasic.com © 2018

Contact Form

Name

Email *

Message *

© 2018 All rights reserved by DotNetBasic. Powered by Blogger.