Initial commit
This commit is contained in:
Binary file not shown.
@@ -0,0 +1,83 @@
|
||||
Imports System
|
||||
Imports System.Data
|
||||
Imports System.IO
|
||||
|
||||
Public Class DataTableHelper
|
||||
|
||||
'
|
||||
' Can stream DataTable to Browser, directly, you need to set
|
||||
'
|
||||
' Response.Clear();
|
||||
' Response.Buffer= true;
|
||||
' Response.ContentType = "application/vnd.ms-excel";
|
||||
' Response.AddHeader("Content-Disposition", "inline;filename=Clientes.xls");
|
||||
' Response.Charset = "";
|
||||
' this.EnableViewState = false
|
||||
' ACTUAL CODE
|
||||
' ProduceCSV(dt, Response.Output, true);
|
||||
'
|
||||
|
||||
Public Shared Sub ProduceCSV(ByVal dt As DataTable, _
|
||||
ByVal httpStream As System.IO.TextWriter, ByVal WriteHeader As Boolean)
|
||||
Dim i As Int32
|
||||
Dim j As Int32
|
||||
If WriteHeader Then
|
||||
|
||||
Dim arr(dt.Columns.Count) As String
|
||||
|
||||
For i = 0 To dt.Columns.Count - 1
|
||||
arr(i) = dt.Columns(i).ColumnName
|
||||
arr(i) = GetWriteableValue(arr(i))
|
||||
Next
|
||||
httpStream.WriteLine(String.Join(",", arr))
|
||||
End If
|
||||
|
||||
For j = 0 To dt.Rows.Count - 1
|
||||
Dim dataArr(dt.Columns.Count) As String
|
||||
For i = 0 To dt.Columns.Count - 1
|
||||
Dim o As Object = dt.Rows(j)(i)
|
||||
dataArr(i) = GetWriteableValue(o)
|
||||
Next
|
||||
httpStream.WriteLine(String.Join(",", dataArr))
|
||||
Next
|
||||
|
||||
End Sub
|
||||
|
||||
#Region "CSVProducer"
|
||||
Public Shared Sub ProduceCSV(ByVal dt As DataTable, _
|
||||
ByVal file As System.IO.StreamWriter, ByVal WriteHeader As Boolean)
|
||||
|
||||
Dim i As Int32
|
||||
Dim j As Int32
|
||||
If (WriteHeader) Then
|
||||
Dim arr(dt.Columns.Count - 1) As String
|
||||
For i = 0 To dt.Columns.Count - 1
|
||||
arr(i) = dt.Columns(i).ColumnName
|
||||
arr(i) = GetWriteableValue(arr(i))
|
||||
Next
|
||||
file.WriteLine(String.Join(",", arr))
|
||||
End If
|
||||
|
||||
For j = 0 To dt.Rows.Count - 1
|
||||
Dim dataArr(dt.Columns.Count - 1) As String
|
||||
For i = 0 To dt.Columns.Count - 1
|
||||
Dim o As Object = dt.Rows(j)(i)
|
||||
dataArr(i) = GetWriteableValue(o)
|
||||
Next
|
||||
file.WriteLine(String.Join(",", dataArr))
|
||||
Next
|
||||
End Sub
|
||||
|
||||
Public Shared Function GetWriteableValue(ByVal o As Object) As String
|
||||
If o Is Nothing OrElse IsDBNull(o) Then
|
||||
Return ""
|
||||
ElseIf (o.ToString().IndexOf(",") = -1) Then
|
||||
Return o.ToString()
|
||||
Else
|
||||
Return "\"" + o.ToString() + " \ ""
|
||||
|
||||
End If
|
||||
End Function
|
||||
#End Region
|
||||
|
||||
End Class
|
||||
Reference in New Issue
Block a user