Guest User

Untitled

a guest
Jan 18th, 2018
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.17 KB | None | 0 0
  1. Public ReadOnly Property IsReusable As Boolean Implements IHttpHandler.IsReusable
  2. Get
  3. Throw New NotImplementedException()
  4. End Get
  5. End Property
  6.  
  7. Public Sub ProcessRequest(ByVal context As HttpContext) Implements IHttpHandler.ProcessRequest
  8.  
  9. Dim connectionString As String = "Data Source=(Local); Initial Catalog= SPC; Integrated Security= true"
  10. Using cnn As New SqlConnection(connectionString)
  11.  
  12.  
  13. Dim selectCommand As String = "select tbl_asignacion_criterios.cod_asigcriterio,tbl_asignacion_criterios.cod_edicion,tbl_edicion.nombre_edicion ,tbl_criterios.nombre_criterio,tbl_detalle_asignacion.cod_criterio from tbl_asignacion_criterios inner join tbl_edicion on tbl_asignacion_criterios.cod_edicion=tbl_edicion.cod_edicion inner join tbl_detalle_asignacion on tbl_asignacion_criterios.cod_asigcriterio=tbl_detalle_asignacion.cod_criterio inner join tbl_criterios on tbl_detalle_asignacion.cod_criterio=tbl_criterios.cod_criterio "
  14. Dim da As New SqlDataAdapter(selectCommand, cnn)
  15. Dim ds As New DataSet
  16. da.Fill(ds)
  17. ExportDataTableToExcel(ds.Tables.Item(0))
  18.  
  19. End Using
  20.  
  21. End Sub
  22.  
  23.  
  24.  
  25.  
  26. Private Sub DoSomething()
  27.  
  28. End Sub
  29.  
  30. Public Overloads Shared Sub ExportDataTableToExcel(ByVal table As DataTable)
  31. ExportDataTableToExcel(table, String.Empty, Nothing)
  32. End Sub
  33. Public Overloads Shared Sub ExportDataTableToExcel(ByVal table As DataTable, ByVal captions As Dictionary(Of String, String))
  34. ExportDataTableToExcel(table, String.Empty, captions)
  35. End Sub
  36. Public Overloads Shared Sub ExportDataTableToExcel(ByVal table As DataTable, ByVal name As String)
  37. ExportDataTableToExcel(table, name, Nothing)
  38. End Sub
  39. Public Overloads Shared Sub ExportDataTableToExcel(ByVal table As DataTable, ByVal name As String, ByVal captions As Dictionary(Of String, String))
  40. Dim content As New Text.StringBuilder()
  41. Dim columnName As String = String.Empty
  42. For Each column As DataColumn In table.Columns
  43. If Not captions Is Nothing Then
  44. If Not captions.TryGetValue(column.ColumnName, columnName) Then
  45. columnName = column.ColumnName
  46. End If
  47. Else
  48. columnName = column.ColumnName
  49. End If
  50. content.Append(columnName & ";")
  51. Next
  52. content.Append(Environment.NewLine)
  53. Dim value As String
  54. For Each row As DataRow In table.Rows
  55. For i As Integer = 0 To table.Columns.Count - 1
  56. value = String.Empty
  57. If Not row.IsNull(i) Then
  58. value = row(i).ToString().Replace(";", String.Empty)
  59. End If
  60. content.Append(value & ";")
  61. Next
  62. content.Append(Environment.NewLine)
  63. Next
  64. content.Length = content.Length - 1
  65. Dim context As HttpContext = HttpContext.Current
  66. With context.Response
  67. .Clear()
  68. .ContentType = "text/csv"
  69. If String.IsNullOrEmpty(name) Then
  70. name = DateTime.Now.ToString("ddMMyyyyHHmmss")
  71. End If
  72. .AppendHeader("Content-Disposition", String.Format("attachment; filename={0}.csv", name))
  73. .Charset = Encoding.Unicode.WebName
  74. .ContentEncoding = Encoding.Unicode
  75. .Write(content.ToString())
  76. .End()
  77. End With
  78. End Sub
Add Comment
Please, Sign In to add comment