Click here to Skip to main content
16,021,621 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi all,
I am using the below code to export data from Gridview to Excel. But the problem I am getting is that my data in excel is coming as an image of Gridview and not in excel format. Can anyone suggest me the reason?

C#
protected void btnexporttoxl_Click(object sender, EventArgs e)
{       
        Response.Clear();
        Response.AddHeader("content-disposition", "attachment;filename=FileName.xls");
        Response.Charset = "";
        // If you want the option to open the Excel file without saving than
        // comment out the line below
        Response.Cache.SetCacheability(HttpCacheability.NoCache);
        Response.ContentType = "application/vnd.xls";
        System.IO.StringWriter stringWrite = new System.IO.StringWriter();
        System.Web.UI.HtmlTextWriter htmlWrite = new HtmlTextWriter(stringWrite);        
        gd1.RenderControl(htmlWrite);
        Response.Write(stringWrite.ToString());
        Response.End();
}
public override void VerifyRenderingInServerForm(Control control)
{
        // Confirms that an HtmlForm control is rendered for the specified ASP.NET server control at run time.
        return;
}
Posted
Updated 3-Jun-10 23:44pm
v4

1 solution

In Override() method,
Please Comment 'Return'
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900