i have one gridview which I need to export it to excel ,
is there any way using VB
I 'm using the following code in the click event of a button on the page
Response.ContentType =
"application/vnd.ms-excel"
' Remove the charset from the Content-Type header.
Response.Charset =
""
' Turn off the view state.
Me.EnableViewState =
False
Dim tw
As New System.IO.StringWriter()
Dim hw As
New System.Web.UI.HtmlTextWriter(tw)
' Get the HTML for the control.
GridView1.RenderControl(hw)
' Write the HTML back to the browser.
Response.Write(tw.ToString())
' End the response.
Response.End()
but I'm getting the following error message
" Control 'ctl00_MainContent_GridView1' of type 'GridView' must be placed inside a form tag with runat=server. "
Based on my understanding, you want to export the GridView into Excel in your asp.net VB application. When you try to run it, you get the error message above. If I have misunderstood you, please feel free to let me know.
Sometimes, we can confirm that an HtmlForm control is rendered for the specified ASP.NET server control at run time using VerifyRenderingInServerForm method.
Please try to add the following code in your .aspx.vb file:
Public Overloads Overrides Sub VerifyRenderingInServerForm(ByVal control As Control)
End Sub
I hope this helps.
Thomas Sun
Microsoft Online Community Support
Please remember to click “Mark as Answer” on the post that helps you, and to click “Unmark as Answer” if a marked post does not actually answer your question.
Please remember to click “Mark as Answer” on the post that helps you, and to click “Unmark as Answer” if a marked post does not actually answer your question.
sarajuma
Member
3 Points
21 Posts
Export to excel
Oct 24, 2007 06:48 AM|LINK
i have one gridview which I need to export it to excel ,
is there any way using VB
I 'm using the following code in the click event of a button on the page
Response.ContentType =
"application/vnd.ms-excel" ' Remove the charset from the Content-Type header.Response.Charset =
"" ' Turn off the view state. Me.EnableViewState = False Dim tw As New System.IO.StringWriter() Dim hw As New System.Web.UI.HtmlTextWriter(tw) ' Get the HTML for the control.GridView1.RenderControl(hw)
' Write the HTML back to the browser.Response.Write(tw.ToString())
' End the response.Response.End()
but I'm getting the following error message
" Control 'ctl00_MainContent_GridView1' of type 'GridView' must be placed inside a form tag with runat=server. "
anything I can do .
thanks
Excel
ca8msm
Star
12439 Points
2153 Posts
Re: Export to excel
Oct 24, 2007 08:59 AM|LINK
Have a read of the following article (especially the last paragraph):
http://aspnetlibrary.com/articledetails.aspx?article=How-to-export-a-GridView-to-Excel
Website Design Darlington - http://lessthandot.com -
http://aspnetlibrary.com
M A A Mehedi...
Contributor
7324 Points
939 Posts
Re: Export to excel
Oct 24, 2007 09:32 AM|LINK
Hi,
Put the gridview inside the form tag i.e.
<form id="Form1" method="post" runat="server">
<asp:DataGrid id="DataGrid1" runat="server"></asp:DataGrid>
</form>
To Export to Excel
Mark as answer if the post meets your requirement!
sarajuma
Member
3 Points
21 Posts
Re: Export to excel
Oct 24, 2007 11:01 AM|LINK
thanks for the reply , but I'm getting this error now
RegisterForEventValidation can only be called during Render();
any ideas ???
Thomas Sun –...
All-Star
64969 Points
5621 Posts
Re: Export to excel
Oct 29, 2007 01:59 AM|LINK
Hi,
Based on my understanding, you want to export the GridView into Excel in your asp.net VB application. When you try to run it, you get the error message above. If I have misunderstood you, please feel free to let me know.
Sometimes, we can confirm that an HtmlForm control is rendered for the specified ASP.NET server control at run time using VerifyRenderingInServerForm method.
Please try to add the following code in your .aspx.vb file:
Public Overloads Overrides Sub VerifyRenderingInServerForm(ByVal control As Control)
End Sub
I hope this helps.
Microsoft Online Community Support
Please remember to click “Mark as Answer” on the post that helps you, and to click “Unmark as Answer” if a marked post does not actually answer your question.
Thomas Sun –...
All-Star
64969 Points
5621 Posts
Re: Export to excel
Oct 29, 2007 02:11 AM|LINK
Hi,
For the this error message, please try to set the EnableEventValidation to false. For example:
I hope this helps.Microsoft Online Community Support
Please remember to click “Mark as Answer” on the post that helps you, and to click “Unmark as Answer” if a marked post does not actually answer your question.
allanhorwitz
Contributor
2517 Points
623 Posts
Re: Export to excel
May 07, 2008 06:41 PM|LINK
eforr
Member
49 Points
16 Posts
Re: Export to excel
May 07, 2008 07:59 PM|LINK
try this
Dim stringWrite As IO.StringWriter Dim htmlWrite As HtmlTextWriter Dim frm As HtmlForm = New HtmlForm()Response.Clear()
Response.AddHeader("content-disposition", "attachment;filename=titleforexcelhere.xls")Response.Charset =
""Response.ContentType =
"application/vnd.ms-xls" Dim strStyle As String = "<style>.text {mso-number-format:\@; } </style>"Response.Write(strStyle)
Response.Write(
"<H4>") ' throw in a pageheaderResponse.Write(
"you can put title here")Response.Write("</H4>")
stringWrite = New IO.StringWriterhtmlWrite = New HtmlTextWriter(stringWrite)Controls.Add(frm)
frm.Controls.Add(GridView1)
frm.RenderControl(htmlWrite)
Response.Write(stringWrite.ToString())
Response.End()
but gridview must be inside form tags
BarbaMarioli...
Member
208 Points
136 Posts
Re: Export to excel
Nov 02, 2009 09:50 AM|LINK
What you are doing here is basically fooling your browser to redirect this request to Excel program although generated output is HTML.
There are much cleaner ways of exporting to Excel. Check out GemBox.Spreadsheet and this web sample demonstrates CSV/XLS/XLSX/ODS/HMTL writing.
bartekm
Member
240 Points
106 Posts
Re: Export to excel
Dec 07, 2009 09:41 PM|LINK
The problem is that you also need to overwrite the render() method.
See if this article helps
http://blog.evonet.com.au