我正在尝试使用datatable从excel文件中读取数据。
命令“select * from [Sheet1 $]”工作正常,但如果excel文件的工作表具有不同的名称,则会出错。
所以现在我需要知道如何使用ADO.Net找到架构中可用的表名。
答案 0 :(得分:0)
以下函数将返回给定位置的表名(例如,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