I have a requirement to export a dataset as a CSV file.
I have spent a while searching for a set of rules to go by and realised there are quite a few rules and exce
Here is the function you can use to generate a row of CSV file from string list (IEnumerable(Of String) or string array can be used as well):
Function CreateCSVRow(strArray As List(Of String)) As String
Dim csvCols As New List(Of String)
Dim csvValue As String
Dim needQuotes As Boolean
For i As Integer = 0 To strArray.Count() - 1
csvValue = strArray(i)
needQuotes = (csvValue.IndexOf(",", StringComparison.InvariantCulture) >= 0 _
OrElse csvValue.IndexOf("""", StringComparison.InvariantCulture) >= 0 _
OrElse csvValue.IndexOf(vbCrLf, StringComparison.InvariantCulture) >= 0)
csvValue = csvValue.Replace("""", """""")
csvCols.Add(If(needQuotes, """" & csvValue & """", csvValue))
Next
Return String.Join(",", csvCols.ToArray())
End Function
As I think, it won't be difficult to convert from VB.NET to C#)