Hey guys,
I have a function that works perfectly to perform the DataGrid export to Excel but I would like to adapt it to the GridView export. Can someone help me?
Public Function exportarExcel(ByVal grid As DataGrid, ByVal saveAsFile As String)
' O limite de linhas do Excel é 65536
If grid.Items.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=" & saveAsFile & ".xls")
' Remover caracteres do header - Content-Type
HttpContext.Current.Response.Charset = "iso-8859-1"
HttpContext.Current.Response.ContentEncoding = System.Text.Encoding.GetEncoding("iso-8859-1")
' 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
Return grid
End Function
Grateful,
ilano