如何将多行Excel数据传输到访问表中

现在,我循环遍历Excel中的每一行,并使用插入查询将每行插入到Access表中。 它有效,但速度很慢。 我如何使一个查询一次添加所有的logging?

这是我现在的代码

Sub Insert() Dim rs As ADODB.Recordset, strSQL As String, minPrem As Double, i As Long, datetime As Date datetime = Now() Call DB.ConnectToDB 'Connect to the database Set rs = New ADODB.Recordset i = 7 'first row of data Do While (Cells(i, 1) <> "") If (IsNumeric(Cells(i, 6))) Then minPrem = Cells(i, 6) Else minPrem = 0 End If strSQL = "INSERT INTO Rates (EffectiveDate, State, Company, ClassCode, Rate, MinPremium, TimeOfEntry) VALUES " _ & "(#" & Cells(i, 1) & "#,'" & Cells(i, 2) & "','" & Cells(i, 3) & "','" & Cells(i, 4).Text & "'," & Cells(i, 5) & "," & minPrem & ", #" & datetime & "#)" rs.Open strSQL, Cn i = i + 1 Loop End Sub 

在一个事务中包装多个INSERT操作通常可以加快速度,因为单独的INSERT被caching,然后一次写入(提交)到数据库。 尝试在进入循环之前添加一个cn.BeginTrans语句,然后在退出循环之后执行cn.CommitTrans ,看看是否有帮助。

另外,如果您只是在执行INSERT操作,那么您不需要混淆单独的Recordset对象; 你可以做cn.Execute strSQL