请教一个使用ADO将access与excel数据互倒的问题
想实现一个功能 在电脑上没有装office的情况下,将access中数据添加到excel表格中,或将excel中数据添加到access中,想以ado的方式实现,把xls文件当作数据库处理。
目前将access导入到excel中成功了
"select tele_code as 号码,username as 姓名,department as 单位,address as 地址,remark as 备注 into [excel 8.0;database=" & App.Path & "\book.xls].号码 from tele"
Excel access ado
"select 姓名 as USER,单位 as DEPARTMENT,代码 as CODE,号码1 as TELE_CODE into [tele] from [excel 8.0;database=" & LocalPath & "].[tele$]"
’经实际测试,以下代码数据库和Excel之间互相导入导出,完全成功!
Private Sub Command1_Click()
'access导出到excel
Dim db As New ADODB.Connection
Dim sPath As String
db.Open "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=E:\Temp\Test\db1.mdb Persist Security Info=False"
sPath = App.Path + "\backup.xls"
If Dir(sPath) <> "" Then
Kill sPath
Else
Call db.Execute("select * into Sheet1 In '" & sPath & "' 'excel 8.0;' from 表1")
MsgBox "导出成功", vbOKOnly, "提示"
End If
db.Close
Set db = Nothing
End Sub
Private Sub Command2_Click()
'从excel导出到 access
Dim db As New ADODB.Connection
Dim sPath As String
db.Open "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=E:\Temp\Test\db1.mdb;Persist Security Info=False"
sPath = App.Path + "\backup.xls"
Call db.Execute("select * into Table4 From [Sheet1$] In '" & sPath & "' 'excel 8.0;'")
db.Close
Set db = Nothing
End Sub