TXT转换为过滤的CSV

我正在构build一个小型应用程序,旨在简化显示在.txt文件中的信息并显示在Excel(.CSV)中。

我已经得到了一个string的信息,我打算使用Nugget中提供的CSVExport库来构buildexcel文件。 问题是我需要过滤和处理信息。 数据具有以下格式:

0001. 001.0001. 01. 3. 17. 07. 13. 12. 02.0002.V TT. .MTC1. 75.39.00.00.00.00.00.00.00.00. 00.00.00.00.00.00.00.00.00.00. 00.00.00.00.00.00.00.00.00.00. 00.00.00.00.00.00.00.00.00.00. 00.00.00.00.00.00.00.00.00.00. 00.00.00.00.00.00.00.00.00.00. 00.00.00.00.00.00.00.00.00.00. 00.00.00.00.00.00.00.00.00.00. 00.00.00.00.00.00.00.00.00.00. 00.00.00.00.00.00.00.00.00.00. 00.00.00.00.00.00.00.00.00.00. 00.00.00.00.00.00.00.00.00.00. 00.00.00.00.00.00.00.00.00.00. 00.00.00.00.00.00.00.00.00.00. 00.00.00.00.00.00.00.00.00.00. 00.00.00.00.00.00.00.00.00.00. 00.00.00.00.00.00.00.00.00.00. 00.00.00.00.00.00.00.00.00.00. 00.00.00.00.00.00.00.00.00.00. 00.00.00.00.00.00.00.00.00.00. 00.00.00.00.00.00.00.00.00.00. 00.00.00.00.00.00.00.00.00.00. 00.00.00.00.00.00.00.00.00.00. 00.00.00.00.00.00.00.00.00.00. 00.00.00.00.00.00.00.00.00.00. 00.00.00.00.00.00.00.00.00.00. 00.00.00.00.00.00.00.00.00.00. 00.00.00.00.00.00.00.00.00.00. 00.00.00.00.00.00.00.00.00.00. 00.00.00.00.00.00.00.00.00.00. 00.00.00.00.00.00.00.00.00.00. 00.00.00.00.00.00.00.00.00.00. 00.00.00.00.00.00.00.00.00.00. 00.00.00.00.00.00.00.00.00.00. 00.00.00.00.00.00.00.00.00.00. 00.00.00.00.00.00.00.00.00.00. 00.00.00.00.00.00.00.00.00.00. 00.00.00.00.00.00.00.00.00.00. 00.00.00.00.00.00.00.00.00.00. 00.00.00.00.00.00.00.00.00.00. 00.00.00.00.00.00.00.00.00.00. 00.00.00.00.00.00.00.00.00.00. 00.00.00.00.00.00.00.00.00.00. 00.00.00.00.00.00.00.00.00.00. 00.00.00.00.00.00.00.00.00.00. 00.00.00.00.00.00.00.00.00.00. 00.00.00.00.00.00.00.00.00.00. 00.00.00.00.00.00.00.00.00.00. 00.00.00.00.00.00.00.00.00.00. 00.00.00.00.00.00.00.00.00.00. 00.00.00.00.00.00.00.00.00.00. 00.00.00.00.00.00.00.00.00.00. 00.00.00.00.00.00.00.00.00.00. 00.00.00.00.00.00.00.00.00.00. 00.00.00.00.00.00.00.00.00.00. 00.00.00.00.00.00.00.00.00.00. 00.00.00.00.00.00.00.00.00.00. 00.00.00.00.00.00.00.00.00.00. 00.00.00.00.00.00.00.00.00.00. 00.00.00.00.00.00.00.00.00.00. 00.00.00.00.00.00.00.00.00.00. 00.00.00.00.00.00.00.00.00.00. 00.00.00.00.00.00.00.00.00.00. 00.00.00.00.00.00.00.16. 

我打算隔离第一行(Header),保留它的值,制定一个名为“Controller” – >“MTC1”的单元格,第一次收集的date“13.07.2017 – 14.00h”,“Type” – >“TT”

对于其余的信息,我打算把每个数据“75”,“39”,“00”……在第一次收集之后每隔5分钟一个单元格。

 Controller MTC1 Type TT Start 20/10/2016 15:45 20/10/2016 15:45 52 15:50 50 15:55 00 16:00 00 

我的问题是我要用来滤除信息的方法。 我一直在研究一些方法,但由于缺乏经验,我似乎无法select正确的方法。 build立一个列表,StringBuffer …

以下是我到目前为止所做的:

 namespace WindowsFormsApplication1 { public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void button1_Click(object sender, EventArgs e) { CsvExport myExport = new CsvExport(); OpenFileDialog openFileDialog1 = new OpenFileDialog(); int first=0; openFileDialog1.ShowDialog(); { String line = ""; string[] Results = new string[500]; String filepath = openFileDialog1.FileName; StreamReader sr = new StreamReader(filepath); int i = 0; while ((line = sr.ReadLine()) != null) { Results[i] += line; i++; } Console.Write(Results[0]); string csvpath = "C:\\counts.csv"; } } } } 

现在,有了这种格式的文件,你将如何过滤表的值?

谢谢。

我想你应该看看Split方法( msdn文档 )。 你可以像这样使用它:

 var sr = new StreamReader(filepath); string[] header = null; while (true) { var line = sr.ReadLine(); if (line == null) break; // End of file var lineParts = line.Split(" .".ToCharArray(), StringSplitOptions.RemoveEmptyEntries); if (header == null) { // save first line header = lineParts; // now header[13] is equal "MTC1" } else { // process other lines by one } } 

如果你需要在内存中build立一个string, StringBuilder会很有帮助。 在你的情况下,你似乎需要一个StreamWriter对象。