如何从CSV文件中获取行数据

时间:2013-09-05 11:54:29

标签: asp.net .net vb.net csv

我正在从CSV文件中读取数据,我的目标是只获取选定的列,但到目前为止,我只能循环获取标题。有人可以协助我循环直到我得到行吗?

导入System.IO 公共类Form1

Private Sub btn_import_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn_import.Click
    Call csvmanagement()

End Sub
Structure systemstructure

    Dim cGroupName As String
    Dim cTransactDate As String
    Dim cRow_ID As String
    Dim cFullName As String
End Structure


Public Sub csvmanagement()

    Dim systemspecs As New systemstructure
    'Dim raw, column As Integer
    Using csvparser As New FileIO.TextFieldParser _
    ("C:\Users\Eranji\Downloads\new.csv")

        Dim csvalue As String()

        csvparser.TextFieldType = Microsoft.VisualBasic.FileIO.FieldType.Delimited
        csvparser.Delimiters = New String() {","}

        csvalue = csvparser.ReadFields()

        systemspecs.cGroupName = csvalue(0)
        systemspecs.cTransactDate = csvalue(1)
        systemspecs.cRow_ID = csvalue(2)
        systemspecs.cFullName = csvalue(2)

        While Not csvparser.EndOfData

        End While
        csvalue = csvparser.ReadFields()

        End

        ListBox1.Items.Add(systemspecs.cFullName & Chr(9) & systemspecs.cGroupName & Chr(9) & systemspecs.cRow_ID & systemspecs.cTransactDate)
    End Using

End Sub

结束班

1 个答案:

答案 0 :(得分:4)

你只有ReadFields一次 ReadFields是一行
你需要循环结束

While Not MyReader.EndOfData
   csvalue = csvparser.ReadFields()
   ...
End 

TextFieldParser.ReadFields