将数据从 Excel 工作表导出到 MS Access 表时,传统方法涉及使用循环迭代地将每行数据插入到表中。然而,这种方法对于大型数据集可能效率低下。这是使用单个 SQL INSERT 语句的替代方法,可以显着提高性能:
Public Sub DoTrans() Set cn = CreateObject("ADODB.Connection") dbPath = Application.ActiveWorkbook.Path & "\FDData.mdb" dbWb = Application.ActiveWorkbook.FullName dbWs = Application.ActiveSheet.Name scn = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & dbPath dsh = "[" & Application.ActiveSheet.Name & "$]" cn.Open scn ' Construct SQL INSERT statement to bulk insert data ssql = "INSERT INTO fdFolio ([fdName], [fdOne], [fdTwo]) " ssql = ssql & "SELECT * FROM [Excel 8.0;HDR=YES;DATABASE=" & dbWb & "]." & dsh ' Execute SQL INSERT statement to insert data in one go cn.Execute ssql End Sub
在此代码中:
对于具有特定名称的字段,您可以可以修改ssql语句,在INSERT子句中包含特定的字段名:
ssql = "INSERT INTO fdFolio (fdName, fdOne, fdTwo) " ssql = ssql & "SELECT * FROM [Excel 8.0;HDR=YES;DATABASE=" & dbWb & "]." & dsh
以上是如何在不使用循环的情况下高效地将 Excel 数据导出到 MS Access?的详细内容。更多信息请关注PHP中文网其他相关文章!