【问题标题】:read csv file in vb.net在 vb.net 中读取 csv 文件
【发布时间】:2014-11-07 00:05:15
【问题描述】:

我在 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

这很好

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

【问题讨论】:

  • 您“使用此代码”还是“编写此代码”?
  • 编写并使用它(:
  • 您为什么不直接阅读并丢弃该行的其余部分?

标签: vb.net csv


【解决方案1】:

.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

【讨论】:

    【解决方案2】:
    Private Sub ParseCSV(ByVal path As String)
        Dim inidex As Integer
        Dim oxygenSaturation As Object
        Dim pulse As Object
    
        Using MyReader As New Microsoft.VisualBasic.FileIO.
        TextFieldParser(path)
    
            MyReader.TextFieldType = Microsoft.VisualBasic.FileIO.FieldType.Delimited
            MyReader.Delimiters = New String() {","}
    
            MyReader.ReadLine() ' skip header
            'Loop through all of the fields in the file. 
            'If any lines are corrupt, report an error and continue parsing. 
            While Not MyReader.EndOfData
                Try
                    'currentRow = MyReader.ReadFields()
                    Dim fields = MyReader.ReadFields()
    
                    inidex = fields(0)
                    oxygenSaturation = fields(28)
                    pulse = fields(30)
    
                    Dim a As String
                    a = "Index = " & inidex & "oxygenSaturation = " & oxygenSaturation & "pulse = " & pulse
                    MessageBox.Show(a, "Has been Changed", MessageBoxButtons.OKCancel)
    
                    ' Include code here to handle the row.
                Catch ex As Microsoft.VisualBasic.FileIO.MalformedLineException
                    MsgBox("Line " & ex.Message &
                    " is invalid.  Skipping")
                End Try
            End While
        End Using
    End Sub
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-10-02
      • 2010-11-27
      • 2012-09-28
      • 2016-04-09
      • 2023-03-22
      • 2011-07-24
      • 2022-01-09
      • 2015-07-21
      相关资源
      最近更新 更多