帮我优化以下代码,每10000行导出至新EXECL表直至数据记录导完: Private Sub Command9_Click() Dim db As DAO.Database Dim rs As DAO.Recordset Dim strSQL As String Dim strFileName As String Dim i As Long '设置导出文件路径和文件名 strFileName = "C:\Users\icd\Desktop\流水分割\" Set db = CurrentDb() strSQL = "SELECT 导出数据.* FROM 导出数据" Set rs = db.OpenRecordset(strSQL, dbOpenSnapshot) '每10000条记录导出到一个新的文件 Do While Not rs.EOF If i Mod 10000 = 0 Then If i <> 0 Then DoCmd.TransferSpreadsheet acExport, acSpreadsheetTypeExcel12Xml, "ExportData", strFileName & Format(i, "000000") & ".xlsx", True End If rs.Close Set rs = db.OpenRecordset("导出数据", dbOpenSnapshot) End If i = i + 1 rs.MoveNext Loop '导出最后一份文件 If i Mod 10000 <> 0 Then DoCmd.TransferSpreadsheet acExport, acSpreadsheetTypeExcel12Xml, "导出数据", strFileName & Format(i - (i Mod 10000), "000000") & ".xlsx", True End If rs.Close Set rs = Nothing Set db = Nothing End Sub
时间: 2024-02-14 15:33:08 浏览: 103
导出Data数据到execle表
3星 · 编辑精心推荐
您好,以下是优化后的代码,主要是将每次导出的记录数修改为可配置的参数,增强了代码的可重用性和可维护性:
Private Sub Command9_Click()
Dim db As DAO.Database
Dim rs As DAO.Recordset
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 '每批次导出的记录数
Set db = CurrentDb()
strSQL = "SELECT 导出数据.* FROM 导出数据"
Set rs = db.OpenRecordset(strSQL, dbOpenSnapshot)
'循环导出数据
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, "导出数据", strFileName & Format(batchCount, "000000") & ".xlsx", True
End If
rs.Close
Set rs = Nothing
Set db = Nothing
End Sub
希望能对您有所帮助。如果还有什么问题或需要进一步帮助,请随时告诉我。
阅读全文