【问题标题】:VB.NET read only some cells for each row from csv fileVB.NET 从 csv 文件中读取每一行的一些单元格
【发布时间】:2013-08-17 13:46:34
【问题描述】:

我有一个 csv 文件,其结构如下:

id,name,adress,email,age
1,john,1 str xxxx,john@gmail.com,19
2,mike,2 bd xxxx,mike@gmail.com,21
3,jeana,1 str ssss,jeana@gmail.com,18
.......................
.......................

我想做的是读取 csv 文件,跳过第一行(包含标题)并从每行中提取第二、第三和第四数据并填充数据网格视图。

这是我正在使用的代码,但它为我带来了所有 csv 内容:

DataGridView1.ColumnCount = 4
DataGridView1.Columns(0).Name = "ID"
DataGridView1.Columns(1).Name = "NAME"
DataGridView1.Columns(2).Name = "ADRESS"
DataGridView1.Columns(3).Name = "AGE"
Using MyReader As New Microsoft.VisualBasic.FileIO.TextFieldParser _
            (openFile.FileName)//the csv path

                'Specify that reading from a comma-delimited file'
                MyReader.TextFieldType = FileIO.FieldType.Delimited
                MyReader.SetDelimiters(",")
                Dim currentRow As String()
                While Not MyReader.EndOfData
                    Try
           currentRow = MyReader.ReadFields()
           With DataGridView1.Rows.Add(currentRow) 'Add new row to data gridview'
                        End With
                    Catch ex As Microsoft.VisualBasic.FileIO.MalformedLineException
                        MsgBox("Line " & ex.Message & "is not valid and will be skipped.")
                    End Try
                End While
            End Using

那么有人可以告诉我怎么做吗? 谢谢。

【问题讨论】:

    标签: vb.net csv datagridview


    【解决方案1】:

    可能很简单,读取第一行并丢弃它,然后开始从文件中读取真实数据

    Using MyReader As New TextFieldParser(openFile.FileName)
       MyReader.TextFieldType = FileIO.FieldType.Delimited
       MyReader.SetDelimiters(",")
       Dim currentRow As String()
       ' Read the first line and do nothing with it
       If Not MyReader.EndOfData Then
           currentRow = MyReader.ReadFields()
       End If
       While Not MyReader.EndOfData
          Try
              ' Read again the file
              currentRow = MyReader.ReadFields()
              DataGridView1.Rows.Add(currentRow(1), currentRow(2),currentRow(3)) 
          Catch ex As Microsoft.VisualBasic.FileIO.MalformedLineException
              MsgBox("Line " & ex.Message & "is not valid and will be skipped.")
          End Try
      End While
    End Using
    

    EDIT 看到您在下面的评论,然后我更改了添加行的行,仅在位置 1,2 和 3 添加字符串。这当然与添加到 DataGridView 的列不同.目前尚不清楚是否要将这些列更改为仅包含这 3 个字段。如果您仍然想要网格中的 ID 和 AGE 列,您可以更改添加到

    DataGridView1.Rows.Add("", currentRow(1), currentRow(2),currentRow(3), "") 
    

    【讨论】:

    • 您好,感谢您的回复,请问读取每一行中的目标数据:2nd, 3rd, 4th 怎么样?
    • 对不起,我不明白。网格是否填充了数据?您的意思是现在要从网格中读取该数据吗?
    • 嗨,是的,网格中填充了来自 csv 的全部数据,但这不是我想要的,我希望它只填充来自的“姓名”、“地址”和“电子邮件” csv 文件中的每一行都没有“id”和“age”
    • 好吧,但是您需要更改 DataGridView 中的列,因为您在其中包含 ID 和 AGE。但是,currentRow 是一个由 5 个字符串组成的数组,其中索引 0 处的第一个字符串是 ID,索引 4 处的最后一个字符串是 AGE,您可以直接添加索引 1、2、3 处的字符串,请参阅我的更新答案跨度>
    • 您好,先生,当我尝试您的代码时,我似乎收到了一条错误消息。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-05-07
    • 1970-01-01
    • 1970-01-01
    • 2021-01-16
    • 2019-10-28
    • 2021-12-11
    相关资源
    最近更新 更多