Geeks With Blogs
AzamSharp Some day I will know everything. I hope that day never comes.
Several days back I wrote an article Exporting Datagrid to Excel, Word and Text Files. I tried the same technique for the GridView control and was smashed with a silly error saying that GridView control must be placed inside the Form tag with runat server = true. I switched to html view and found that GridView is already inside the form tag with runat = server. I googled it and found that many people are facing this problem and no one has found a solution yet. Microsoft has classified it as a "BUG". Here is a link to microsoft website about this BUG: GridView Export to Excel Bug I just got a direct link that talks about this BUG. You need to override Page.VerifyRenderingInServerForm Method to get it to work. Here is the msdn link which defines this method: GridView Excel Rendering Problem Posted on Monday, July 25, 2005 2:17 PM | Back to top


Comments on this post: GridView Export to Excel Problems

# re: GridView Export to Excel Problems
Requesting Gravatar...
Hi,

Could you post the complete code on you website or on send me an email with the correct code ?
I have a gridview control on one of my pages and would like to export the data to an excel file.

Thanks for your help !
Bart
Left by Bart on Oct 13, 2005 7:22 AM

# re: GridView Export to Excel Problems
Requesting Gravatar...
When I use your code for Exporting datagrid to Excel, VWD gives me the message that all, for example Response.Charset = ""; is not declared !!
Left by Bart on Oct 13, 2005 7:24 AM

# re: GridView Export to Excel Problems
Requesting Gravatar...
Public Overrides Sub VerifyRenderingInServerForm(ByVal control As Control)
'###this removes the no forms error by overriding the error
End Sub
Left by Zenith on Mar 14, 2006 7:05 AM

# re: GridView Export to Excel Problems
Requesting Gravatar...
Hi,
Could you please provide me the code sample or please send me the code to this email id (madhan_seran@yahoo.com)

Thanks,
Madhan
Left by Madhan on May 25, 2006 3:12 AM

# re: GridView Export to Excel Problems
Requesting Gravatar...
I tried and got the solution for my problem. thanks for your information.

- Madhan
Left by Madhan on May 25, 2006 5:42 AM

