代码: Private Sub Command1_Click()Rem 定义连接excel所需要的变量 Dim xlApp As New Excel.Application Dim xlBook As New Excel.Workbook Dim xlSheet As New Excel.Worksheet Dim intcountrow As Integer Dim intcountco As Integer Rem 定义连接数据库所需要的变量
Dim con As ADODB.Connection Dim sql As String, cnstr As String Dim rs As New ADODB.Recordset Dim open_table As String
'打开access表Set con = New ADODB.Connection con.Open "Provider=Microsoft.Jet.OLEDB.4.0;Persist Security Info=False;" & "Data Source=F:\22.mdb" open_table = "select * from test_db" '打开一个表 Set rs = New ADODB.Recordset '新建一个实例 rs.Open open_table, con, adOpenKeyset, adLockOptimistic '使用open方法打开数据库的一个表
'rs.Open "select * from test_db", con, adOpenKeyset, adLockOptimistic '打开excel表 On Error Resume Next '后台进程运行excel程序,并得到该工作簿 Set xlBook = xlApp.Workbooks.Open("F:\test_excel.xls")
xlApp.Visible = False 'excel表不显示
'获得该工作簿的“sheet1”表 Set xlSheet = xlBook.Sheets("sheet1") xlSheet.Select
For intcountrow = 3 To xlSheet.UsedRange.Rows.Count '行循环 rs.AddNew For intcountco = 1 To xlSheet.UsedRange.Columns.Count '列循环