我有这段代码来读取CVS文件。它读取每一行,用分隔符','分隔每行,并将字段值存储在数组'strline()'中。只阅读vb.net中CSV文件的特定字段
如何仅从CSV文件中提取必填字段?
例如,如果我有CSV文件等
类型,组编号,序列号,行号,日期(换行) 0,管理员,3,345678,1,26052010(换行) 1 ,工作人员,5,78654,3,26052010
I只需要列组的值,序列号和日期。
在此先感谢您的任何想法。
Dim myStream As StreamReader = Nothing
' Hold the Parsed Data
Dim strlines() As String
Dim strline() As String
Try
myStream = File.OpenText(OpenFile.FileName)
If (myStream IsNot Nothing) Then
' Hold the amount of lines already read in a 'counter-variable'
Dim placeholder As Integer = 0
strlines = myStream.ReadToEnd().Split(Environment.NewLine)
Do While strlines.Length <> -1 ' Is -1 when no data exists on the next line of the CSV file
strline = strlines(placeholder).Split(",")
placeholder += 1
Loop
End If
Catch ex As Exception
LogErrorException(ex)
Finally
If (myStream IsNot Nothing) Then
myStream.Close()
End If
End Try
+1 string.split不仅错误,与状态机 – 2010-04-29 22:06:51
+1相比,速度更慢,用于在OP上标注重要警告。 – 2010-04-30 03:39:28
我同意这是一个关于String.Split和“等等的一般性警告,但这一切都取决于数据来自何处以及规范是什么。大多数时候我已经读取了CSV文件并使用了String。分裂,如果我结束了太多的列,我没有通过文件,并告诉文件的制作者修复他的数据。 – 2010-04-30 07:30:26