# re: GridView Export to Excel Problems
Requesting Gravatar...
would you like to tell us how you fixed this?
Left by doug` on Jun 19, 2006 6:23 AM

# re: GridView Export to Excel Problems
Requesting Gravatar...
Why dont you update your article with this?
Left by gridview guru on Aug 03, 2006 2:21 AM

# re: GridView Export to Excel Problems
Requesting Gravatar...
The article already talks about this issue. I have also created a video which demonstrates the same concept.
Left by AzamSharp on Aug 03, 2006 6:13 AM

# re: GridView Export to Excel Problems
Requesting Gravatar...
How do you override this method? I'm not sure what exactly you mean by this
Left by Amanda on Aug 11, 2006 8:35 AM

# re: GridView Export to Excel Problems
Requesting Gravatar...
Check out the following link:

http://geekswithblogs.net/azamsharp/archive/2005/12/21/63843.aspx

You can also override the Render method like shown below:

Protected Overrides Sub Render(ByVal writer As System.Web.UI.HtmlTextWriter)
If Not (Request.QueryString("ViewAsExcel") Is Nothing) Then
Me.DataBind()

Response.Clear()
Response.AddHeader("content-disposition", "attachment;filename=CompletionData.xls")
Response.ContentType = "application/vnd.ms-excel"
Response.Charset = ""

Dim textWriter As New IO.StringWriter()
Dim htmlWriter As New System.Web.UI.HtmlTextWriter(textWriter)

grdTable.AllowPaging = False
grdTable.AllowSorting = False
grdTable.RenderControl(htmlWriter)
Response.Write(textWriter.ToString())
Response.End()
Else
MyBase.Render(writer)
End If
End Sub
Left by AzamSharp on Aug 11, 2006 8:40 AM

# re: GridView Export to Excel Problems
Requesting Gravatar...
I am trying to the same in aspnet2.0 i am getting run time unknown error after it hits response.end

Does any one has a vb version of this code, i tried converting did not work

let me know

Neel
Left by Neel on Aug 25, 2006 7:01 AM

# re: GridView Export to Excel Problems
Requesting Gravatar...
Great solution, thanks for your help.
Left by Kenneth on Jan 26, 2007 9:05 AM

# re: GridView Export to Excel Problems
Requesting Gravatar...
Here is the c#... just add this to your code behind:

public override void VerifyRenderingInServerForm(Control aControl) {
//###this removes the no forms error by overriding the error
}
Left by Allen on Apr 09, 2007 8:18 PM

# re: GridView Export to Excel Problems
Requesting Gravatar...
Thanks..that was a quick workaround...Worked for me!!!
Left by Raghav on Jul 17, 2007 1:38 AM

# re: GridView Export to Excel Problems
Requesting Gravatar...
Hey I came across the same problems but the reference links you gave are dead.

Fixed it though here it is for anyone who is curious in VB.NET

'**********************************************
Protected Sub btnExportExcel_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnExportExcel.Click
'export to excel
Try
Response.Clear()
Response.AddHeader("content-disposition", "attachment;filename=CompletionData.xls")
Response.ContentType = "application/vnd.ms-excel"
Response.Charset = ""

Dim textWriter As New IO.StringWriter()
Dim htmlWriter As New System.Web.UI.HtmlTextWriter(textWriter)

gvResults.AllowPaging = False
gvResults.AllowSorting = False

Dim control As Control
control = gvResults
Me.VerifyRenderingInServerForm(control)

gvResults.RenderControl(htmlWriter)
Response.Write(textWriter.ToString())
Response.End()
Catch ex As Exception

End Try
End Sub
Public Overrides Sub VerifyRenderingInServerForm(ByVal control As Control)

End Sub
Protected Overrides Sub Render(ByVal writer As HtmlTextWriter)
Try
' Ensure that the control is nested in a server form.
If Not (Page Is Nothing) Then
Page.VerifyRenderingInServerForm(Me)
End If

MyBase.Render(writer)
Catch ex As Exception

End Try
End Sub
'**********************************************
Left by Mynoduesp on Jul 27, 2007 7:37 AM

# re: GridView Export to Excel Problems
Requesting Gravatar...
use try catch block inside the button click event. because when we render the htmltextwriter object with gridview it throws exception httpexception.

c# user can try the code like below :

protected void btnExcel_Click(object sender, EventArgs e)
{
try
{
Response.ClearContent();

Response.AddHeader("content-disposition", "attachment; filename=ChangeControl.xls");

Response.Charset = "";

Response.Cache.SetCacheability(HttpCacheability.NoCache);

Response.ContentType = "application/excel";

StringWriter sw = new StringWriter();

HtmlTextWriter htw = new HtmlTextWriter(sw);

GridView1.GridLines = GridLines.None;

GridView1.RenderControl(htw);

Response.Write(sw.ToString());

Response.End();
}
catch (HttpException ex)
{
string strMessage = ex.Message;
}
}
Left by Lokesh on Oct 30, 2007 12:40 AM

# re: GridView Export to Excel Problems
Requesting Gravatar...
use try catc block inside the button click method like below :

protected void btnExcel_Click(object sender, EventArgs e)
{
try
{
Response.ClearContent();

Response.AddHeader("content-disposition", "attachment; filename=ChangeControl.xls");

Response.Charset = "";

Response.Cache.SetCacheability(HttpCacheability.NoCache);

Response.ContentType = "application/excel";

StringWriter sw = new StringWriter();

HtmlTextWriter htw = new HtmlTextWriter(sw);

GridView1.GridLines = GridLines.None;

GridView1.RenderControl(htw);

Response.Write(sw.ToString());

Response.End();
}
catch (HttpException ex)
{
string strMessage = ex.Message;
}
}
Left by Lokesh on Oct 30, 2007 12:42 AM

# re: GridView Export to Excel Problems
Requesting Gravatar...
Hi, i have managed to get the export to excel button working correctly. I also have an extra css file that allows formatting of the data when it is displayed in the excel page. However, I was wondering if it was possible to include page setting (e.g. page set to horizontal). If so, does anyone know where i could find sytax to help me code this into my application. Thank you in advance

mike
Left by Mike Daly on Jan 10, 2008 3:48 AM

# re: GridView Export to Excel Problems
Requesting Gravatar...
I was getting the same error. Later I found a slightly different code that worked fine. The only difference was that the GridView was put inside a form created in code.

Like this:

HtmlForm frm = new HtmlForm();

Controls.Add(frm);
frm.Controls.Add(gridview);
frm.RenderControl(textWrite);
Left by Arth on Apr 02, 2008 11:35 PM

# GridView Export to Excel Problems
Requesting Gravatar...
Hi,I have a problem while export gridview to excel. It was not export all records when page size setting. how to export all Record while page size is set.
Left by Bikash on Jun 13, 2008 10:52 PM

Your comment:
 (will show your gravatar)


Copyright © Mohammad Azam | Powered by: GeeksWithBlogs.net