ValentinBadea Posted June 22, 2005 Posted June 22, 2005 Hello ! How can I export gridView to Excel ? I'm using ASP.NET 1.1 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 ? Quote
Administrators PlausiblyDamp Posted June 22, 2005 Administrators Posted June 22, 2005 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? Quote Posting Guidelines FAQ Post Formatting Intellectuals solve problems; geniuses prevent them. -- Albert Einstein
ValentinBadea Posted June 22, 2005 Author Posted June 22, 2005 I'll try, but the error rise on rendering control, on line gvw.RenderControl(hw) (a one line before !) Thank you anyway ! Quote
birch9397 Posted April 26, 2006 Posted April 26, 2006 Did you ever figure this out? I am getting the same message. Quote
tfowler Posted April 27, 2006 Posted April 27, 2006 (edited) My code for saving GridView to XLS Did you ever figure this out? I am getting the same message.This is how I am doing it: 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 Edited April 27, 2006 by PlausiblyDamp Quote
birch9397 Posted April 28, 2006 Posted April 28, 2006 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): 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); Quote
phoenixfire425 Posted March 26, 2008 Posted March 26, 2008 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 Quote
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.