ASP.NET - Analista de Sistemas

 
Vista:

Analista de Sistemas

Publicado por Ivana (2 intervenciones) el 16/05/2007 20:13:18
Necesito exportar los datos de un datatable a excel y obtengo el siguiente error:

El tipo 'System.Web.UI.WebControls.GridView' del ensamblado 'System.Web, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a' no está marcado como serializable.
Descripción: Excepción no controlada al ejecutar la solicitud Web actual. Revise el seguimiento de la pila para obtener más información acerca del error y dónde se originó en el código.

Detalles de la excepción: System.Runtime.Serialization.SerializationException: El tipo 'System.Web.UI.WebControls.GridView' del ensamblado 'System.Web, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a' no está marcado como serializable.

El código para la exportación es el siguiente:

Public Overloads Sub ExportarDTExcel(ByVal DT As Data.DataTable, Optional ByVal FileName As String = "tmp")
Try
Dim strhtml As String
strhtml = "<TABLE> "
Dim cantCol As Integer
cantCol = DT.Columns.Count
'BARRA DE TITULO
strhtml &= "<TR><B> "
Dim c As Int32 = 0
For c = 0 To cantCol - 1 Step 1
strhtml &= "<TD bgColor=black style='COLOR: white; FONT-FAMILY: Arial'> " & DT.Columns(c).ToString & "</TD> "
Next c
strhtml &= "</B></TR> "
Dim itm As Data.DataRow
For Each itm In DT.Rows
'ITEMS
strhtml &= "<TR> "
c = 0
For c = 0 To cantCol - 1 Step 1
strhtml &= "<TD> " & itm(c).ToString & "</TD> "
Next
strhtml &= "</TR> "
Next
strhtml &= "</TABLE> "

''EL ERROR OCURRE EN LA SIGUIENTE LÍNEA DE CÓDIGO:
Dim FilePath As String = Request.PhysicalApplicationPath & "tmp/" & FileName & ".xls"
Dim File As New System.IO.FileStream(FilePath, IO.FileMode.Create, IO.FileAccess.Write)
Dim Writer As New System.IO.StreamWriter(File, System.Text.Encoding.Unicode)
Writer.Write(strhtml)
Writer.Close()
File.Close()
Response.Redirect("tmp/" & FileName & ".xls", False)

Catch ex As Exception
Throw New Exception("No se pudo completar el proceso para exportar a Excel.", ex)
End Try

end sub
Valora esta pregunta
Me gusta: Está pregunta es útil y esta claraNo me gusta: Está pregunta no esta clara o no es útil
0
Responder