Though the original post mentioned in the comments should have been enough help, this code should also do the trick
The ExportToCSV function takes a DataTable and a FileName as parameter, returns True on Success, False on failure (and prints the error to the console)
You could do the username encrypting at the places where i have put the remark OR you add them to the datatable before exporting it
The exported csv file works with MS Excel & SpreadSheets (KingSoft office)
Imports System.IO
Imports System.Text
Module Module1
Function wrapValue(value As String, group As String, separator As String) As String
If value.Contains(separator) Then
If value.Contains(group) Then
value = value.Replace(group, group + group)
End If
value = group & value & group
End If
Return value
End Function
Function ExportToCSV(dtable As DataTable, fileName As String) As Boolean
Dim result As Boolean = True
Try
Dim sb As New StringBuilder()
Dim separator As String = ";"
Dim group As String = """"
Dim newLine As String = Environment.NewLine
For Each column As DataColumn In dtable.Columns
sb.Append(wrapValue(column.ColumnName, group, separator) & separator)
Next
' here you could add the column for the username
sb.Append(newLine)
For Each row As DataRow In dtable.Rows
For Each col As DataColumn In dtable.Columns
sb.Append(wrapValue(row(col).ToString(), group, separator) & separator)
Next
' here you could extract the password for the username
sb.Append(newLine)
Next
Using fs As New StreamWriter(fileName)
fs.Write(sb.ToString())
End Using
Catch ex As Exception
Console.WriteLine(ex.Message & vbCrLf & ex.StackTrace)
result = False
End Try
Return result
End Function
Sub Main()
Dim dt As New DataTable
dt.Columns.Add("String Column", GetType(String))
dt.Columns.Add("Integer Column", GetType(Integer))
dt.Columns.Add("Bool Column", GetType(Boolean))
dt.Columns.Add("Double Column", GetType(Double))
For i As Integer = 0 To 10
dt.Rows.Add("string "";value " & i, i, IIf(i Mod 2, True, False), Math.Sqrt(i))
Next
If ExportToCSV(dt, Path.Combine(Environment.CurrentDirectory, "TestFile.csv")) Then
Console.WriteLine("CSV File succesfully written")
End If
Console.ReadLine()
End Sub
End Module