Excel 如何在不使用存储过程等的情况下通过Ado.net从模式中查找表名?

Excel 如何在不使用存储过程等的情况下通过Ado.net从模式中查找表名?,excel,ado.net,schema,Excel,Ado.net,Schema,我正在尝试使用datatable从excel文件中读取数据 命令select*from[Sheet1$]工作正常,但如果excel文件中的工作表名称不同,则会出现错误 因此,现在我需要知道如何使用ADO.Net在模式中找到可用的表名。以下函数将返回给定位置的表名例如excel工作表或返回空白 Private Function GetTableName(ByVal ConnectionString As String, ByVal TableNumber As Integer) As String

我正在尝试使用datatable从excel文件中读取数据

命令select*from[Sheet1$]工作正常,但如果excel文件中的工作表名称不同,则会出现错误


因此,现在我需要知道如何使用ADO.Net在模式中找到可用的表名。

以下函数将返回给定位置的表名例如excel工作表或返回空白

Private Function GetTableName(ByVal ConnectionString As String, ByVal TableNumber As Integer) As String
    Dim i As Integer
    Dim dtXlsSchema As DataTable
    Dim myConn As New OleDbConnection
    myConn.ConnectionString = ConnectionString
    myConn.Open()
    dtXlsSchema = myConn.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, _
                      New Object() {Nothing, Nothing, Nothing, "TABLE"})


    If TableNumber > dtXlsSchema.Rows.Count Then
        Return ""
    Else
        Return dtXlsSchema.Rows(TableNumber - 1).Item("Table_Name").ToString
    End If
End Function