Saludos amigos foxeros:
Un cliente me pidió que busque como acelerar un reporte que se exporta a Excel, ya que tarda mucho, la computadora que usan es potente.
Hice pruebas en mi computadora en un disco virtual con VirtualBox y al exportar a Excel por medio de automatización, tarda 2 segundos por registro.
Usando el administrador de tareas vi que el procesador se satura.
El código es muy sencillo:
GO TOP
DO WHILE !EOF()
oleExcel.cells.cells(nRen,1) = ALLTRIM(dia)
oleExcel.cells.cells(nRen,2) = 'WEEK ' + ALLTRIM(semana)
oleExcel.cells.cells(nRen,3) = DTOC(teni_fecha)
oleExcel.cells.cells(nRen,4) = 'MES'
oleExcel.cells.cells(nRen,5) = ALLTRIM(po)
oleExcel.cells.cells(nRen,6) = 'M1'
oleExcel.cells.cells(nRen,7) = ALLTRIM(bd_style)
oleExcel.cells.cells(nRen,8) = ALLTRIM(STR(bd_pound))
oleExcel.cells.cells(nRen,9) = ALLTRIM(rib_style)
oleExcel.cells.cells(nRen,10) = IIF(rib_pound > 0,ALLTRIM(STR(rib_pound)),'')
oleExcel.cells.cells(nRen,11) = ALLTRIM(xtra_style)
oleExcel.cells.cells(nRen,12) = IIF(xtra_pound > 0,ALLTRIM(STR(xtra_pound)),'')
oleExcel.cells.cells(nRen,14) = ALLTRIM(color_ord)
oleExcel.cells.cells(nRen,15) = ALLTRIM(cut_plant)
oleExcel.cells.cells(nRen,16) = ALLTRIM(maq_nomb)
oleExcel.cells.cells(nRen,17) = ALLTRIM(turno)
nRen = nRen + 1
SKIP
ENDDO
Agradeciendo su atencion y comentarios.
Bendiciones.
Carlos Alfaro
Luis María Guayán
Tucumán, Argentina
_________________________
http://www.PortalFox.com
Nada corre como un zorro
_________________________
Gracias amigo Luis Ma.:
Eso voy a hacer.
Bendiciones.
Carlos Alfaro
Interesante amigo Gabriel:
Muchas gracias.
Bendiciones.
Carlos Alfaro
* Método DbfExcel
LPARAMETERS xcFile,xcListaCampos,xcListaHeader* xcFile= Tabla o cursor* xcListaCampos= Campos a listar separado por comas. (Omite=Todos)* xcListaHeader= Encabezados de columnas (Omite=xcLlistaCampos)
* .DbfExcel(cTabla,cListaCampos,cListaHeader)*------------------------------------------------------------------LOCAL minfeval,i,i_,lpag,r,cCampo,cValprop,cVarType,bDezim
minfeval=Ctod("01/01/1900")WAIT WINDOW " Exportando tabla "+Versales(xcfile) at 12,15 nowait
Oexcel = CREATEOBJECT("Excel.Application")IF TYPE('Oexcel')#'O' release mensajero =MESSAGEBOX("No se puede procesar el archivo porque no tiene la aplicación"+CHR(13)+; "Microsoft Excel instalada en su computador.",0,tH_mensaje) RETURN .F.ENDIF
lPag=1xLApp = Oexcelkelibro=XLApp.workbooks.ADD()XLSheet = XLApp.ActiveSheetXLSheet.NAME=Versales(xcFile)+'_'+ALLTR(STR(lpag))HojaName=Versales(xcFile)+'_'+ALLTR(STR(lpag))
SELECT (xcFile)AFIELDS(gastruc)IF EMPTY(xcListaCampos) LOCAL ARRAY gaCampos(ALEN(gastruc,1)) FOR i=1 TO ALEN(gastruc,1) gaCampos=gastruc[i,1] NEXTELSE nCampos=ALINES(gaCampos,UPPER(ALLTRIM(xcListaCampos)),1,",") ENDIFIF EMPTY(xcListaHeader) LOCAL ARRAY gaHeader(ALEN(gaCampos)) FOR i=1 TO ALEN(gaCampos) gaHeader[i]=PROPER(gaCampos[i]) NEXTELSE nHeaders=ALINES(gaHeader,xcListaHeader,1,",") FOR i=1 TO nHeaders gaheader[i]=PROPER(gaHeader[i]) NEXTENDIF
lcExact=SET("Exact")SET EXACT ONr=1FOR i_=1 TO ALEN(gaHeader) with xlSheet.Cells(r,i_) .Value=ALLTRIM(gaHeader[i_]) .Font.Bold=.t. ENDWITHnextr=r+1
SCAN FOR i_=1 TO ALEN(gaCampos) cCampo=gaCampos[i_] cValRep=EVALUATE(cCampo) nas=ASCAN(gaStruc,cCampo) IF nas=0 LOOP && Error en xcListaCampos ENDIF cVartype=gastruc[nas+1] bDezim=gastruc[nas+3]
IF !cVarType$'GQ' with xlSheet.Cells(r,i_) DO CASE CASE cVartype="C" .Value=ALLTRIM(cValRep) CASE cVartype="N" cNumform=iif(bdezim=0,"#,##0","#,##0.")+Repli("0",bDezim) .Style.IncludeNumber =.T. .NumberFormat=cNUmForm .VALUE=cValrep CASE cVartype="D" if cValrep<minfeval cValrep=Ctod("") endif IF !EMPTY(cValrep) .VALUE=cValrep endif CASE cVartype="T" .VALUE=TTOC(cValrep) OTHERWISE .VALUE=cValrep ENDCASE endwith ENDIF NEXT
R=R+1ENDSCANSET EXACT &lcExactWAIT clearOexcel.VISIBLE=.T.
Local
cErrores, lExcel,oExcel** BUSCO UNA SESION DE EXCEL YA ACTIVA
*nRows se usa para los registro o fila
Wait Window
"Espere... Exportandos datos" NowaitcErrores =
On("ERROR")On Error
lExcel = .F.oExcel =
Getobject(,"excel.application")On Error
&cErroresoExcel =
Createobject("Excel.Application")oExcel.
Visible = .T. && VISUALIZO EXCELoExcel.Workbooks.
Add && PREPARO UN NUEVO TRABAJO DE EXCELoExcel.ActiveWindow.
Zoom = 95 && Zoom** Empiezo a carga los datos correspondiente en la hoja de excel
** Nombre de la planilla o hoja1 de excel
oExcel.Sheets("Hoja1").
Name = "Movimientos General" &&Nombre de la hoja
** Crea el Formato de la Hoja
** Formato general de la hoja
WITH
oExcel.Cells.Font.Name = "Tahoma"
.
Size = 8ENDWITH
** Tamaño para las columnas
WITH
oExcel.
Columns("A:A").ColumnWidth = 11.
Columns("B:B").ColumnWidth = 9.
Columns("C:C").ColumnWidth = 9.
Columns("D:D").ColumnWidth = 60.
Columns("E:J").ColumnWidth = 8*.Columns("F:J").ColumnWidth = 10
*.Columns("G:H").ColumnWidth = 11
*.Columns("I:I").ColumnWidth = 10
*.Columns("J:J").ColumnWidth = 12
ENDWITH
** Rectangulo para los titulos
_bor = 3
WITH
oExcel.Range(oExcel.Cells(3,1), oExcel.Cells(3,8)).Borders(01).Weight = _bor
.Borders(02).Weight = _bor
.Borders(03).Weight = _bor
.Borders(04).Weight = _bor
.Borders(07).Weight = _bor
.Borders(08).Weight = _bor
.Borders(09).Weight = _bor
.Borders(10).Weight = _bor
ENDWITH
** Mensaje
nRows = 1
oExcel.Cells(nRows,1) = "SysGes exportando datos. Por favor Espere....."
** Para la fecha
_f1 = _f1 + 1
&&DESDE_f2 = _f2 - 1
&&HASTAnRows = 2
oExcel.Cells(nRows,1) =
ALLTRIM(DTOC(_f1))+ " al " + ALLTRIM(DTOC(_f2))
** Título de los campos
nRows = 3
oExcel.Cells(nRows,1) = "Tipo de mov."
oExcel.Cells(nRows,2) = "Fecha"
oExcel.Cells(nRows,3) = "Nº de Mov."
oExcel.Cells(nRows,4) = "Detalle del movimiento"
oExcel.Cells(nRows,5) = "Cantidad"
oExcel.Cells(nRows,6) = "Precio"
oExcel.Cells(nRows,7) = "Debe"
oExcel.Cells(nRows,8) = "Haber"
** Formato para el Título
WITH
oExcel.Range(oExcel.Cells(1, 1), oExcel.Cells(1,8)) &&combina desde la columna 1 hasta 8.HorizontalAlignment = 3
.MergeCells = .T.
.
Font.Bold = .T..
Font.ColorIndex = 5.
Font.Size = 14ENDWITH
** Formato para la fecha (desde al hasta)
WITH
oExcel.Range(oExcel.Cells(2, 1), oExcel.Cells(2,8)) &&combina desde la columna 1 hasta 8.HorizontalAlignment = 3
.MergeCells = .T.
.
Font.Bold = .T..
Font.ColorIndex = 5.
Font.Size = 10ENDWITH
** Formato columna tipo de movimiento
WITH
oExcel.Range(oExcel.Cells(3,1), oExcel.Cells(3,1)).HorizontalAlignment = 3
.MergeCells = .T.
.
Font.Bold = .T..
Font.ColorIndex = 5.
Font.Size = 8ENDWITH
** Formato columna fecha
WITH
oExcel.Range(oExcel.Cells(3,2), oExcel.Cells(3,2)).HorizontalAlignment = 3
.MergeCells = .T.
.
Font.Bold = .T..
Font.ColorIndex = 5.
Font.Size = 8ENDWITH
** Formato columna Nº de mov.
WITH
oExcel.Range(oExcel.Cells(3,3), oExcel.Cells(3,3)).HorizontalAlignment = 3
.MergeCells = .T.
.
Font.Bold = .T..
Font.ColorIndex = 5.
Font.Size = 8ENDWITH
** Formato columna Descripción de movimiento
WITH
oExcel.Range(oExcel.Cells(3,4), oExcel.Cells(3,4)).HorizontalAlignment = 3
.MergeCells = .T.
.
Font.Bold = .T..
Font.ColorIndex = 5.
Font.Size = 8ENDWITH
** Formato columna Cantidad
WITH
oExcel.Range(oExcel.Cells(3,5), oExcel.Cells(3,5)).HorizontalAlignment = 3
.MergeCells = .T.
.
Font.Bold = .T..
Font.ColorIndex = 5.
Font.Size = 8ENDWITH
** Formato columna Precio
WITH
oExcel.Range(oExcel.Cells(3,6), oExcel.Cells(3,6)).HorizontalAlignment = 3
.MergeCells = .T.
.
Font.Bold = .T..
Font.ColorIndex = 5.
Font.Size = 8ENDWITH
** Formato columna Debe
WITH
oExcel.Range(oExcel.Cells(3,7), oExcel.Cells(3,7)).HorizontalAlignment = 3
.MergeCells = .T.
.
Font.Bold = .T..
Font.ColorIndex = 5.
Font.Size = 8ENDWITH
** Formato columna Haber
WITH
oExcel.Range(oExcel.Cells(3,8), oExcel.Cells(3,8)).HorizontalAlignment = 3
.MergeCells = .T.
.
Font.Bold = .T..
Font.ColorIndex = 5.
Font.Size = 8ENDWITH
** Empiezo a cargar campo por campo en la hoja de excel
Select
mov_gralScan
nRows = nRows + 1
For nColumn = 1 To 8
If nColumn = 1 && tipo de mov
IF mov_gral.Tipo > " "
oExcel.Cells(nRows,nColumn) = mov_gral.Tipo
endif
ENDIF
If
nColumn = 2 && fecha del movimientoIF mov_gral.fecha <> {}
oExcel.Cells(nRows,nColumn) = mov_gral.fecha
endif
ENDIF
If
nColumn = 3 && Código de los movimientoIF mov_gral.codigo > 0
oExcel.Cells(nRows,nColumn) = mov_gral.codigo
endif
ENDIF
If
nColumn = 4 && descripcionoExcel.Cells(nRows,nColumn) = ALLTRIM(mov_gral.descripcion)
ENDIF
If
nColumn = 5 && CantidadIF mov_gral.Cantidad > 0
oExcel.Cells(nRows,nColumn) = mov_gral.Cantidad/100
endif
ENDIF
If
nColumn = 6 && precioIF mov_gral.precio > 0
oExcel.Cells(nRows,nColumn) = mov_gral.precio
endif
ENDIF
If
nColumn = 7 && HaberIF mov_gral.totalsal > 0
oExcel.Cells(nRows,nColumn) = mov_gral.totalsal
endif
ENDIF
If
nColumn = 8 && DebeIF mov_gral.TotalEnd > 0
oExcel.Cells(nRows,nColumn) = mov_gral.TotalEnd
endif
ENDIF
Next
nColumnEndscan
** Calculando el total de la entrada y salida del cursor de movimiento
SELECT SUM
(TotalSal) salida , SUM(TotalEnd) entrada FROM mov_gral INTO CURSOR cur_total** Inserto el total de la salida y entrada
nRows = nRows + 2
oExcel.Cells(nRows,4) = " TOTALES:"
oExcel.Cells(nRows,7) = cur_total.salida
oExcel.Cells(nRows,8) = cur_total.entrada
** Fin
** Para el log del programa
nRows = nRows + 2
oExcel.Cells(nRows,4) = "Generado por SysGes - Sistema de Gestión"
**
** Otros formato
** Formato General para las columnas: precio, debe, haber
oExcel.
Range(oExcel.Cells(4,8), oExcel.Cells(nRows,4)).NumberFormat = '#,##'** Formato para la columna cantidad
oExcel.
Range(oExcel.Cells(5,5), oExcel.Cells(nRows,4)).NumberFormat = '#,##.00'**
** Formato columna Descripción de movimiento
WITH
oExcel.Range(oExcel.Cells(nRows,4), oExcel.Cells(nRows,4)).HorizontalAlignment = 3
.MergeCells = .T.
.
Font.Bold = .T..
Font.ColorIndex = 5.
Font.Size = 8ENDWITH
** Formato columna Debe
WITH
oExcel.Range(oExcel.Cells(nRows,7), oExcel.Cells(nRows,7)).HorizontalAlignment = 3
.MergeCells = .T.
.
Font.Bold = .T..
Font.ColorIndex = 5.
Font.Size = 8ENDWITH
** Formato columna Haber
WITH
oExcel.Range(oExcel.Cells(nRows,8), oExcel.Cells(nRows,8)).HorizontalAlignment = 3
.MergeCells = .T.
.
Font.Bold = .T..
Font.ColorIndex = 5.
Font.Size = 8ENDWITH
** Formato para el Título
x = 1
&&Se usa para la filaoExcel.Cells(x,1) = "Movimiento General"
WITH
oExcel.Range(oExcel.Cells(1, 1), oExcel.Cells(1,8)) &&combina desde la columna 1 hasta 8.HorizontalAlignment = 3
.MergeCells = .T.
.
Font.Bold = .T..
Font.ColorIndex = 5.
Font.Size = 14ENDWITH
** Preparando vista previa
WITH
oExcel.ActiveSheet.PageSetup.PrintArea = "$A$1:$J$" +
ALLTRIM(STR(nRows + 2)).PrintTitleRows = "$1:$9"
.PrintTitleColumns = ""
.
Zoom = .f..FitToPagesWide = 1
.FitToPagesTall = 100
.Orientation = 1
.PaperSize = 1
*.CenterFooter ="COT. " + ALLTRIM(rpcab.nombre)
*.RightFooter = "Página &P"
ENDWITH
*oExcel.ActiveWindow.SelectedSheets.PrintPreview
h = "SysGes" +" - "+
Wtitle()Messagebox
("Exportación de datos completados","&h")
PUBLIC oXls
oXls = CREATEOBJECT(“excel.application”)
oXls.Workbooks.Add
oXls.Visible = .T.
SELECT * FROM ‘Tu Tabla’ INTO CURSOR cursorXls && No es necesario pasarlo a un cursor
_Vfp.DataToClip (‘cursorXls’,,3) && Copiar el cursor a memoria
oXls.Cells(1,1).Select && Seleccionar el Inicio de la Copia de los Datos
oXls.ActiveSheet.Paste() && Copiar los datos al Excel
_Cliptext = ” && Para liberar la memoria
USE IN (SELECT(‘cursorXls’)) && Cerrar el cursor
Saludos.
Muchas gracias amigos fidel, mangomez y Miguel:
Son muy interesantes sus propuestas.
Espero mañana ir a donde el cliente y aplicar sus sugerencias.
Que Dios les bendiga.
Carlos Alfaro
From: publice...@googlegroups.com [mailto:publice...@googlegroups.com] On Behalf Of Miguel Antúnez
Sent: jueves, 07 de marzo de 2013 06:34 a.m.
To: publice...@googlegroups.com