我有這段代碼來讀取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