|
楼主 |
发表于 2024-2-23 00:16
|
显示全部楼层
Option Compare Database
Sub test()
t = Timer
Set SelectFiles = Application.FileDialog(3)
With SelectFiles
.AllowMultiSelect = True '可多选
.Title = "请选择要导入的Excel表(可多选)"
.Filters.Clear '清除文件过滤器
If .Show = 0 Then
Exit Sub
End If
FullPath = .SelectedItems(i)
FileName = Mid(FullPath, InStrRev(FullPath, "\") + 1, InStr(FullPath, ".") - InStrRev(FullPath, "\") - 1)
Set xlApp = CreateObject("Excel.Application")
Set xlBook = xlApp.Workbooks.Open(FullPath)
For Each xlwk In xlBook.Worksheets
s = s & "、" & xlwk.Name
Next
arr = Split(Mid(s, 2), "、")
s = ""
Set con = CreateObject("adodb.connection") '创建ado对象
Set rs = CreateObject("ADODB.recordset") '创建记录集
Set rst = CreateObject("ADODB.recordset") '创建记录集
con.Open "Provider=Microsoft.ACE.OLEDB.12.0;Persist Security Info=False;Data Source= " & FullPath & " ;Extended Properties='Excel 12.0;HDR=Yes'"
rs.Open "select * from [" & arr(0) & "$]", con, 1, 3
Erase arr
rst.Open "汇总", CurrentProject.Connection, , adLockOptimistic
Do While Not rs.EOF
rst.AddNew
rst.Fields(0) = rs.Fields(0).Value
rst.Fields(1) = rs.Fields(3).Value
rst.Fields(16) = rs.Fields(21).Value
rst.Fields(17) = rs.Fields(22).Value
rst.Fields(18) = rs.Fields(23).Value * 1024
rst.Fields(19) = rs.Fields(24).Value * 1024
rst.Update
rs.MoveNext
Loop
xlBook.Close
Set xlBook = Nothing
Set xlApp = Nothing
rs.Close
Set rs = Nothing
rst.Close
Set rst = Nothing
End With
Set SelectFiles = Nothing
MsgBox Timer - t
End Sub
用你的方法,记录集导入,虽然不报错了,但是速度会很慢。 |
|