2012-04-30 12 views
2

私は次のようなデータを含むテキストファイル名list.txtを持っています。ストリームリーダー経由でテキストファイルを読む

AC-No.     Name    Time  State New State Exception 

    100   ZULFIQUAR 09/04/2012 01:53 PM   C/In    Invalid 
    100   ZULFIQUAR 10/04/2012 01:39 PM   C/In    Invalid 
    100   ZULFIQUAR 11/04/2012 01:38 PM   C/In    Invalid 
    1002    SAQIB 09/04/2012 10:42 PM   C/In  C/Out  OK 
    1002    SAQIB 10/04/2012 08:01 AM   C/In      OK 
    1002    SAQIB 10/04/2012 10:28 PM   C/In  C/Out  OK 
    1002    SAQIB 11/04/2012 09:25 AM   C/In      OK 
    1002    SAQIB 11/04/2012 10:40 PM   C/In  C/Out  OK 
    1002    SAQIB 12/04/2012 07:15 AM   C/In      OK 
    1002    SAQIB 12/04/2012 11:12 PM   C/In  C/Out  OK 
    1002    SAQIB 13/04/2012 07:23 AM   C/In      OK 
    1002    SAQIB 13/04/2012 10:53 PM OverTime Out    Invalid 
    1002    SAQIB 14/04/2012 06:58 AM OverTime Out    Invalid 
    1002    SAQIB 15/04/2012 10:50 PM   C/In    Invalid 
    1002    SAQIB 16/04/2012 07:09 AM   C/In      OK 
    1002    SAQIB 17/04/2012 10:36 PM   C/In  C/Out  OK 
    1002    SAQIB 18/04/2012 07:21 AM   C/In      OK 
    1002    SAQIB 18/04/2012 10:46 PM   C/In  C/Out  OK 
    1002    SAQIB 19/04/2012 06:32 AM   C/In      OK 
    1002    SAQIB 19/04/2012 10:47 PM   C/In  C/Out  OK 

今私は行全体を3つの列(AC-No、Name.Time)で選択し、そのデータソースにdatagridviewを与えなければなりません。 私は以下のコードを使用していますが、運はありません。

Dim tbl As New DataTable("mytable") 
     tbl.Columns.Add("col1", GetType(String)) 
     'tbl.Columns.Add("col2", GetType(String)) 
     'tbl.Columns.Add("col3", GetType(Integer)) 
     Dim sFilename As String = TextBox1.Text 
     Dim myStream As System.IO.StreamReader = New System.IO.StreamReader(sFilename) 
     Dim line As String 
     Dim aRow As DataRow 
     Do 
      line = myStream.ReadLine() 
      If line Is Nothing Then 
       Exit Do 
      End If 
      Dim sAry As String() = Split(line, " ") 
      aRow = tbl.NewRow 
      aRow(0) = sAry(0) 
      'aRow(1) = sAry(1) 
      ' aRow(2) = sAry(2) 
      tbl.Rows.Add(aRow) 
     Loop 
     myStream.Close() 
     DataGridView1.DataSource = tbl 

私を助けてください。

+1

ですか? –

答えて

4
Dim delimiter As String = "," 
Dim fileName As String = "c:\file.txt" 

Dim sr As New StreamReader(fileName) 

Try 
    While sr.Peek() >= 0 
     Dim r As String = sr.ReadLine() 
     Dim items As String() = r.Split(delimiter.ToCharArray()) 
    End While 
Finally 
    sr.Close() 
End Try 
+0

ありがとうRajeesh。まだ成功していません。実際には、私はスペースがないデータを持っているので、私はSQLテーブル03の列に列全体を挿入する必要があるので、行の束とSQLテーブルの03の列に挿入されます。 –

+0

をご確認ください。ありがとうございました。 –

0

ちょうど提案、mystream.readtoend()を試してみて、あなたのuはそれをデバッグして、正しい結果を得るまで遊ぶことができ、各行のための独立した文字列を持って、その後、すべてのvbnewlineでスライス。

+0

ご意見ありがとうございます。 –

2

1)列幅が固定されたファイルを表示していること、2)文字列ではなくDateTimeとして時間が必要であること、3)AC-Noであると仮定します。実際に文字列ではなく整数されています動作しませんどのような質問は、どのような

Imports System.Globalization 
Sub GetData() 
    Dim tbl As New DataTable("mytable") 
    tbl.Columns.Add("col1", GetType(String)) 
    tbl.Columns.Add("col2", GetType(String)) 
    tbl.Columns.Add("col3", GetType(DateTime)) 

    Dim sFilename As String = "C:\temp\testdata.txt" 

    Using myStream As System.IO.StreamReader = New System.IO.StreamReader(sFilename) 
     Dim ac As String 
     Dim username As String 
     Dim clocktime As DateTime 
     Dim culture As CultureInfo = CultureInfo.CreateSpecificCulture("en-GB") 

     Dim line As String 
     Dim aRow As DataRow 
     While Not myStream.EndOfStream 
      line = myStream.ReadLine().TrimEnd 
      ' check line is long enough to be valid 
      If line.Length >= 48 Then 
       ' take the first 8 characters as AC-No. 
       ac = line.Substring(0, 8).Trim 
       ' a valid line does not start with "AC-No" 
       If Not ac.StartsWith("AC-No") Then 
        ' extract the name and remove any extra spaces 
        username = line.Substring(9, 20).Trim 
        ' extract the time and convert it to a DateTime 
        clocktime = DateTime.Parse(line.Substring(29, 19), culture, DateTimeStyles.AllowWhiteSpaces) 
        aRow = tbl.NewRow 
        aRow(0) = ac 
        aRow(1) = username 
        aRow(2) = clocktime 
        tbl.Rows.Add(aRow) 
       End If 
      End If 

     End While 

    End Using 

    DataGridView1.DataSource = tbl 
    ' example of formatting the third column 
    DataGridView1.Columns(2).DefaultCellStyle.Format = "dd-MMM-yyyy HH:mm" 

End Sub 

(。示すように、あなたのデータをVB2010で働くように試験)​​

+0

ありがとうございました –

+1

@Asifkhanよろしくお願いします。それがあなたの質問に答えるなら、答えとしてマークしてください。 –

関連する問題