本人初学VB,用VB读取Excel数据到Access数据库,但是为什么读取到数据库里的数据全是0?

代码:
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 '列循环

rs("intcuntrow") = xlSheet.Cells(intcountrow, intcountco)
'rs.Fields(intcountco) = xlSheet.Cells(intcountrow, intcountco) '将excel的数据写入access中
rs.Update
Print xlSheet.Cells(intcountrow, intcountco);
Next intcountco
Print
'rs.MoveNext

Next intcountrow

Text1.Text = xlSheet.UsedRange.Columns.Count '将读取的列数保存在Text1中

Text2.Text = xlSheet.UsedRange.Rows.Count '将读取的行数保存在Text2中

Set xlSheet = Nothing
Set xlBook = Nothing
xlApp.Quit
Set xlApp = Nothing
Set rs = Nothing
Set con = Nothing
rs.Close
con.Close

End Sub
最新回答
宣我会死啊

2025-06-27 16:33:08

rs("intcuntrow") = xlSheet.Cells(intcountrow, intcountco),这句有错误,它重复的把Excel表的数据读入到记录集的一列中的当前点中去了,如果你的xlSheet.Cells(intcountrow, intcountco)最后一列数据刚好是0就会导致这个现象.