December 10
Microsoft Excel è un software foglio di calcolo che permette di inserire e gestire grandi quantità di dati in fogli di lavoro. Se si desidera trasferire i dati in Excel da un database, Visual Basic è un linguaggio di programmazione di Microsoft che consente di copiare il contenuto di un set di dati in un file Excel. Visual Basic è stato progettato per i programmatori principianti e viene utilizzato principalmente per sviluppare e personalizzare le applicazioni Windows come Word, PowerPoint ed Excel.
1 Creare un nuovo file di classe nel progetto denominato "DataTableHelper.vb".
2 Copiare il seguente copia nel nuovo file:
Imports System
Imports System.Data
Imports System.IO
Public Class DataTableHelper
'
' Can stream DataTable to Browser, directly, you need to set
'
' Response.Clear()
ANDARE
' Response.Buffer= true
ANDARE
' Response.ContentType = \"application/vnd.ms-excel\"
ANDARE
' Response.AddHeader(\"Content-Disposition\", \"inline;filename=Clientes.xls\");
' Response.Charset = \"\"
ANDARE
' this.EnableViewState = false
' ACTUAL CODE
' ProduceCSV(dt, Response.Output, true)
ANDARE
'
Public Shared Sub ProduceCSV (ByVal dt come DataTable, _
ByVal httpStream Come System.IO.TextWriter, ByVal WriteHeader come booleano)
Dim i As Int32
Dim j As Int32
If WriteHeader Then
arr Dim (dt.Columns.Count) As String
Per i = 0 Per dt.Columns.Count - 1
arr (i) = dt.Columns (i) .ColumnName
arr (i) = GetWriteableValue (arr (i))
Il prossimo
httpStream.WriteLine (String.Join (\ & quot;, \ & quot ;, arr))
Finisci se
Per j = 0 Per dt.Rows.Count - 1
Dim dataArr (dt.Columns.Count) As String
Per i = 0 Per dt.Columns.Count - 1
Dim o As Object = dt.Rows (J) (i)
dataArr (i) = GetWriteableValue (o)
Il prossimo
httpStream.WriteLine (String.Join (\ & quot;, \ & quot ;, dataArr))
Il prossimo
End Sub Regione \ "CSVProducer \"
Public Shared Sub ProduceCSV(ByVal dt As DataTable, _
file di ByVal come System.IO.StreamWriter, ByVal WriteHeader come booleano)
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
file.WriteLine(String.Join(\",\", arr))
End If
Per j = 0 Per dt.Rows.Count - 1
Dim dataArr (dt.Columns.Count) As String
Per i = 0 Per dt.Columns.Count
Dim o As Object = dt.Rows (J) (i)
dataArr (i) = GetWriteableValue (o)
Il prossimo
file.WriteLine (String.Join (\ & quot;, \ & quot ;, dataArr))
Il prossimo
End Sub
Pubblica funzione condivisa GetWriteableValue (ByVal o come oggetto) come stringa
Se o è niente OrElse IsDBNull (o) Poi
Return \ & quot; \ & quot;
ElseIf (. O.ToString () IndexOf (\ & quot;, \ & quot;) = -1) Poi
Return o.ToString ()
Altro
Return \ & quot; \\\ & quot; \ & quot; + O.ToString () + \ & quot; \\ \ & Quot; \ & quot;
Finisci se
End Function End Regione
classe fine
3 Eseguire il seguente codice per utilizzare la nuova classe, e vedere l'output csv alla console:
Dim dt Come DataTable = ds.Tables (0)
Dim swriter Come IO.StreamWriter = Nuovo IO.StreamWriter (\ "c: \ yourCSVFile.csv \")
DataTableHelper.ProduceCSV (dt, swriter, True)