VBA execl将数据导入数据库
发布网友
发布时间:2022-06-06 09:30
我来回答
共1个回答
热心网友
时间:2023-10-08 18:47
Sub 把Excel数据插入数据库中()
'*******************************************
'时间:2010-06-28
'作者:bengdeng
'功能:把当前工作表的数据增加到在程序文件同一目录下进销存表数据库中
'注意:要在工具/引用中引用microsoft activex date objects x.x
' 其中x.x为版本号,可能会因为你安装的office的版本不同而不同,本例引用了2.5版
'发布:http://www.excelba.com
'*******************************************
Dim conn As ADODB.Connection
Dim WN As String
Dim TableName As String
Dim sSql As String
Dim tStr As String
'数据库名,请自行修改,路径与当前工作簿在同一目录
WN = "进销存表.mdb"
'数据库的表名与当前工作表名一致
TableName = ActiveSheet.Name
Set conn = New ADODB.Connection
conn.ConnectionString = "Provider=Microsoft.Jet.Oledb.4.0;" & _
"Extended Properties=Excel 8.0;" & _
"Data Source=" & ThisWorkbook.Path & "\" & ActiveWorkbook.Name
conn.Open
If conn.State = adStateOpen Then
sSql = "Insert Into [;DataBase=" & ActiveWorkbook.Path & "\" & WN & "]." & TableName & " Select * From [" & ActiveSheet.Name & "$]"
conn.Execute sSql
MsgBox "成功把数据插入到“" & TableName & "”中!", , "http://excelba.com"
conn.Close
End If
Set conn = Nothing
End Sub