Creare un nuovo file di classe nel progetto denominato " DataTableHelper.vb "
2
Copia il seguente copia nel nuovo file : . Importazioni SystemImports System.DataImports System.IO
Public Class DataTableHelper
'' possibile lo streaming DataTable al browser , direttamente , è necessario impostare '' Response.Clear (); ' Response.Buffer = true; ' di risposta . ContentType = " application /vnd.ms -excel "; ' Response.AddHeader ( " Content-Disposition " , "inline ; filename = Clientes.xls "); ' Response.Charset = ""; ' this.EnableViewState = false' REALE CODICE ' ProduceCSV ( dt , Response.Output , true); '
Public Shared Sub ProduceCSV ( ByVal dt Come DataTable , _ByVal httpStream Come System.IO.TextWriter , WriteHeader ByVal As Boolean ) Dim i As Int32Dim j come Int32If WriteHeader Poi
Dim arr ( dt.Columns.Count ) As String
For i = 0 To dt.Columns.Count - . 1arr ( i) = dt.Columns ( i) ColumnNamearr ( i) = GetWriteableValue ( arr ( i) ) NexthttpStream.WriteLine ( String.Join ( " , " , arr) ) End If
Per j = 0 Per dt.Rows.Count - 1Dim dataArr (it. Columns.Count ) Come StringFor i = 0 To dt.Columns.Count - 1Dim o As Object = dt.Rows ( j ) (i ) dataArr ( i) = GetWriteableValue ( o) NexthttpStream.WriteLine ( String.Join ( " , " , dataArr )) Next End Sub
# Region " CSVProducer " Public Shared Sub ProduceCSV ( ByVal dt Come DataTable , file di _ByVal Come System.IO.StreamWriter , ByVal WriteHeader come booleano )
Dim i As Int32Dim j Come Int32If ( WriteHeader ) ThenDim arr ( dt.Columns.Count ) Come StringFor i = 0 To dt.Columns.Count - . 1arr ( i) = dt.Columns ( i) ColumnNamearr (i ) = GetWriteableValue ( arr ( i) ) Nextfile.WriteLine ( String.Join ( " , " , arr) ) End If
For j = 0 To dt.Rows.Count - 1Dim dataArr ( dt.Columns . Count ) Come StringFor i = 0 To dt.Columns.CountDim o As Object = dt.Rows ( j ) (i ) dataArr ( i) = GetWriteableValue ( o) Nextfile.WriteLine ( String.Join ( " , " , dataArr ) ) Succ.Fine Sub
software © www.354353.com