当前位置:网站首页>VBA批量将Excel数据导入Access数据库
VBA批量将Excel数据导入Access数据库
2022-07-30 18:50:00 【OOQ】
- 需求目标:将存在于Excel表格中的数据导入已经创建好的Access数据库中
实现方式
方式一:逐行扫描工作表,往数组里面存数据时,从单元格中获取值【rs.Fields(j - 1) = Cells(i, j).Value】
Sub addDate1() Dim i As Integer, j As Integer, n As Integer Dim sql As String Dim con As New ADODB.Connection With con .Provider = "microsoft.ace.oledb.12.0" .ConnectionString = ThisWorkbook.Path & "\test.accdb" .Open End With Set rs = con.OpenSchema(adSchemaTables) n = Range("A1").End(xlDown).Row For i = 1 To n Set rs = New ADODB.Recordset sql = "select * from m_check" rs.Open sql, con, adOpenKeyset, adLockOptimistic rs.AddNew For j = 1 To rs.Fields.Count rs.Fields(j - 1) = Cells(i, j).Value Next j rs.Update Next i MsgBox "success" + Str(n) rs.Close con.Close '关闭连接 Set con = Nothing '释放变量 Set rs = Nothing End Sub方式二:将工作表中的数据读到数组里面,在往数据库中存数据时,从数组里面获取值【rs.Fields(j - 1) = arr(i, j)】
Sub addDate() Dim arr, i As Integer, j As Integer Dim sql As String arr = Range("A2").CurrentRegion Dim con As New ADODB.Connection Dim rs As New ADODB.Recordset With con .Provider = "microsoft.ace.oledb.12.0" .ConnectionString = ThisWorkbook.Path & "\test.accdb" .Open End With sql = "select * from m_check" rs.Open sql, con, adOpenKeyset, adLockOptimistic For i = 2 To UBound(arr) rs.AddNew For j = 1 To rs.Fields.Count rs.Fields(j - 1) = arr(i, j) Next j rs.Update Next i MsgBox "success" ' sql = "delete * from m_check" ' con.Execute (sql) rs.Close con.Close '关闭连接 Set con = Nothing '释放变量 Set rs = Nothing End Sub
不同数据量比较
Sub GetRunTime() Dim i As Long Dim dteStart As Date Dim strTime As String '关闭屏幕刷新 'Application.ScreenUpdating = False dteStart = Timer '---------运行过程主体------- addDate1 strTime = Format((Timer - dteStart), "0.00000") MsgBox "运行过程: " & strTime & "秒" '打开屏幕刷新 'Application.ScreenUpdating = True End Sub处理11条数据,耗费了0.05469s
处理28339条数据,数组的方式快
处理1048576 条数据 ,显然数组会发生溢出。
总结
一般来讲,当所处理的数据量较少,无论采取哪种方式,差别不大。但是当数据量达到数万条时,数组方式处理显然较快。当数据量达到百万条时,以上两种方式,均会发生“溢出”,此时我们应该考虑数据分割,将百万条数据分成几个部分,分批进行处理。
边栏推荐
- 《痞子衡嵌入式半月刊》 第 59 期
- The use of terminal split screen tool Terminalx
- NC | Tao Liang Group of West Lake University - TMPRSS2 "assists" virus infection and mediates the host invasion of Clostridium sothrix hemorrhagic toxin...
- LeetCode Exercise - Two Questions About Finding Sum of Array Elements
- 7.30模拟赛总结
- 载誉而归,重磅发布!润和软件亮相2022开放原子全球开源峰会
- natural language processing nltk
- - daily a LeetCode 】 【 191. A number of 1
- requet.getHeader(“token“) 为null
- AWS 控制台
猜你喜欢

Swiper轮播图片并播放背景音乐

LeetCode 练习——关于查找数组元素之和的两道题

CIMC Shilian Dafeitong is the global industrial artificial intelligence AI leader, the world's top AI core technology, high generalization, high robustness, sparse sample continuous learning, industri

cocos creater 热更重启导致崩溃

微信小程序云开发 | 城市信息管理

core sound driver详解

使用postman调接口报Content type ‘text/plain;charset=UTF-8‘ not supported

nlohmann json 使用指南【visual studio 2022】

LeetCode Exercise - Two Questions About Finding Sum of Array Elements

Read the "Language Model" in one article
随机推荐
[OC study notes] attribute keyword
Scrapy框架介绍
kotlin的by lazy
NC | Tao Liang Group of West Lake University - TMPRSS2 "assists" virus infection and mediates the host invasion of Clostridium sothrix hemorrhagic toxin...
mysql的多实例
MYSQL (Basic) - An article takes you into the wonderful world of MYSQL
第4章 控制执行流程
Anaconda Navigator卡在loading applications
OneFlow源码解析:Op、Kernel与解释器
scrapy基本使用
LeetCode Exercise - Two Questions About Finding Sum of Array Elements
沉浸式体验科大讯飞2022消博会“官方指定产品”
JS提升:Promise中reject与then之间的关系
Swiper轮播图片并播放背景音乐
arcpy获取要素类(属性表)包含的数目
Hello, my new name is "Bronze Lock/Tongsuo"
DM8: Single database and single instance to build a local data guard service
【总结】1396- 60+个 VSCode 插件,打造好用的编辑器
OSPF详解(3)
微博广告分布式配置中心的构建与实践(有彩蛋)




