Private Sub Command3_Click()
On Error GoTo MyError
Dim cat As Object '数据库对象
Dim cn As Object '数据连接对象
Dim tbl As Object '数据表对象
Dim xlssql As String
Set cn = CreateObject("ADODB.Connection")
Set cat = CreateObject("ADOX.Catalog")
SheetName = Combo1.Text
If Len(SheetName) = 0 Then
MsgBox "请输入工作表(Sheet)名称", vbInformation, "温馨提示"
Exit Sub
End If
With cdlg
.Filter = "Excel 97-2003工作簿(*.xls)|*.xls|Excel 工作簿(*.xlsx)|*.xlsx"
.DialogTitle = "选择导入文件"
.InitDir = App.Path
.FileName = ""
.ShowOpen
ImportFile = .FileName
End With
If Len(ImportFile) > 0 Then
Debug.Print "filename=" & ImportFile
Text1.Text = ImportFile
DoEvents
'获取sheet名称
If InStr(ImportFile, "xlsx") > 0 Then
cn.Open "Provider=Microsoft.ACE.OLEDB.12.0;" & _
"Data Source=" & ImportFile & _
";Extended Properties='Excel 12.0 Xml;HDR=Yes;IMEX=1'"
Else
cn.Open "Provider=Microsoft.Jet.OLEDB.4.0;" & _
"Data Source=" & ImportFile & _
";Extended Properties='Excel 8.0;HDR=Yes;IMEX=1'"
End If
Set cat.ActiveConnection = cn
Combo1.Clear
For Each tbl In cat.Tables
Debug.Print tbl.Name
If InStr(tbl.Name, "$") > 0 Then
Combo1.AddItem tbl.Name
End If
Next
Combo1.Text = "请选择数据表名称"
Set tbl = Nothing
Set cat = Nothing
Set cn = Nothing
End If
MyError:
If Err.Number = 0 Then Exit Sub
MsgBox Err.Number & vbCrLf & Err.Description, vbCritical, "错误"
End Sub
