2017-07-17 74 views
0

我正在構建一個小型應用程序,旨在簡化顯示在.txt文件中的信息並顯示在Excel(.CSV)中。將TXT轉換爲已過濾的CSV

我已經得到了一個字符串的信息,我打算使用可用於金塊的CSVExport庫來構建excel文件。問題是我需要過濾和處理信息。 數據具有以下格式:

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. 

我打算在第一線(報頭)隔離,保持其值,制訂了被稱爲「控制器」細胞 - >「MTC1」,日期第一收集「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 

...

我的問題是,我將用它來濾除信息的方法。 我一直在研究一些方法,但由於缺乏經驗,我似乎無法選擇正確的方法。現在建立一個列表,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"; 

      } 
     } 
    } 
} 

現在,有文件的格式,你將如何爲表篩選值?

謝謝。

+0

不清楚你想要做什麼。你有一個csv,你正在閱讀,然後你想過濾?你不知道列已經? – FortyTwo

+1

您可以添加所需輸出文件格式的示例嗎? –

+0

我已經更新了這個問題! – kohhworlwide

回答

1

我想你應該看看Split方法(msdn docs)。如果你需要在內存中構建一個字符串

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 
    } 
} 

StringBuilder是有幫助的:你可以這樣使用它。在你的情況下,你似乎需要一個StreamWriter對象。

+0

謝謝,這是一個很大的幫助。 – kohhworlwide

+0

@kohhworlwide請參閱[SO瀏覽](https://stackoverflow.com/tour)。如果答案是有用的,你可以upvote它,否則downvote。這將幫助其他用戶找到很好的答案 –