Visual Basic中如何阅读一个CSV文件,并在DataGrid中显示的值? [英] Visual Basic How do I read a CSV file and display the values in a datagrid?

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

问题描述

我用VB 2005年,我怎么打开一个CSV文件,并读取列/行,并在DataGrid中显示的值?

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文件示例: JSMITH,jsmith的@ hotmail.com

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.

感谢

推荐答案

使用的<一个href="http://msdn.microsoft.com/en-us/library/microsoft.visualbasic.fileio.textfieldparser.aspx">TextFieldParser这是内置到 VB.NET 。谷歌发现了我这个例子

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文件,并在DataGrid中显示的值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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