如何使用Visual Basic在Datagridview中一次导入多个文件

时间:2015-03-03 21:49:01

标签: datagridview

我是Visual Basic的新手。我有一个文本框,其中我有多个文件的路径,比如四个文件。每个路径都在同一文本框中的新行中。我想通过一个单击按钮将这些CSV或文本文件的数据导入到数据网格视图中。我一直在努力实现这一目标,但到目前为止还没有成功。问题是如果我在文本框中有一个文件的路径,我的应用程序将成功运行。我有以下代码用于单击按钮以导入数据。

Dim TextFileReader As New Microsoft.VisualBasic.FileIO.TextFieldParser(txtFilesPathHo.Text)

    TextFileReader.TextFieldType = FileIO.FieldType.Delimited
    TextFileReader.SetDelimiters(",", ";")

    'Now Create an empty table in the computer memory and declare necessary variables

    Dim TextFileTable As DataTable = Nothing


    Dim Column As DataColumn
    Dim Row As DataRow
    Dim UpperBound As Int32
    Dim ColumnCount As Int32
    Dim CurrentRow As String()




    'Use While Loop to readfile and create table

    While Not TextFileReader.EndOfData

        Try

            CurrentRow = TextFileReader.ReadFields()

            If Not CurrentRow Is Nothing Then

                'Check if the datatable has been created

                If TextFileTable Is Nothing Then

                    TextFileTable = New DataTable("TextFileTable")

                    'Get number of columns

                    UpperBound = CurrentRow.GetUpperBound(0)

                    'Create new columns in the datatable

                    For ColumnCount = 0 To UpperBound

                        Column = New DataColumn()

                        Column.DataType = System.Type.GetType("System.String")
                        Column.ColumnName = "Column" & ColumnCount
                        Column.Caption = "Column" & ColumnCount
                        Column.ReadOnly = False
                        Column.Unique = False
                        TextFileTable.Columns.Add(Column)

                    Next

                End If


                Row = TextFileTable.NewRow
                For ColumnCount = 0 To UpperBound
                    Row("Column" & ColumnCount) = CurrentRow(ColumnCount).ToString
                Next

                TextFileTable.Rows.Add(Row)

            End If


        Catch ex As Microsoft.VisualBasic.FileIO.MalformedLineException
            MsgBox("Line" & ex.Message & "is not valid and will be skipped")

        End Try


    End While


    'Now stop textfile reader
    TextFileReader.Dispose()

    'Copy data to datagridview

    DataGridViewHO.DataSource = TextFileTable




End Sub

任何人都可以帮助我在上面的代码中进行哪些更改,只需点击一下即可加载多个文件。

1 个答案:

答案 0 :(得分:0)

我试过这段代码并且它有效,希望可以帮助

Private Sub Button1_Click(ByVal sender As System.Object,ByVal e As System.EventArgs)Handles Button1.Click '使用多个选择的Openfiledialog

    OpenFileDialog1.InitialDirectory = "c:\temp\"
    OpenFileDialog1.Filter = "CSV files (*.csv)|*.CSV"
    OpenFileDialog1.FilterIndex = 2
    OpenFileDialog1.RestoreDirectory = True
    Me.OpenFileDialog1.Multiselect = True

    If (OpenFileDialog1.ShowDialog() = Windows.Forms.DialogResult.OK) Then
        For x = 0 To OpenFileDialog1.FileNames.Count
            Try
                Dim fName As String = ""
                fName = OpenFileDialog1.FileNames(x)
                Me.TextBox1.Text += fName

                Dim TextLine As String = ""

                Dim SplitLine() As String

                If System.IO.File.Exists(fName) = True Then

                    Dim objReader As New System.IO.StreamReader(fName)

                    Do While objReader.Peek() <> -1

                        TextLine = objReader.ReadLine()

                        SplitLine = Split(TextLine, "|")

                        Me.DataGridView1.Rows.Add(SplitLine)

                    Loop

                Else

                    MsgBox("File Does Not Exist")

                End If
            Catch ex As Exception

            End Try
        Next
    MsgBox("Total files loaded = " & OpenFileDialog1.FileNames.Count)
    End If

End Sub