从CSV文件构建DataTable?ASP.net VB

law*_*tog 1 xml vb.net csv asp.net datatable

我有一个函数可以读取CSV文件并构建数据表.我不会声称这是我自己的因为它不是.我不记得我从哪里得到它,但它是来自几个不同来源的组合.

我的功能正常工作,直到我需要处理一些在值中带有","逗号的CSV.任何人都可以帮我解决这个问题吗?

非常感谢 ..

示例CSV文件

    FirstName, LastName, Comment, "address, just city",
    John, Smith, "you are very good, but ugly", London,
    Britney, Spear, "I am a singer, and beautiful", New York,
Run Code Online (Sandbox Code Playgroud)

我的功能

    Public Function BuildDataTable() As DataTable

    Dim myTable As DataTable = New DataTable("MyTable")
    Dim i As Integer
    Dim myRow As DataRow
    Dim fieldValues As String()
    Dim myReader As IO.StreamReader
    Dim csv2xml As New csv2xml

    Try
        'Open file and read first line to determine how many fields there are.
        myReader = File.OpenText(_fileFullPath)
        fieldValues = myReader.ReadLine().Split(_seperator)
        'Create data columns accordingly
        If _hasheader = False Then
            For i = 0 To fieldValues.Length() - 1
                myTable.Columns.Add(New DataColumn("Column(" & i & ")"))
            Next
        Else
            'if the file has header, take the first row as header for datatable
            For i = 0 To fieldValues.Length() - 1
                myTable.Columns.Add(New DataColumn(fieldValues(i).Replace(" ", "")))
            Next
        End If

        'Adding the first line of data to data table
        myRow = myTable.NewRow

        'if the csv file has not got a column header. defined by radio button list on first page by user
        'if csv file has header, then not need to read the first line
        If _hasheader = False Then
            For i = 0 To fieldValues.Length() - 1
                myRow.Item(i) = fieldValues(i).ToString
            Next
            myTable.Rows.Add(myRow)
        End If

        'Now reading the rest of the data to data table
        While myReader.Peek() <> -1
            fieldValues = myReader.ReadLine().Split(_seperator)
            myRow = myTable.NewRow
            For i = 0 To fieldValues.Length() - 1
                myRow.Item(i) = fieldValues(i).Trim.ToString
            Next
            'check if there are empty rows in csv, ignore empty rows

            If Not csv2xml.AreAllColumnsEmpty(myRow) = True Then
                myTable.Rows.Add(myRow)
            End If

        End While
    Catch ex As Exception
        'MsgBox("Error building datatable: " & ex.Message)
        Dim oError As ErrorLog = New ErrorLog
        oError.LogError(_strWebsiteName, _
            loginID, _
            ex.Source.ToString, _
            ex.Message.ToString, _
            , _
            ex.StackTrace.ToString)
        oError = Nothing
        Return New DataTable("Empty")
        'Server.Transfer(CustomErrorPage)
    Finally
        csv2xml = Nothing
        myRow = Nothing
    End Try
    myReader.Close()
    Return myTable

    End Function
Run Code Online (Sandbox Code Playgroud)

Jim*_*eon 6

只需使用VB TextFieldParser类,并将HasFieldsEnclosedInQuotes属性设置为True: