Export GridView To Excel

Today it is common trying to export from Gridview in ASP.NET to excel right. Please find below the piece of code that can help you export Gridview details to excel.

C#

protected
void Button1_Click(object sender, EventArgs e)

    {

        Response.AddHeader(“content-disposition”, “attachment;filename=FileName.xls”);

        Response.Charset = String.Empty;

        Response.ContentType = “application/vnd.xls”;

        System.IO.StringWriter sw = new System.IO.StringWriter();

        System.Web.UI.HtmlTextWriter hw = new
HtmlTextWriter(sw);

        GridView1.RenderControl(hw);

        Response.Write(sw.ToString());

        Response.End();

    }

 
 

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: