帮我优化以下代码,将导出数据以每次导出不同的10000条至一份新的EXECL表,直至数据记录导完Dim strSQL As String Dim strFileName As String Dim i As Long Dim batchSize As Long '每批次导出的记录数 Dim batchCount As Long '批次计数器 Dim exportCount As Long '导出计数器 '设置导出文件路径和文件名 strFileName = "C:\Users\icd\Desktop\流水分割" batchSize = 10000 '每批次导出的记录数 DoCmd.SetWarnings False '暂时关闭Access的警告信息 DoCmd.RunSQL "SELECT 导出数据.* INTO ExportData FROM 导出数据" '将表导入临时表ExportData DoCmd.SetWarnings True '重新开启Access的警告信息 Set rs = CurrentDb.OpenRecordset("SELECT * FROM ExportData", dbOpenSnapshot) '打开临时表ExportData '循环导出数据 Do While Not rs.EOF If exportCount Mod batchSize = 0 Then '每批次导出到一个新的文件 If exportCount <> 0 Then DoCmd.TransferSpreadsheet acExport, acSpreadsheetTypeExcel12Xml, "ExportData", strFileName & Format(batchCount, "000000") & ".xlsx", True End If batchCount = batchCount + 1 End If exportCount = exportCount + 1 rs.MoveNext Loop '导出最后一份文件 If exportCount Mod batchSize <> 0 Then DoCmd.TransferSpreadsheet acExport, acSpreadsheetTypeExcel12Xml, "ExportData", strFileName & Format(batchCount, "000000") & ".xlsx", True End If rs.Close Set rs = Nothing DoCmd.SetWarnings False '暂时关闭Access的警告信息 DoCmd.RunSQL "DROP TABLE ExportData" '删除临时表ExportData DoCmd.SetWarnings True '重新开启Access的警告信息
时间: 2024-04-21 11:25:40 浏览: 141
可以考虑使用ADO(ActiveX Data Objects)来优化代码,同时可以使用Excel.Application对象来处理Excel文件的导出操作。以下是修改后的代码:
Dim strSQL As String
Dim strFileName As String
Dim i As Long
Dim batchSize As Long '每批次导出的记录数
Dim batchCount As Long '批次计数器
Dim exportCount As Long '导出计数器
'设置导出文件路径和文件名
strFileName = "C:\Users\icd\Desktop\流水分割"
batchSize = 10000 '每批次导出的记录数
DoCmd.SetWarnings False '暂时关闭Access的警告信息
DoCmd.RunSQL "SELECT 导出数据.* INTO ExportData FROM 导出数据" '将表导入临时表ExportData
DoCmd.SetWarnings True '重新开启Access的警告信息
'使用ADO打开临时表ExportData
Dim cn As ADODB.Connection
Dim rs As ADODB.Recordset
Set cn = CurrentProject.Connection
Set rs = New ADODB.Recordset
rs.Open "SELECT * FROM ExportData", cn, adOpenForwardOnly, adLockReadOnly
'使用Excel.Application对象
Dim xlApp As Excel.Application
Set xlApp = New Excel.Application
xlApp.Visible = False '设置Excel应用程序不可见
'循环导出数据
Do While Not rs.EOF
If exportCount Mod batchSize = 0 Then
'每批次导出到一个新的文件
If exportCount <> 0 Then
'使用Excel.Application对象将数据导出到Excel文件
Dim xlBook As Excel.Workbook
Set xlBook = xlApp.Workbooks.Add
xlBook.Worksheets(1).Range("A1").CopyFromRecordset rs
xlBook.SaveAs strFileName & Format(batchCount, "000000") & ".xlsx", Excel.XlFileFormat.xlOpenXMLWorkbook
xlBook.Close
Set xlBook = Nothing
End If
batchCount = batchCount + 1
End If
exportCount = exportCount + 1
rs.MoveNext
Loop
'导出最后一份文件
If exportCount Mod batchSize <> 0 Then
'使用Excel.Application对象将数据导出到Excel文件
Dim xlBook As Excel.Workbook
Set xlBook = xlApp.Workbooks.Add
xlBook.Worksheets(1).Range("A1").CopyFromRecordset rs
xlBook.SaveAs strFileName & Format(batchCount, "000000") & ".xlsx", Excel.XlFileFormat.xlOpenXMLWorkbook
xlBook.Close
Set xlBook = Nothing
End If
rs.Close
Set rs = Nothing
cn.Execute "DROP TABLE ExportData" '删除临时表ExportData
Set cn = Nothing
xlApp.Quit
Set xlApp = Nothing
这样,使用ADO和Excel.Application对象可以更快地导出数据,同时也可以避免Access警告信息的干扰。
阅读全文