如何使用dynamic工作表名称导入Excel文件? (T-SQL)

我基本上设置了从T-SQL中从Excel导入到SQL的过程:

SELECT * FROM OPENROWSET('Microsoft.Jet.OLEDB.4.0','Excel 8.0;HDR=YES;Database=C:\report.xls','select * from [name555$]') 

名字555包含一个固定的名字,而555则是一个随机的三位数字。 当报告来到我的时候,有时候这个名字是555,有时候是439,390等

有没有办法指示SQL服务器(最好在T-SQL中,因为这正是我现在使用的)dynamic读取名称? 这是XLS文件中的唯一表格,但它

例如在VBA中,您可以使用该工作表作为name1 $名称或作为“sheet1 $”索引。 那么,我希望有人可以帮助这个:)

您可以查询工作簿的SCHEMA以检索TABLE(工作表)名称。 如果您的工作簿中只有一个工作表,则第一个有效的TABLE_NAME将成为工作表的名称。

下面给出(在VB.NET代码中)是一种帮助者的方法,我一直使用这种任务。 该函数的返回值将括号放在名称周围,以便在SQL语句中使用。

 ''' <summary> ''' Returns the name of the first worksheet in an Excel workbook. ''' </summary> ''' <param name="connectString">OleDb connection string for an Excel workbook.</param> ''' <returns></returns> ''' <remarks></remarks> Public Shared Function GetTableName(ByVal connectString As String) As String Dim dtSheets As New DataTable Dim tmp As String = "" Using cn As New OleDbConnection(connectString) cn.Open() dtSheets = cn.GetSchema("Tables") End Using For Each rw As DataRow In dtSheets.Rows 'Get the name of the first worksheet in the file that ends 'with a $. tmp = rw("TABLE_NAME") 'Check to see if the table name is surrounded by apostorphes. If tmp.StartsWith("'") And tmp.EndsWith("'") Then 'Remove the apostrophes. tmp = tmp.TrimEnd("'") tmp = tmp.TrimStart("'") End If If tmp.EndsWith("$") Then Exit For Else tmp = "" End If Next Return "[" & tmp & "]" End Function