Yes, instead of csvFields(1), change the (1) to the field number that corresponds to the field you're searching for. BTW... it's a 0 based array so if you're counting the column, subtract 1 from the actual column number you see in excel.
VB.NET - Read CSV file, look for specific data
Collapse
X
-
I get an out of range error when I tried that.
Here is what I updated it to.
Code:For Each line As String In System.IO.File.ReadAllLines("C:\Configuration.csv") csvFields = line.Split(",") If startCounting Then If csvFields(0) = String.Empty Then Exit For ElseIf csvFields(23) = "TRUE" Then trueCount += 1 End If ElseIf Not startCounting AndAlso csvFields.Length > 1 AndAlso csvFields(23) = "US_VoiceMailOn" Then startCounting = True End If
Comment
-
I looked at your file an you're getting the error because you're working with a screwed up CSV file. Whoever the developer is who wrote the code to output this should receive a very hard slap on their hand. Take a look at http://en.wikipedia.org/wiki/Comma-s...s_and_examples for best practices with CSV files. I work with banks and their back end systems and see this a lot. You basically have a CSV file with combined table outputs which makes it difficult to read and requires some fairly tricky code to handle it elegantly. So enough now with my rant. All you need to do is change the line:
Code:ElseIf Not startCounting AndAlso csvFields.Length => 23 AndAlso csvFields(23) = "US_VoiceMailOn" Then
Comment
-
Ok, I tried that and got the same error.
Code:
Code:For Each line As String In System.IO.File.ReadAllLines("C:\Configuration.csv") csvFields = line.Split(",") If startCounting Then If csvFields(0) = String.Empty Then Exit For ElseIf csvFields(23) = "TRUE" Then trueCount += 1 End If ElseIf Not startCounting AndAlso csvFields.Length >= 23 AndAlso csvFields(23) = "US_VoiceMailOn" Then startCounting = True End If Next Label4.Text = String.Format("{0} Users Licensed", trueCount)
Comment
-
I ran the following code against your CSV file and receive the following result:
#35 True Values
Code:Private Sub Button2_Click(sender As System.Object, e As System.EventArgs) Handles Button2.Click Dim startCounting As Boolean = False Dim trueCount As Integer = 0 Dim csvFields() As String For Each line As String In System.IO.File.ReadAllLines("C:\Configuration.csv") csvFields = line.Split(",") If startCounting Then If csvFields(0) = String.Empty OrElse csvFields.Length < 23 Then Exit For ElseIf csvFields(23) = "True" Then trueCount += 1 End If ElseIf Not startCounting AndAlso csvFields.Length >= 23 AndAlso csvFields(23) = "US_VoiceMailOn" Then startCounting = True End If Next MessageBox.Show(String.Format("#{0} True Values", trueCount)) End Sub
Comment
Comment