visual basic编辑器-工具-引用-microsoft activex data objects 2.8
添加一个模块,写以下代码:
Public Rs As New ADODB.Recordset
Public conn As New ADODB.Connection
在sheet1中写以下代码:
Private Sub Worksheet_Activate()
dfile = "数据库文件位置" 比如:"c:\a.mdb"
On Error Resume Next
conn.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & dfile
conn.Open
Sql = "select * from 表名 order by id desc" 备注:表里必须添加 id(自动编号)
Rs.Open Sql, conn, 1, 3
For i = 1 To Rs.RecordCount
Sheet1.Cells(i, 1) = Rs(0)
Sheet1.Cells(i, 2) = Rs(1)
Sheet1.Cells(i, 3) = Rs(2)
Sheet1.Cells(i, 4) = Rs(3)
......................................备注:表里的值多的话继续添加
Rs.MoveNext
Next
Rs.Close
Set Rs = Nothing
End Sub
如果写到按钮单击事件,就把这些代码复制到按钮单击事件内(sub 和 end sub 不需要复制)