标题: 利用ADO的OpenSchema方法在不打开xls文件情况下获取Sheet名集合 [打印本页] 作者: yehf 时间: 2011-10-14 16:17 标题: 利用ADO的OpenSchema方法在不打开xls文件情况下获取Sheet名集合 需引用ADO库,此法不能打开带有密码的xls文件
Public Function GetTableName(DataBaseName As String)
Dim strcnn As String
Dim TableSet As ADODB.Recordset
Dim con As New ADODB.Connection
Dim DataBaseType As String
Select Case Right(DataBaseName, 3)
Case "xls"
DataBaseType = "Excel Files"
Case "mdb"
DataBaseType = "MS Access Database"
End Select
strcnn = "DSN=" & DataBaseType & ";dbq=" & DataBaseName
con.Open strcnn
Set TableSet = con.OpenSchema(adSchemaTables)
Do Until TableSet.EOF
GetTableName = GetTableName & TableSet!table_name & ","
TableSet.MoveNext
Loop
GetTableName = Left(GetTableName, Len(GetTableName) - 1)
con.Close
Set gconnection = Nothing
End Function 作者: 鱼儿游游 时间: 2011-10-14 16:20 本帖最后由 鱼儿游游 于 2011-10-14 16:45 编辑
'打开Excel文件
Set objExcelApp = CreateObject("Excel.Application")
Set objExcelBook = objExcelApp.Workbooks.Open(strExcelFileName, , True)
strSheetList=""
For Each objExcelSheet In objExcelBook.Worksheets
strSheetList=strSheetList & objExcelSheet.Name & ","
Next
多谢提醒,我再测试下作者: tanhong 时间: 2011-10-17 12:12
我好象没遇到过作者: yehf 时间: 2011-10-17 12:21
改用ADOX的Catalog对象试试
Public Function GetTableName(FileName As String)
'需引用Microsoft ADO .Ext各版本
Dim cnn As New ADODB.Connection
Dim mycat As New ADOX.Catalog
Dim i As Integer
Set cnn = New ADODB.Connection
With cnn
.Provider = "microsoft.jet.oledb.4.0"
If Right(FileName, 4) = ".xls" Then _
.Properties("extended properties").Value = "excel 8.0" '文件为xls,增加连接词
.Open FileName
End With
mycat.ActiveConnection = cnn
For i = 0 To mycat.Tables.Count - 1
If Left(mycat.Tables.Item(i).Name, 4) <> "MSys" Then
Debug.Print mycat.Tables.Item(i).Name
End If
Next i
Set mycat = Nothing
cnn.Close
Set cnn = Nothing
End Function