CSV处的数据未正确对齐

时间:2019-07-18 05:05:07

标签: vb.net csv export

早上好,将数据从datagrid导出到csv文件时遇到麻烦。

Sub csv()

    Dim headers = (From header As DataGridViewColumn In dgShow.Columns.Cast(Of DataGridViewColumn)()
                   Select header.HeaderText).ToArray
    Dim rows = From row As DataGridViewRow In dgShow.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("export.csv")
        sw.WriteLine(String.Join(",", headers))
        For Each r In rows
            sw.WriteLine(String.Join(",", r))
        Next
    End Using
    Process.Start("export.csv")




End Sub

这是我的示例代码

这里是数据网格

data grid

内部csv文件

csv

1 个答案:

答案 0 :(得分:0)

这是我的解决方法:

sw.WriteLine(String.Join(",", r).Replace(vbCrLf, [String].Empty))