Private Sub ExportarGridView(ByRef gv As GridView, Optional ByVal NombreArchivo As String = "datos.xls")
gv.AllowPaging = False
gv.AllowSorting = False
ClearControls(gv)
Me.EnableViewState = False
Response.Buffer = True
Response.ContentType = "application/vnd.ms-excel"
Response.AddHeader("content-disposition", "attachment;filename=" _
& NombreArchivo)
Dim tw As New System.IO.StringWriter()
gv.RenderControl(New HtmlTextWriter(tw))
Response.Write("<HEAD><STYLE type=""text/css"">" _
& ReadCss("~/ExportarExcel.css") & "<STYLE></HEAD>")
Response.Write(tw.ToString)
Response.End()
End Sub
Private Function ReadCss(ByVal arch As String) As String
Dim archivo As String = Server.MapPath(arch)
Dim flujoLectura As System.IO.StreamReader
flujoLectura = System.IO.File.OpenText(archivo)
Dim contenido As String = flujoLectura.ReadToEnd()
flujoLectura.Close()
Return contenido
End Function
Protected Sub ClearControls(ByRef control As Control)
Dim i As Integer
For i = control.Controls.Count - 1 To 0 Step -1
ClearControls(control.Controls(i))
Next i
If Not TypeOf control Is TableCell Then
If Not (control.GetType().GetProperty("SelectedItem") Is Nothing) Then
Dim literal As New LiteralControl()
control.Parent.Controls.Add(literal)
Try
literal.Text = CStr(control.GetType().GetProperty("SelectedItem").GetValue(control, Nothing))
Catch
End Try
control.Parent.Controls.Remove(control)
Else
If Not (control.GetType().GetProperty("Text") Is Nothing) Then
Dim literal As New LiteralControl()
control.Parent.Controls.Add(literal)
literal.Text = CStr(control.GetType().GetProperty("Text").GetValue(control, Nothing))
control.Parent.Controls.Remove(control)
End If
End If
End If
Return
End Sub
Public Overrides Sub VerifyRenderingInServerForm(ByVal control As System.Web.UI.Control)
End Sub
No hay comentarios:
Publicar un comentario