帮我优化以下代码,不使用ADO,实现将表"导出数据"每次以不同的10000条导出至EXECL文件中,直至数据记录导完,ExportData为临时表,每次“导出数据”不同的10000条记录,导出完成后需要删除。 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 '清空导出数据表 DoCmd.RunSQL "DELETE * FROM 导出数据"
时间: 2024-03-15 22:47:36 浏览: 52
可以尝试使用ADO来代替DAO来操作数据库,ADO更加灵活且性能更好。具体代码如下:
```
Dim conn As ADODB.Connection
Dim rs As ADODB.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 conn = New ADODB.Connection
conn.Open "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" & CurrentDb.Name & ";Persist Security Info=False;"
strSQL = "SELECT 导出数据.* FROM 导出数据"
Set rs = New ADODB.Recordset
rs.Open strSQL, conn, adOpenStatic, adLockReadOnly, adCmdText
'循环导出数据
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
conn.Execute "DELETE * FROM 导出数据"
conn.Close
Set conn = Nothing
```
此外,还可以考虑使用ADODB.Stream来导出Excel文件,可以更加灵活地控制Excel文件的格式和内容。使用ADODB.Stream的代码如下:
```
Dim conn As ADODB.Connection
Dim rs As ADODB.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 '导出计数器
Dim stream As ADODB.Stream
Dim fieldCount As Integer
Dim fieldIndex As Integer
'设置导出文件路径和文件名
strFileName = "C:\Users\icd\Desktop\流水分割\"
batchSize = 10000 '每批次导出的记录数
Set conn = New ADODB.Connection
conn.Open "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" & CurrentDb.Name & ";Persist Security Info=False;"
strSQL = "SELECT 导出数据.* FROM 导出数据"
Set rs = New ADODB.Recordset
rs.Open strSQL, conn, adOpenStatic, adLockReadOnly, adCmdText
'循环导出数据
Do While Not rs.EOF
If exportCount Mod batchSize = 0 Then
'每批次导出到一个新的文件
If exportCount <> 0 Then
'保存Excel文件
stream.SaveToFile strFileName & Format(batchCount, "000000") & ".xlsx", adSaveCreateOverWrite
End If
batchCount = batchCount + 1
'创建新的Excel文件
Set stream = New ADODB.Stream
stream.Type = adTypeBinary
stream.Open
stream.WriteText "sep=," & vbCrLf '设置CSV格式参数
fieldCount = rs.Fields.Count
For fieldIndex = 0 To fieldCount - 1
stream.WriteText rs.Fields(fieldIndex).Name & IIf(fieldIndex < fieldCount - 1, ",", vbCrLf)
Next fieldIndex
End If
'写入记录到Excel文件
For fieldIndex = 0 To fieldCount - 1
stream.WriteText rs.Fields(fieldIndex).Value & IIf(fieldIndex < fieldCount - 1, ",", vbCrLf)
Next fieldIndex
exportCount = exportCount + 1
rs.MoveNext
Loop
'导出最后一份文件
If Not stream Is Nothing Then
stream.SaveToFile strFileName & Format(batchCount, "000000") & ".xlsx", adSaveCreateOverWrite
stream.Close
Set stream = Nothing
End If
rs.Close
Set rs = Nothing
conn.Execute "DELETE * FROM 导出数据"
conn.Close
Set conn = Nothing
```
使用ADODB.Stream可以更加灵活地控制Excel文件的格式和内容,同时也可以减少不必要的COM互操作,提高性能。
阅读全文