Public Function WriteToCSV(ByVal FileName As String) As Boolean
Dim fsCSV As System.IO.FileStream = Nothing
Dim swCSV As System.IO.StreamWriter = Nothing
Dim intRows As Integer = 0
Dim intCols As Integer = 0
Dim strRow As String = ""
Dim strHeader As String = ""
Dim strRowHeader As String = ""
Try
If (System.IO.File.Exists(FileName)) Then
System.IO.File.Delete(FileName)
End If
fsCSV = New FileStream(FileName, System.IO.FileMode.CreateNew, System.IO.FileAccess.Write)
If Not dsToReport Is Nothing Then
swCSV = New StreamWriter(fsCSV, System.Text.Encoding.Default)
strHeader="XXXX"
swCSV.WriteLine(strHeader)
For intCols = 0 To dsToReport.Tables(0).Columns.Count - 1
Select Case intCols
Case 0
strRowHeader = ""
strRowHeader += ","
Case 1
strRowHeader += ""
strRowHeader += ","
Case 2
strRowHeader += ""
strRowHeader += ","
Case 3
strRowHeader += ""
strRowHeader += ","
Case 4
strRowHeader += ""
strRowHeader += ","
Case 5
strRowHeader += ""
strRowHeader += ","
Case 6
strRowHeader += ""
strRowHeader += ","
Case 7
strRowHeader += ""
strRowHeader += ","
Case 8
strRowHeader += ""
strRowHeader += ","
Case 9
strRowHeader += ""
strRowHeader += ","
Case 10
strRowHeader += ""
strRowHeader += ","
Case 11
strRowHeader += ""
strRowHeader += ","
Case 12
strRowHeader += ""
strRowHeader += ","
Case 13
strRowHeader += ""
strRowHeader += ","
Case 14
strRowHeader += ""
strRowHeader += ","
Case 15
strRowHeader += ""
strRowHeader += ","
Case 16
strRowHeader += ""
strRowHeader += ","
Case 17
strRowHeader += ""
strRowHeader += ","
Case 18
strRowHeader += ""
strRowHeader += ","
End Select
Next
swCSV.WriteLine(strRowHeader)
For intRows = 0 To dsToReport.Tables(0).Rows.Count - 1
strRow = ""
For intCols = 0 To dsToReport.Tables(0).Columns.Count - 1
strRow += dsToReport.Tables(0).Rows(intRows)(intCols).ToString()
If intCols < dsToReport.Tables(0).Columns.Count - 1 Then
strRow += ","
End If
Next
' 写CSV文件
swCSV.WriteLine(strRow)
Next
swCSV.Close()
End If
fsCSV.Close()
Return True
Catch ex As Exception
Return False
End Try
End Function