Personalmente ho effettuato diversi test di efficienza sull'export in
Excel in seguito ad un'esigenza che mi richiedeva l'esportazione di
più di 1milione di Records...
Il sistema più veloce ed efficiente è il CopyFromRecordset di cui ti
allego esempio:
Function EXP2XLS(SQL as string, _
Optional ByRef NomeFile As String = vbNullString, _
Optional OpenDialog As Boolean = False, _
Optional CloseExcel As Boolean = True) As Boolean
On Error GoTo Handle_err
Dim xlApp As Object ' Oggetto EXCEL
Dim oWkb As Object ' Oggetto WORKBOOK
Dim x As Integer
Dim rs As DAO.Recordset
EXP2XLS= False
' APRE SE ESISTE GIA' UN'OGGETTO EXCEL
Set xlApp = CreateObject("Excel.Application")
Set oWkb = xlApp.Workbooks.Add()
If OpenDialog = True Then
Do Until NomeFile <> ""
NomeFile = xlApp.GetSaveAsFilename(InitialFilename:="C:
\TestExport.xls", _
fileFilter:="Excel
Files (*.xls), *.xls", _
Title:="SELEZIONA NOME
FILE")
DoEvents
Loop
End If
If Len(NomeFile) = 0 Then NomeFile = "C:\TestExport.xls"
If EsisteFile(NomeFile) Then Kill NomeFile
' ATTENZIONE ALLA VERSIONE DI EXCEL, usare 43 oppure 50 a seconda
della versione 2003 o superiore...
oWkb.SaveAs NomeFile, 43
' Elimino i Fogli che Excel crea di DEFAULT
For x = oWkb.Sheets.Count To 2 Step -1
oWkb.Sheets(x).Delete
Next
' Assegno un nome al Foglio(Sheet) rimasto.
oWkb.Sheets(1).Name = "MANUTENZIONI"
Set rs = CurrentDb.OpenRecordset(SQL,dbOpenDynaset,dbReadOnly)
' Scrivo nella Cella A1
oWkb.Sheets(1).Range("A1").CopyFromRecordset rs
rs.Close
Set rs = Nothing
EXP2XLS= True
Exit_Here:
oWkb.Save
If CloseExcel Then
oWkb.Close True
xlApp.Quit
Else
xlApp.Visible = True
End If
Set oWkb = Nothing
Set xlApp = Nothing
Exit Function
Handle_err:
MsgBox Err.Number & " " & Err.Description
Resume Exit_Here
End Function
Saluti
@Alex