DataGridView 到 CSV 文件

发布于 2024-11-24 05:16:13 字数 1146 浏览 3 评论 0原文

我有一个 VB 2010 Express 项目,其中有一个 DataGridView,我正在尝试将其写入 CSV 文件。

我已经写完了。但它很慢。慢 = 8 列 6000 行可能需要 30 秒。

这是我的代码:

    Private Sub btnExportData_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnExportData.Click
    Dim StrExport As String = ""
    For Each C As DataGridViewColumn In DataGridView1.Columns
        StrExport &= """" & C.HeaderText & ""","
    Next
    StrExport = StrExport.Substring(0, StrExport.Length - 1)
    StrExport &= Environment.NewLine

    For Each R As DataGridViewRow In DataGridView1.Rows
        For Each C As DataGridViewCell In R.Cells
            If Not C.Value Is Nothing Then
                StrExport &= """" & C.Value.ToString & ""","
            Else
                StrExport &= """" & "" & ""","
            End If
        Next
        StrExport = StrExport.Substring(0, StrExport.Length - 1)
        StrExport &= Environment.NewLine
    Next

    Dim tw As IO.TextWriter = New IO.StreamWriter("C:\Test1.CSV")
    tw.Write(StrExport)
    tw.Close()
End Sub

有谁知道我可以做些什么来加快速度?

谢谢

I have a VB 2010 Express Project, that has a DataGridView in it, that I am trying to write to a CSV file.

I have the write all working. But its slow. Slow = maybe 30 seconds for 6000 rows over 8 columns.

Here is my code:

    Private Sub btnExportData_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnExportData.Click
    Dim StrExport As String = ""
    For Each C As DataGridViewColumn In DataGridView1.Columns
        StrExport &= """" & C.HeaderText & ""","
    Next
    StrExport = StrExport.Substring(0, StrExport.Length - 1)
    StrExport &= Environment.NewLine

    For Each R As DataGridViewRow In DataGridView1.Rows
        For Each C As DataGridViewCell In R.Cells
            If Not C.Value Is Nothing Then
                StrExport &= """" & C.Value.ToString & ""","
            Else
                StrExport &= """" & "" & ""","
            End If
        Next
        StrExport = StrExport.Substring(0, StrExport.Length - 1)
        StrExport &= Environment.NewLine
    Next

    Dim tw As IO.TextWriter = New IO.StreamWriter("C:\Test1.CSV")
    tw.Write(StrExport)
    tw.Close()
End Sub

Does anyone know what I can do to speed it up?

thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

与君绝 2024-12-01 05:16:14

您难道不喜欢花几个小时搜索,然后发布问题,几分钟后自己找到答案吗?

这对我来说很有效:

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("csv.txt")
    sw.WriteLine(String.Join(",", headers))
    For Each r In rows
        sw.WriteLine(String.Join(",", r))
    Next
End Using
Process.Start("csv.txt")

Don't you just love it when you spend hours searching, then post a question, and a few minutes later find the answer yourself?

This did the trick for me:

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("csv.txt")
    sw.WriteLine(String.Join(",", headers))
    For Each r In rows
        sw.WriteLine(String.Join(",", r))
    Next
End Using
Process.Start("csv.txt")
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文