- Mar 1, 2000
- 30,890
- 5,001
- 126
I have the following code which will export my datagrid to .csv. The problem that I have though is that I need the values to be wrapped in double quotes (") so that any values with commas won't be thrown off when opening in Excel etc.
Can somebody help me with this? I feel as if the solution is easy, but I just can't see it this morning...
Thanks in advance.
Can somebody help me with this? I feel as if the solution is easy, but I just can't see it this morning...
Thanks in advance.
Code:
Try
If Not Directory.Exists("C:\My Files\" & Format(Now(), "yyyy") & "\") Then
Directory.CreateDirectory("C:\My Files\" & Format(Now(), "yyyy") & "\")
End If
Catch ex As System.Exception
System.Windows.Forms.MessageBox.Show(ex.Message)
End Try
Dim headers = (From header As DataGridViewColumn In DataGridView1.Columns.Cast(Of DataGridViewColumn)() _
Select header.HeaderText).ToArray
Dim rows = From row As DataGridViewRow In DataGridView1.Rows.Cast(Of DataGridViewRow)() _
Where Not row.IsNewRow _
Select Array.ConvertAll(row.Cells.Cast(Of DataGridViewCell).ToArray, Function(c) If(c.Value IsNot Nothing, c.Value.ToString, ""))
Using sw As New IO.StreamWriter("C:\My Files\" & Format(Now(), "yyyy") & "\" & Format(Now(), "yyyy-MM-dd") & " file.csv")
sw.WriteLine(String.Join(",", headers))
For Each r In rows
sw.WriteLine(String.Join(",", r))
Next
End Using
Last edited: