从目录(包括所有子目录)中获取文件并打开文件

时间:2019-03-21 09:42:04

标签: excel vb.net

我在VB.NET中有一个小应用程序,当单击DatagriedView的按钮时,应在目录中搜索并打开一个excel文件,该目录中包含按年份排序的几个子目录(2019 .... 2018 ......)。这些子目录中有几个用数字命名的excell文件(5673455.xlsx ..... 11122232.xlsx ...)。该文件的名称应放在文本框中,单击按钮时应打开文件。

我如何做到这一点,从而不必在文本框中输入文件的全名并打开它?例如,完整文件名为“ 23456_B_D.xlsx”,而仅将文本框置于“ 23456”将打开文件。如果文件扩展名为xls而不是xlsx怎么办?

这是我到目前为止的代码。但是我遇到一个错误:“类型'一维字符串数组'的值不能转换为'字符串'

Private Sub DataGridView_descricao_ColumnAdded(sender As System.Object, e As DataGridViewCellEventArgs) _
                                       Handles DataGridView_descricao.CellContentClick
    Dim senderGrid = DirectCast(sender, DataGridView)

    If TypeOf senderGrid.Columns(e.ColumnIndex) Is DataGridViewButtonColumn AndAlso
       e.RowIndex >= 0 Then

        Dim filePaths As String() = Directory.GetFiles("C:\SALES", "*.xlsx", SearchOption.AllDirectories)
        Dim fileName As String = TextBox_sa.Text + ".xlsx"
        System.Diagnostics.Process.Start(Path.Combine(filePaths, fileName))
    End If

End Sub

谢谢

1 个答案:

答案 0 :(得分:0)

我尝试过

    Private Sub DataGridView_descricao_ColumnAdded(sender As System.Object, e As DataGridViewCellEventArgs) _
                                       Handles DataGridView_descricao.CellContentClick
    Dim senderGrid = DirectCast(sender, DataGridView)

    If TypeOf senderGrid.Columns(e.ColumnIndex) Is DataGridViewButtonColumn AndAlso
       e.RowIndex >= 0 Then

        Dim filePaths As String() = Directory.GetFiles("C:\SALES", "*.xlsx", SearchOption.AllDirectories)
        Dim fileName As String = TextBox_sa.Text + ".xlsx"

        For Each path As String In filePaths
                Process.Start(New ProcessStartInfo With {
                    .FileName = path,
                    .UseShellExecute = True,
                    .Verb = "open"
                })
            Next
    End If

End Sub

但这会打开几个文件!!!