无法使用GetOleSchemaTable()获得工作表名称

|| 我正在使用一些代码,这些代码使用OleDbConnection将数据从Excel文件加载到DataTable。当前,它默认为第一个工作表,但使用以下代码获取其名称:
string connectionString = @\"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=myFilename.xlsx;Extended Properties=\"\"Excel 12.0 Xml;HDR=YES\"\"\"

DataTable = connection.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, null);
if (schemaTable.Rows.Count > 0)
    return schemaTable.Rows[0][\"TABLE_NAME\"].ToString();
else
    return \"Sheet1$\"
直到最近,当Excel文档(我们从第三方收到)开始包含命名的Ranges时,此方法一直有效。我没有隐藏的工作表。 现在
schemaTable.Rows[0][\"TABLE_NAME\"].ToString()
返回第一个Range的名称。 我可以使用schemaTable对象做一些事情来识别工作表,而不识别工作表中的命名范围吗?     
已邀请:
        是否必须使用OleDbConnection,还是可以切换到其他选项(例如DAO)? 替代解决方案1:使用互操作:
    Private Function GetSheetNames(ByVal path As String)
    Dim lst As New List(Of String)
    \'Note: this will not work for Excel 2007 (.xlsx) workbooks.
    If IO.File.Exists(path) AndAlso IO.Path.GetExtension(path) = \".xls\" Then
    Dim app As New Excel.Application
    Dim WB As Excel.Workbook
    Try
       WB = app.Workbooks.Open(Filename:=path)
       If WB IsNot Nothing Then
         For Each ws As Excel.Worksheet In WB.Sheets
           lst.Add(ws.Name)
         Next
         WB.Close()
         System.Runtime.InteropServices.Marshal.ReleaseComObject(WB)
         WB = Nothing
       End If
    Catch ex As Exception
       MessageBox.Show(ex.Message)
    Finally
       System.Runtime.InteropServices.Marshal.ReleaseComObject(app)
       app = Nothing
    End Try
     End If
     Return lst
End Function
来源:http://social.msdn.microsoft.com/Forums/en-US/vbgeneral/thread/21d3f77c-1d3d-44e0-9bd5-eca45a0affa6 替代解决方案2:使用DAO:
Dim dao_dbE As dao.DBEngine
Dim dao_DB As dao.Database
dao_dbE = New dao.DBEngine
Dim sheetsNames As List(Of String) = New List(Of String)()
dao_DB = dao_dbE.OpenDatabase(completeFileName, False, True, \"Excel 8.0;\")
For i As Integer = 0 To dao_DB.TableDefs.Count - 1
    sheetsNames.Add(dao_DB.TableDefs(i).Name)
Next
    
        由于@Heinzi确实令人难过,您应该研究返回的元数据的其他属性,这是唯一的方法。由于某些属性值,范围很可能与工作表有所不同。 更新: 由于此解决方案不起作用,并且如果文件为XSLX格式,我认为最快,最简单的解决方案是使用#ZipLib抓取原始XML(记住XLSX只是ZIP)
myFilename.xlsx/xl/workbook.xml
,并抓取所有工作表名称来自“ 5”节点。     

要回复问题请先登录注册