Setup Environment:
I'm using vb.net to develop an Excel Add-In with .NET Framework 4.
My goal:
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"
If the output is more or less standard, lets say you know that the lines you need start as RunStart, RunTag, RunDuration and the numeric ones, then you can open the csv file through regular Stream, read a line(the output you've shown, convert it to string and Check if it contains the data you need. If it does, split it on the comma and output however you need. If you need a code for this, let me know, I am a bit strapped on time right now, but can create it later today or tomorrow.
Hope this helps.
Also, if you can paste the CSV here, as text, I can use it to test.
This is how I would do it, i think. Let me know if you have the code to import it to excel.
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Diagnostics;
namespace CSVParser
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
//location of the file
System.IO.StreamReader _fs = new StreamReader("../../TextFile1.txt");
int _result;
while (!_fs.EndOfStream)
{
string _filestring = _fs.ReadLine();
string[] _csvstring = _filestring.Split(Convert.ToChar(","));
if(_csvstring.Length > 1)
{
//check if the string begins with run maybe ignore the first run, since you don't need it
if (_csvstring[0].StartsWith("Run") && !_csvstring[0].Equals("Run"))
{
Debug.WriteLine(_csvstring[0] + "-" + _csvstring[1]);
}
//check for the datapoints based ont heir numeric values
if(int.TryParse(_csvstring[0],out _result))
{
if (_result <= 10)
{
Debug.WriteLine(_csvstring[0] + "-" + _csvstring[1] + "-" + _csvstring[2]);
}
}
}
}
_fs.Close();
}
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.