Como ya sabemos COM es muy lento para la exportación de datos, así que he buscado algunas opciones que ayuden a incrementar la velocidad y según he leído hay otras técnicas, ya sea exportar datos desde ADO RecordSet a Excel o exportar datos desde un Array
He realizado éste ejercicio el cual utiliza la primera técnica (ADO RecordSet)
Y funciona bastante bien, de hecho bastante rápido comparado con COM
Code: Select all
#define fileFormat 51
/*
* main
*/
Function main
Local oError
Local oExcel
Local oSheet
Local oRs
Local sqlcommand
Local nSecs
Public connectionString
conexion()
If ( oExcel := win_oleCreateObject( 'Excel.Application' ) ) == Nil
? 'Excel no disponible, ', win_OleErrorText()
Return Nil
Endif
*-- catch any errors
BEGIN SEQUENCE WITH ErrorBlock( { | oError | Break( oError ) } )
oExcel:Visible := .F.
oExcel:DisplayAlerts :=.F.
*-- open new book
oExcel:WorkBooks:Add()
*-- set first sheet as current
oSheet := oExcel:ActiveSheet()
oRs := win_OleCreateObject( "ADODB.Recordset" )
*-- This query returns approximately 10K records
sqlcommand2 := 'Select * From Ventas'
oRs:Open( sqlcommand, connectionString )
If oRs:Eof()
? hb_Utf8ToStr( 'No hay información' )
oRs:Close()
//Return
Endif
nSecs := Seconds()
n := oSheet:Cells( 1, 1 ):CopyFromRecordSet( rs )
nSecs := Seconds() - nSecs
? 'Se exportaron ' + hb_NToS( n ) + ' filas a Excel en ' + hb_NToS( nSecs ) + ' segundos'
*-- block to save file
BEGIN SEQUENCE WITH ErrorBlock( { | oError | Break( oError ) } )
*-- if the file already exists and it's not open, it's overwritten without asking
oSheet:SaveAs( hb_dirSepAdd( hb_dirBase() ) + 'test_excel', fileFormat )
? hb_Utf8ToStr( 'Archivo Excel se generó en ' ) + hb_dirSepAdd( hb_dirBase() )
RECOVER USING oError
*-- if oSheet:SaveAs() fails, show the error
? 'Error al grabar el archivo', hb_Utf8ToStr( oError:Description )
END SEQUENCE
RECOVER USING oError
? oError:Description, 'Error Excel'
END SEQUENCE
oExcel:DisplayAlerts := .T.
*-- close and remove the copy of Excel.exe from memory
*-- close the excel workbook
oExcel:Workbooks:Close()
*-- close Microsoft Excel
oExcel:Quit()
*-- free the COM Excel Object from memory
Release oExcel
oSheet := Nil
oExcel := Nil
oRs:Close()
connectionString:Close()
rs := NIL
connectionString := NIL
? 'Fin!'
Return Nil
/*********/
Agradezco si pueden por favor proporcionarme algún ejemplo para exportar datos a Excel desde array
Saludos,
Javier