我需要分析一个CSV文件使用VB。NET使用CarriageReturn和LineFeed作为第三列包含逗号。CSV标题是ID,名称,说明。
Imports Microsoft.VisualBasic.FileIO
Public Class Form1
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim ID As String
Dim Element As String
Dim Description As String
Dim tfp As New TextFieldParser("Z:\Downloads\test.csv")
tfp.Delimiters = New String() {","}
tfp.TextFieldType = FieldType.Delimited
tfp.ReadLine() ' skip header
While tfp.EndOfData = False
Dim fields = tfp.ReadFields()
ID = fields(0)
Element = fields(1)
Description = fields(2)
CustomerBindingSource.DataSource = fields
End While
End Sub
End Class
字符串
2条答案
按热度按时间35g0bw711#
您可以使用
Microsoft.VisualBasic.FileIO
中的TextFieldParser
类。该类有助于读取结构化文本文件,参考以下代码以供参考:
字符串
5jdjgkvh2#
考虑到第一行是一个header,并且ID和Element都不能包含逗号,您可以使用蛮力:
字符串