我想遍历所有文本文件并将数据插入到listview中

时间:2017-02-07 21:35:36

标签: vb.net vba

我正在制作一个odering系统程序,该程序是Order History功能,其中读取文件夹中的所有文本文件并将其插入ListView1。

对于ListView,我有三列。

  1. 日期 - 文本文件的名称
  2. 订单 - 位于文本文件
  3. 价格 - 也在文本文件中
  4. 示例 - 文本文件:“05/05/2017”<这是第一列。在文本文件中,项目和价格按“,”分隔。例如,“芯片,0.50”

    这是适用于Date列并插入第一行的项目和价格的代码,而不是之后的那些。

    ` Imports System.IO
    Public Class Form1
    
       Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
            Dim path As String = "C:\Users\Example\Desktop\1\Tesst\Dates\"
            For Each i As String In System.IO.Directory.GetFiles(path)
                Dim a, b, c As String
                a = System.IO.Path.GetFileNameWithoutExtension(i)
                b = System.IO.Path.GetFileName(i)
                c = System.IO.Path.GetFullPath(i)
                Dim arrCheck(3) As String
                ListView1.Items.Add(a)
                For Each j As String In System.IO.Directory.GetFiles(path)
                    Dim text As String = IO.File.ReadAllText(j)
                    arrCheck = Split(text, ",")
                    ListView1.Items(0).SubItems.Add(arrCheck(0))
                    ListView1.Items(0).SubItems.Add(arrCheck(1))
                Next
            Next
        End Sub
    End Class`
    

    如何制作它以使其适用于所有行而不仅仅是第一行。

    Picture Added

2 个答案:

答案 0 :(得分:0)

试试这个

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    Dim path As String = "C:\Users\Example\Desktop\1\Tesst\Dates\"
    For Each i As String In System.IO.Directory.GetFiles(path)
        Dim a, b, c As String
        a = System.IO.Path.GetFileNameWithoutExtension(i)
        b = System.IO.Path.GetFileName(i)
        c = System.IO.Path.GetFullPath(i)
        Dim arrCheck() As String

        Dim text As String = IO.File.ReadAllText(i)
        arrCheck = Split(text, ",")


        Dim NewItem as New ListViewItem(a)
        NewItem.SubItems.Add(arrCheck(0))
        NewItem.SubItems.Add(arrCheck(1))

        ListView1.Add(NewItem) 

    Next
End Sub

答案 1 :(得分:0)

试试这个:

Imports System.IO
Public Class Form1

   Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        Dim path As String = "C:\Users\Example\Desktop\1\Tesst\Dates\"
        Dim arrCheck(3) As String
        Dim lineNum As Integer = -1
        For Each i As String In System.IO.Directory.GetFiles(path)
            Dim a, b, c As String
            a = System.IO.Path.GetFileNameWithoutExtension(i)
            b = System.IO.Path.GetFileName(i) ' unused
            c = System.IO.Path.GetFullPath(i)
            For Each line As String In File.ReadLines(c)
                lineNum = lineNum + 1
                ListView1.Items.Add(a)
                arrCheck = Split(line, ",")
                ListView1.Items(lineNum).SubItems.Add(arrCheck(0))
                ListView1.Items(lineNum).SubItems.Add(arrCheck(1))
            Next
        Next
    End Sub
End Class

(喊出@CallumDA)