Quando tento exportar um gridview para o formato do excel(xls) acontece o seguinte erro:
---------------------------
Microsoft Internet Explorer
---------------------------
Control 'ctl00_Main_gvCliente' of type 'GridView' must be placed inside a form tag with runat=server.
---------------------------
OK
---------------------------
O código que estou utilizando segue abaixo:
Public Sub exportarExcel(ByVal grid As GridView, ByVal nomeArquivo As String)
' O linite de linhas do Excel é 65536
If grid.Rows.Count.ToString + 1 < 65536 Then
HttpContext.Current.Response.Clear()
HttpContext.Current.Response.ContentType = "application/vnd.ms-excel"
HttpContext.Current.Response.AddHeader("content-disposition", "attachment;filename=" & nomeArquivo & ".xls")
' Remover caracteres do header - Content-Type
HttpContext.Current.Response.Charset = ""
'HttpContext.Current.Response.WriteFile("style.txt")
' desabilita o view state.
grid.EnableViewState = False
Dim tw As New System.IO.StringWriter()
Dim hw As New System.Web.UI.HtmlTextWriter(tw)
grid.RenderControl(hw)
' Escrever o html no navegador
HttpContext.Current.Response.Write(tw.ToString())
' termina o response
HttpContext.Current.Response.End()
Else
HttpContext.Current.Response.Write("Muitas linhas para exportar para o Excel !!!")
End If
End Sub
Por favor alguem pode me ajudar!!