Public Function OutPutCSV(ByVal FileName As String, ByVal DataTable As DataSet, Optional ByVal TableIndex As Integer = 0) As Boolean
Dim fw As StreamWriter
Dim myRow As DataRow
Dim myCol As DataColumn
Try
fw = New StreamWriter(FileName, True)
For Each myCol In DataTable.Tables(TableIndex).Columns
If myCol Is DataTable.Tables(TableIndex).Columns(DataTable.Tables(TableIndex).Columns.Count - 1) Then
fw.WriteLine(myCol.Caption)
Else
fw.Write(myCol.Caption)
fw.Write(",")
End If
Next myCol
For Each myRow In DataTable.Tables(TableIndex).Rows
For Each myCol In DataTable.Tables(TableIndex).Columns
If myCol Is DataTable.Tables(TableIndex).Columns(DataTable.Tables(TableIndex).Columns.Count - 1) Then
fw.WriteLine(myRow(myCol))
Else
fw.Write(myRow(myCol))
fw.Write(",")
End If
Next myCol
Next myRow
Return True
Catch e As Exception
MessageBox.Show(e.Message, "", MessageBoxButtons.OK, MessageBoxIcon.Error)
Return False
Finally
fw.Close()
End Try
End Function