因此,在做了一些研究后,我能够找到获取CSV文件所需的格式 Subject,Start Date,Start Time,End Date,End Time,All Day Event,Description,Location,Private 问题是,我正在使用的CSV导出格式或顺序不正确,收集该信
Subject,Start Date,Start Time,End Date,End Time,All Day Event,Description,Location,Private
问题是,我正在使用的CSV导出格式或顺序不正确,收集该信息的最佳方式是什么?这是我的一些来源.
名称,用户名,行类型,开始日期,开始时间,结束时间,结束日期,段开始日期,类型
“Smith,John J”,jjs,Shift,5/29 / 2011,9:30,17:30,5 / 29 / 2011,5 / 29/2011,Regular
“Smith,John J”,jjs,Shift,5/30 / 2011,13:30,17:30,5 / 30 / 2011,5 / 30/2011,Regular
Dim Name As String = ""
Dim UserName As String = ""
Dim Data As String = """Smith, John J"",jj802b,Shift,5/29/2011,9:30,17:30,5/29/2011,5/29/2011,Transfer"
For r As Integer = 1 To 10
Name = Data.Substring(0, Data.LastIndexOf(""""))
Data = Data.Remove(0, Data.LastIndexOf(""""))
UserName = Data.Substring(Data.LastIndexOf(""""), ",")
Next
以下是解决方案
Dim Name As String = ""
Dim UserName As String = ""
Dim Data As String = """Smith, John J"",jj802b,Shift,5/29/2011,9:30,17:30,5/29/2011,5/29/2011,Transfer"
For r As Integer = 1 To 10
Dim DataArr() As String = DecodeCSV(Data) 'Use DecodeCSV function to regex split the string
Name = DataArr(0) 'Get First item of array as Name
UserName = DataArr(1) 'Get Second item of array as UserName
Next
DecodeCSV by Tim的优秀代码
Public Shared Function DecodeCSV(ByVal strLine As String) As String()
Dim strPattern As String
Dim objMatch As Match
' build a pattern
strPattern = "^" ' anchor to start of the string
strPattern += "(?:""(?<value>(?:""""|[^""\f\r])*)""|(?<value>[^,\f\r""]*))"
strPattern += "(?:,(?:[ \t]*""(?<value>(?:""""|[^""\f\r])*)""|(?<value>[^,\f\r""]*)))*"
strPattern += "$" ' anchor to the end of the string
' get the match
objMatch = Regex.Match(strLine, strPattern)
' if RegEx match was ok
If objMatch.Success Then
Dim objGroup As Group = objMatch.Groups("value")
Dim intCount As Integer = objGroup.Captures.Count
Dim arrOutput(intCount - 1) As String
' transfer data to array
For i As Integer = 0 To intCount - 1
Dim objCapture As Capture = objGroup.Captures.Item(i)
arrOutput(i) = objCapture.Value
' replace double-escaped quotes
arrOutput(i) = arrOutput(i).Replace("""""", """")
Next
' return the array
Return arrOutput
Else
Throw New ApplicationException("Bad CSV line: " & strLine)
End If
End Function
