2012-01-13 34 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