从datagridview导出到csv时更改日期格式

时间:2016-01-23 12:29:15

标签: vb.net date csv datagridview

现在标题可能会误解已经问过的问题,但我想根据我在vb.net中的代码找到一个具体的答案

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

我的上一栏是格式为yyyy-mm-dd HH:mm:ss的日期,但是当导出到.csv时,日期格式不同,我需要更改以制作所述的日期格式?感谢您的时间和精力

1 个答案:

答案 0 :(得分:0)

在您写入Excel之前,请将列的格式设为文本。这应该纠正它

Cell.number.format =“@”