Visual Basic如何读取CSV文件并在数据网格中显示值? [英] Visual Basic How do I read a CSV file and display the values in a datagrid?

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

问题描述

我使用VB 2005,如何打开CSV文件并读取列/行并在datagrid中显示值?



CSV文件示例:
jsmith,jsmith @ hotmail.com



然后我想对每一行,即每个用户执行一个操作,我该如何做?



感谢


div class =h2_lin>解决方案

使用 TextFieldParser ,即内置到 VB中。净。 Google找到我此示例

 使用MyReader作为新的Microsoft.VisualBasic.FileIO.TextFieldParser _ 
(C:\test\info .csv)

指定从逗号分隔的文件中读取
MyReader.TextFieldType = FileIO.FieldType.Delimited
MyReader.SetDelimiters(,)

Dim currentRow As String()
While Not MyReader.EndOfData
Try
currentRow = MyReader.ReadFields()
With Me.dgvReport.Rows
.Add(currentRow)'将新行添加到数据网格视图'
结束于
Catch ex As Microsoft.VisualBasic.FileIO.MalformedLineException
MsgBox(Line& ex.Message& ; _
无效,将被跳过。)
结束尝试
结束而
结束使用


I'm using VB 2005, how do I open a CSV file and read the columns/rows and display the values in a datagrid?

CSV file example: jsmith,jsmith@hotmail.com

I then want to perform an action on each row i.e. each user, how would I do this?

I'm a newbie as you can tell but happy to learn.

Thanks

解决方案

Use the TextFieldParser that's built into VB.NET. Google found me this example

Using MyReader As New Microsoft.VisualBasic.FileIO.TextFieldParser _
  ("C:\test\info.csv")

  '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 Me.dgvReport.Rows
        .Add(currentRow) 'Add new row to data grid view'
     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

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

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