2012-01-13 74 views
0

Example Data Link - 抱歉,粘貼時無法正確格式化。如何在VB.NET中解析這個文本文件?

1. 
Dec 01, 2011 
06:00:00 AM 
Dec 01, 2011 
07:05:00 AM 
65 
2.11 
2. 
Dec 01, 2011 
06:00:00 PM 
Dec 01, 2011 
07:05:00 PM 
65 
2.11 
3. 
Dec 02, 2011 
06:05:00 AM 
Dec 02, 2011 
07:05:00 AM 
60 
1.95 

我想每個單獨行有自己的一席之地陣列中的數據表或,但我似乎無法得到它的正常工作。結尾字符必須有不同的東西嗎?

代碼如下。

Dim strOutput As String = "" 

    '' Demo Data 
    'Dim strData As String = "59. Dec 01, 2011 06:05:00 PM Dec 01, 2011 10:05:00 PM 240 80.00" 
    'strOutput = +FormatRow(strData) 
    '' Demo Data 

    Dim sFileName As String = OpenFileDialog.FileName 
    If My.Computer.FileSystem.FileExists(sFileName) Then 
     Dim srFileReader As System.IO.StreamReader 
     Dim sInputLine As String 
     srFileReader = System.IO.File.OpenText(sFileName) 
     sInputLine = srFileReader.ReadLine() 

     Do Until sInputLine Is Nothing 
      'strOutput = +FormatRow(sInputLine) 
      Dim title As String = srFileReader.ReadLine() 
      Dim startTime As DateTime = srFileReader.ReadLine() & " " & srFileReader.ReadLine() 
      Dim endTime As DateTime = srFileReader.ReadLine() & " " & srFileReader.ReadLine() 
      Dim timeSpan As TimeSpan = endTime.Subtract(startTime) 
      Dim minutesTotal As Integer = timeSpan.TotalMinutes 
      ' Burn Minutes 
      srFileReader.ReadLine() 
      Dim billMinutes As Integer = minutesTotal 
      Dim billTotal As Double = srFileReader.ReadLine() 

      strOutput += "" 
     Loop 

輸出像

12/1/2011 6:00:00 AM 12/1/2011 7:05:00 AM 65 65  2.11 
+1

請格式化該數據文件內容並將其添加到您的文章。 – adatapost 2012-01-13 03:42:10

+0

你是什麼意思格式化它?當我複製並粘貼它時,stackoverflow將更改格式。我會嘗試粘貼一些,它只是一個純文本文件。 – Landmine 2012-01-13 03:44:39

+1

輸出格式是什麼? – 2012-01-13 03:44:48

回答

4

你一次讀取,而文本文件,我想File.ReadAllLines將是不錯的選擇。

Dim str 
Dim fileName = "C:\SampleData.txt" 

Dim lines() = File.ReadAllLines(fileName) 

For i = 0 To lines.GetUpperBound(0) Step 7 
    str = String.Format("{0}{1}{2}{3}{4}{5}", 
        lines(i), lines(i + 1), lines(i + 2), lines(i + 3), 
        lines(i + 4), lines(i + 5)) 
    Console.WriteLine(str) 
Next 
+0

更新輸出格式,認爲我可以給你發送文件?因爲我得到了很多相同數據的行。 – Landmine 2012-01-13 04:56:44