|
|
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();
}
VB.NET
Protected Sub Button1_Click(ByVal sender As Object, _
ByVal e As EventArgs)
Response.AddHeader("content-disposition", _ "attachment;filename=FileName.xls") Response.Charset = String.Empty Response.ContentType = "application/vnd.xls"Dim sw As System.IO.StringWriter = _
New System.IO.StringWriter()Dim hw As System.Web.UI.HtmlTextWriter = _
New HtmlTextWriter(sw)GridView1.RenderControl(hw)
Response.Write(sw.ToString())
Response.End()
End Sub
To read about some more tips and tricks about the GridView, check GridView Tips and Tricks using ASP.NET 2.0
'Like' us on our FaceBook page if you find this blog useful. Thanks!
Did you like this post?
|
|
|
||
|
|
|
|
Save on Delicious |
|
|
subscribe via rss |
|
subscribe via e-mail |
|
|
print this post |
|
follow me on twitter |




comments
8 Responses to "Export GridView to Excel"Trying the VB code I get: System.Web.HttpException: Control 'GridView1' of type 'GridView' must be placed inside a form tag with runat=server.
Peggy, This was a known issue filed over here
A possible solution: http://forums.asp.net/t/1239899.aspx
Hi,
I doubt.
I tried the code-segment in the post. When i click 'Button_1', the page simply refreshes & no excel files are generated.
Please note, my pc doesnt have any version of MS office installed. Im using OpenOffice3.0. Is it due to that...???
Thanks.
Oh!
I solved it. Has to do some searching in MSDN gallery and solved via including the below code inot my code-behind.
---
[code-behind]
public override void VerifyRenderingInServerForm(Control control)
{}
Glad to hear that you solved it! Can you share the complete solution for the benefit of future users.Thanks.
Thanks for your attention.
While building the sample, i encountered 2 problems. First, when i click the Button_1 for downloading, the page simply refreshes & no excel files are generated. When I build the website second time, I's getting the same error what Peggy was having.
The first error was solved automatically(i dont know how!). Peggy's error was solved when i include the 'VerifyRenderingInServerForm()' method in the code-behind.
Hence the complete code looks like
[Code-Behind]
protected void Button1_Click(object sender, EventArgs e)
{
// Same as above code
}
public override void VerifyRenderingInServerForm(Control control)
{
// Empty Method
}
---
Thankz.
Here is complete details about all problems we face in this process:
http://praveenbattula.blogspot.com/2010/09/gridview-and-export-
to-excel.html
Wow Abhilash!!!! thank you! you save my life! with that response!!!!
really work!
greetings from Colombia
Post a Comment