程序如下:
Private Sub Command1_Click()
Dim xlApp As New Excel.Application
Dim xlBook As New Excel.Workbook
Dim xlSheet As New Excel.Worksheet
Dim con As ADODB.Connection
Dim sql As String
Dim rs As New Recordset
'打开access表
Set con = New ADODB.Connection
con.Open "Provider=Microsoft.Jet.OLEDB.4.0;Persist Security Info=False;" & "Data Source=D:\Temp\tres.mdb"
rs.Open "select * from aa", con, adOpenKeyset, adLockOptimistic
'打开excel表
On Error Resume Next
'后台进程运行excel程序,并得到该工作簿
Set xlBook = xlApp.Workbooks.Open("D:\Temp\bb.xls")
xlApp.Visible = False 'excel表不显示
'获得该工作簿的“sheet1”表
Set xlSheet = xlBook.Sheets("sheet1")
xlSheet.Select
'抽取b2格子的数据并赋值给text1.text
For intcountrow = 2 To xlSheet.UsedRange.Rows.Count '行循环
For intcountco = 1 To xlSheet.UsedRange.Rows.Count '列循环
rs.Fields(intcountco - 1) = xlSheet.Cells(intcountrow, intcountco).Value '将excel的数据写入access中
Next intcountco
rs.MoveNext
Next intcountrow
Text1.Text = xlSheet.UsedRange.Columns.Count 'xlSheet.Range("b2")
Text2.Text = rs.RecordCount 'xlSheet.Cells(3, 5).Value
Set xlSheet = Nothing
Set xlBook = Nothing
xlApp.Quit
Set xlApp = Nothing
Set rs = Nothing
Set con = Nothing
rs.Close
con.Close
End Sub
我excel表格中有5个字段,2行数据,但是写入access中却只有1行数据,并且只有3列字段,我看不出错在哪里?