在vb.net中读取csv文件 [英] read csv file in vb.net

查看:729
本文介绍了在vb.net中读取csv文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I use this code in vb.net for reading in csv files:

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不确定何时一行结束

but i now have a different CSV to read in, and it has a , at the end of each line when i open the CSV file in notepad, so its not reading each row in because vb.net is unsure when a row ends

推荐答案

.Net在

.Net has a built in CSV reader in the TextFieldParser. It will handle things like extra commas or quoted delimiters for you. For example, you could do this:

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

这篇关于在vb.net中读取csv文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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