3

I am using bellow code for export gridview data to excel but problem is that whole page export to excel. I want only gridview data not whole page export. How can solve this problem?

HtmlForm form = new HtmlForm();
Response.Clear();
Response.Buffer = true;
string filename = "GridViewExport_" + DateTime.Now.ToString() + ".xls";

Response.AddHeader("content-disposition", "attachment;filename=" + filename);
Response.Charset = "";
Response.ContentType = "application/vnd.ms-excel";
StringWriter sw = new StringWriter();
HtmlTextWriter hw = new HtmlTextWriter(sw);
gdvInBox.AllowPaging = false;
gdvInBox.DataBind();
form.Controls.Add(gdvInBox);
this.Controls.Add(form);
form.RenderControl(hw);

//style to format numbers to string
string style = @"<style> .textmode { mso-number-format:\@; } </style>";
Response.Write(style);
Response.Output.Write(sw.ToString());
Response.Flush();
Response.End();

Thanks in Advance.

Mo Patel
  • 2,321
  • 4
  • 22
  • 37
Letoncse
  • 702
  • 4
  • 15
  • 36

2 Answers2

7

It's pretty easy to set up the excel export that will export only the gridview. This has been tested and will only export the gridview that appears on your given web page.

For your C# code use the following:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class vxcel_export : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
    }

    protected void Button2_Click(object sender, EventArgs e)
    {
        Response.Clear();
        Response.AddHeader("content-disposition", "attachment;filename=file-name.xls");
        Response.ContentType = "application/vnd.xlsx";
        System.IO.StringWriter stringWrite = new System.IO.StringWriter();
        System.Web.UI.HtmlTextWriter htmlWrite = new HtmlTextWriter(stringWrite);
        GridView1.RenderControl(htmlWrite);
        Response.Write(stringWrite.ToString());
        Response.End();
    }    

    public override void VerifyRenderingInServerForm(Control control)
    {
    }
}

In your aspx code use the following:

Make sure you add EnableEventValidation="false" to the <%@Page %> code at the top of the page.

Place the following code where you want to put the button to export your gridview to Excel:

<asp:Button ID="Button2" runat="server" OnClick="Button2_Click" Text="Whatever you want your button to say" />

You can modify the Height and Width to whatever size you want in the button.

That's it. One thing to keep in mind is that when you export the file it's not a true excel file until you save it as a workbook/Excel File.

Mo Patel
  • 2,321
  • 4
  • 22
  • 37
Techie Joe
  • 847
  • 2
  • 14
  • 32
  • How would you make this same thing work from a user control? You can't override the VerifyRendering – BRogers Nov 17 '14 at 23:57
0

I had to also apply the following code to my page to get this to work.

public override void VerifyRenderingInServerForm(Control control)
{
  /* Confirms that an HtmlForm control is rendered for the specified ASP.NET
     server control at run time. */
}

I found the solution here --> GridView must be placed inside a form tag with runat="server" even after the GridView is within a form tag

Community
  • 1
  • 1
TheLegendaryCopyCoder
  • 1,658
  • 3
  • 25
  • 46