Setup Environment:
I'm using vb.net to develop an Excel Add-In with .NET Framework 4.
My goal:
- Parse a csv file into memory using vb.net
- Once parsed, paste only specified values into an Excel worksheet
After researching, this link provided a good start:
The code I'm using right now relies on the TextFieldParserClass in .Net
Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
Dim FileName As String = "C:\Users\windows7User\Desktop\RecordedRuns\recordedrun92637777.csv"
'Use a collection of strings to retreive all values from the csv file
Dim recordedRunCollection As New System.Collections.Specialized.StringCollection
Dim afile As FileIO.TextFieldParser = New FileIO.TextFieldParser(FileName)
Dim CurrentRecord As String() ' this array will hold each line of data
afile.TextFieldType = FileIO.FieldType.Delimited
afile.Delimiters = New String() {","}
afile.HasFieldsEnclosedInQuotes = True
'parse the actual file
Do While Not afile.EndOfData
Try
'If current row has 2 fields, this stores 2 strings. If row has 3 fields, stores 3 strings.
CurrentRecord = afile.ReadFields
For Each storedField As String In CurrentRecord
'Take each field and add it to the collection
recordedRunCollection.Add(storedField)
Next
Catch ex As FileIO.MalformedLineException
Stop
End Try
Loop
'Run Start Time
MsgBox(recordedRunCollection(11))
'Run Duration
MsgBox(recordedRunCollection(13))
'Run Tag
MsgBox(recordedRunCollection(15))
'00:00
MsgBox(recordedRunCollection(55))
'00:15
MsgBox(recordedRunCollection(58))
'00:30
MsgBox(recordedRunCollection(61))
'00:45
MsgBox(recordedRunCollection(64))
'01:00
MsgBox(recordedRunCollection(67))
'01:15
MsgBox(recordedRunCollection(70))
'01:30
MsgBox(recordedRunCollection(73))
'01:45
MsgBox(recordedRunCollection(76))
'02:00
MsgBox(recordedRunCollection(79))
'02:15
MsgBox(recordedRunCollection(82))
End Sub
I have no control over how the csv file is exported. This is what it looks like:
This is the only data I need from it:
I'm hoping there is a better way to approach this, but I'm not sure how.
Could somebody provide a pointer on what the right method to use is? I'd really appreciate it.
Additional Notes:
Here is the csv file if you want to copy it:
SerialNumber,312315
Model,"DD"
MessageStore,""
Version,R0V2
Run,1
RunStartTime,24-Mar-14 08:39:40
RunDuration,9 minutes 15 seconds
RunTag,"Test1"
LoggingInterval,"15.0"
LeftScaleSerialNumber,233730
LeftScaleDatatype,Upper
LeftScaleUnits,""
LeftScaleUserFactor,""
LeftScaleUserOffset,"0.00000"
LeftScaleUserResolution,""
UpperModuleSerialNumber,25
UpperModuleModel,1KSI
UpperModuleMessageStore,""
UpperModuleUserspan,""
UpperModuleUnits,""
UpperModuleUserFactor,"10"
UpperModuleUserOffset,"0.00000"
UpperModuleUserResolution,"1"
UpperModuleVersion,R090007
UpperModuleCalDue,25-Jun-13
Point#,Time,LeftScaleReading
1, 00:00:00.0,"2179"
2, 00:00:15.0,"23603"
3, 00:00:30.0,"23573"
4, 00:00:45.0,"23564"
5, 00:01:00.0,"23590"
6, 00:01:15.0,"23573"
7, 00:01:30.0,"23525"
8, 00:01:45.0,"23564"
9, 00:02:00.0,"23537"
10, 00:02:15.0,"23506"
11, 00:02:30.0,"1657"