Marwan Itani
Marwan Itani

Reputation: 11

Change date format when exporting from datagridview to csv

Now the title might be misleading to an already asked question, but I want a specific answer according to the code that I have in vb.net which is

Private Sub Button1_Click_1(sender As Object, e As EventArgs) Handles Button1.Click
Dim headers = (From header As DataGridViewColumn In DGVData.Columns.Cast(Of DataGridViewColumn)() _
          Select header.HeaderText).ToArray
        Dim rows = From row As DataGridViewRow In DGVData.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("csv.csv")
            sw.WriteLine(String.Join(",", headers))
            For Each r In rows
                sw.WriteLine(String.Join(",", r))
            Next
        End Using
        Process.Start("csv.csv")
    End If
End Sub

My last column is a date of format yyyy-mm-dd HH:mm:ss, however when exported to .csv the date format is different, what do I need to change to make the date format as mentioned? Thank you for your time and efforts

Upvotes: 1

Views: 785

Answers (1)

codeMonger123
codeMonger123

Reputation: 505

Before you write to excel make the format of the column as text. This should correct it

Cell.number.format = "@"

Upvotes: 0

Related Questions