如何在 Visual Basic 2010 中读取 CSV 文件并将结果显示在网格中? [英] How do you read a CSV file and display the results in a grid in Visual Basic 2010?

查看:18
本文介绍了如何在 Visual Basic 2010 中读取 CSV 文件并将结果显示在网格中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在 Visual Basic 2010 中读取 CSV 文件并将结果显示在网格中?这听起来很简单,但我在谷歌搜索了一段时间后仍然找不到答案.我在一个表单上有 DataGridView,它被称为 DataGridView1.我有一个只有 3 列数据的 CSV,我希望能够显示它们.

How do you read a CSV file and display the results in a grid in Visual Basic 2010? This sounds so simple but I still can't find the answer to it after googling for a while. I have DataGridView on a form and it's called DataGridView1. I have a CSV with just 3 columns of data and I want to be able to display them.

推荐答案

使用 TextFieldParser 类内置于 .Net 框架中.

Use the TextFieldParser class built into the .Net framework.

这是从复制的一些代码MSDN 论坛帖子 作者:Paul Clement.它将 CSV 转换为新的内存中的 DataTable,然后将 DataGridView 绑定到 DataTable

Here's some code copied from an MSDN forum post by Paul Clement. It converts the CSV into a new in-memory DataTable and then binds the DataGridView to the DataTable

    Dim TextFileReader As New Microsoft.VisualBasic.FileIO.TextFieldParser("C:Documents and Settings...My DocumentsMy DatabaseTextSemiColonDelimited.txt")

    TextFileReader.TextFieldType = FileIO.FieldType.Delimited
    TextFileReader.SetDelimiters(";")

    Dim TextFileTable As DataTable = Nothing

    Dim Column As DataColumn
    Dim Row As DataRow
    Dim UpperBound As Int32
    Dim ColumnCount As Int32
    Dim CurrentRow As String()

    While Not TextFileReader.EndOfData
        Try
            CurrentRow = TextFileReader.ReadFields()
            If Not CurrentRow Is Nothing Then
                ''# Check if DataTable has been created
                If TextFileTable Is Nothing Then
                    TextFileTable = New DataTable("TextFileTable")
                    ''# Get number of columns
                    UpperBound = CurrentRow.GetUpperBound(0)
                    ''# Create new DataTable
                    For ColumnCount = 0 To UpperBound
                        Column = New DataColumn()
                        Column.DataType = System.Type.GetType("System.String")
                        Column.ColumnName = "Column" & ColumnCount
                        Column.Caption = "Column" & ColumnCount
                        Column.ReadOnly = True
                        Column.Unique = False
                        TextFileTable.Columns.Add(Column)
                    Next
                End If
                Row = TextFileTable.NewRow
                For ColumnCount = 0 To UpperBound
                    Row("Column" & ColumnCount) = CurrentRow(ColumnCount).ToString
                Next
                TextFileTable.Rows.Add(Row)
            End If
        Catch ex As _
        Microsoft.VisualBasic.FileIO.MalformedLineException
            MsgBox("Line " & ex.Message & _
            "is not valid and will be skipped.")
        End Try
    End While
    TextFileReader.Dispose()
    frmMain.DataGrid1.DataSource = TextFileTable

这篇关于如何在 Visual Basic 2010 中读取 CSV 文件并将结果显示在网格中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