在 vb.net 中读取 csv 文件

use*_*234 1 vb.net csv

我在 vb.net 中使用此代码读取 csv 文件:

filename = TextBox1.Text
        FileOpen(1, filename, OpenMode.Input)

        'first row contains header information, therefore read it in, but ignore it
        dummy = LineInput(1)

        While Not EOF(1)

            Input(1, dialcode)
            Input(1, chargecode)
            Input(1, description)
            Input(1, mincharge)
            Input(1, onpeak)
            Input(1, offpeak)
            Input(1, weekendonpeak)
            Input(1, weekendoffpeak)
            Input(1, onpeakconnect)
            Input(1, offpeakconnect)
            Input(1, weekendonpeakconnect)
            Input(1, weekendoffpeakconnect)
End While
Run Code Online (Sandbox Code Playgroud)

这工作很好

但我现在有一个不同的 CSV 来读入,当我在记事本中打开 CSV 文件时,它在每一行的末尾都有一个 , 所以它没有读取每一行,因为 vb.net 不确定一行何时结束

Joh*_*ner 15

.Net 在TextFieldParser 中有一个内置的 CSV 阅读器。它将为您处理额外的逗号或带引号的分隔符之类的事情。例如,你可以这样做:

Dim dialcode As String
Dim chargecode As String
Dim mincharge As String

Dim tfp As New TextFieldParser("Z:\temp\test.csv")
tfp.Delimiters = New String() {","}
tfp.TextFieldType = FieldType.Delimited


tfp.ReadLine() ' skip header
While tfp.EndOfData = False
    Dim fields = tfp.ReadFields()
    dialcode = fields(0)
    chargecode = fields(1)
    mincharge = fields(2)
    Console.WriteLine(String.Format("{0} - {1} - {2}", dialcode, chargecode, mincharge))
End While
Run Code Online (Sandbox Code Playgroud)