ASP.NET 2.0 Exporting gridView to Excel

ValentinBadea

Newcomer
Joined
Mar 2, 2004
Messages
4
Location
Romania
Hello !

How can I export gridView to Excel ? I'm using ASP.NET 1.1 code:
Code:
Response.ContentType = "application/vnd.ms-excel"
		Response.Charset = ""
		'Me.EnableViewState = False
		Dim tw As New System.IO.StringWriter
		Dim hw As New System.Web.UI.HtmlTextWriter(tw)
		gvw.RenderControl(hw)
		Response.Write(tw.ToString)
		Response.End()
that works with 1.1 but here (2.0) this code rise error:
"Control 'ctl00_Main_gvw' of type 'GridView' must be placed inside a form tag with runat=server."
Any idea ?
 
Not got 2.0 installed on this PC so I can't test it but have you tried placing a literal on your page and assigning tw.ToString() to it's text property rather than using Response.Write?
 
My code for saving GridView to XLS

birch9397 said:
Did you ever figure this out? I am getting the same message.
This is how I am doing it:

Visual Basic:
Protected Sub btnSaveToXls_Click(ByVal sender As Object, _
 
 


ByVal e As System.EventArgs) Handles btnSaveToXls.Click[indent]'WARNING: In order to do this I had to set'EnableEventValidation = "false" in the Page definition

'which can open security holes, but since I am not requesting
'any user input in this page, I don't think it will cause any
'major issues
 
Dim gv As GridView = gvQcTestData

If gv.Rows.Count.ToString + 1 < 65536 Then[indent]gv.AllowPaging = Falsegv.DataBind()

Dim tw As New StringWriter
Dim hw As New HtmlTextWriter(tw)
Dim frm As HtmlForm = New HtmlForm()
Response.ContentType = "application/vnd.ms-excel"
Response.AppendHeader("content-disposition", _
"attachment;filename=TestData.xls")
Response.Charset = ""
EnableViewState = False
Controls.Add(frm)
frm.Controls.Add(gv)
frm.RenderControl(hw)
Response.Write(tw.ToString())
Response.End()
gv.AllowPaging = True
gv.DataBind()

[/indent]End If[/indent]End Sub
 
Last edited by a moderator:
Thatnks for the response. I found several posts on other boards that had to do the equivalent. Here's the relative snippet I came up with. Slightly different that yours (plus it's in C#) but it's essentially the same thing (dg is the GridView I'm passing into the method):

Code:
GridView newDG = dg;
newDG.AllowPaging = false;
newDG.AllowSorting = false;
newDG.EnableViewState = false;
newDG.CssClass = string.Empty;
ClearControls(newDG);
System.Web.UI.Page pg = new Page();
System.Web.UI.HtmlControls.HtmlForm frm = new System.Web.UI.HtmlControls.HtmlForm();
frm.Controls.Add(newDG);
pg.Controls.Add(frm);
                
StringWriter SW = new StringWriter(SB);
HtmlTextWriter htmlTW = new HtmlTextWriter(SW);

pg.RenderControl(htmlTW);
 
Change my code to and then turned on the Sorting on the GridView
and i still get the same error.

Sub doExcel(ByVal Source As Object, ByVal E As EventArgs)
GridView1.DataBind()
Dim tw As New StringWriter()
Dim hw As New System.Web.UI.HtmlTextWriter(tw)
Dim frm As HtmlForm = New HtmlForm()
Response.ContentType = "application/vnd.ms-excel"
Response.AddHeader("content-disposition", "attachment;filename=CustomerSalesData.xls")
'Response.AddHeader("content-disposition", "attachment;filename=" & txtFile.text & ".xls")
Response.Charset = ""
EnableViewState = False
Controls.Add(frm)
GridView1.CssClass = String.Empty
frm.Controls.Add(GridView1)
frm.RenderControl(hw)
Response.Write(tw.ToString())
Response.End()
GridView1.AllowPaging = "false"
GridView1.AllowSorting = "false"
GridView1.EnableViewState = "false"
GridView1.EnableSortingAndPagingCallbacks = "false"
frm.Controls.Add(frm)
GridView1.DataBind()
End Sub
 
Back
Top