Automatización Excel con subtotales

631 views
Skip to first unread message

Walter Salcedo

unread,
Jan 7, 2013, 1:09:40 AM1/7/13
to publice...@googlegroups.com
Buenas Noches amigos:

Le he dado varias vueltas a mi problema que me he visto en la necesidad de recurrir por su ayuda:

Necesito exportar un cursor a excel con subtotales y tengo dos problemas:

Explico:
Esta es la estructura de mi cursor:
Field  Field Name      Type                Width    Dec   Index   Collate Nulls    Next    Step
    1  CODIGO          Character               6                             No
    2  RAZONSOC        Character              50                             No
    3  CUENTA          Character               6                             No
    4  NOMCTA          Character              50                             No
    5  FECHA           Date                    8                             No
    6  TD              Character               2                             No
    7  NUMERO          Character              10                             No
    8  REF             Character              10                             No
    9  DEBE            Numeric                15      2                      No
   10  HABER           Numeric                15      2                      No
   11  SALDO           Numeric                15      2                      No
   12  DIAS            Numeric                10                             No
   13  D1A30           Numeric                15      2                      No
   14  D31A60          Numeric                15      2                      No
   15  D61A90          Numeric                16      2                      No
   16  D91A120         Numeric                15      2                      No
   17  D121A180        Numeric                15      2                      No
   18  D181A360        Numeric                15      2                      No
   19  DOVER360        Numeric                15      2                      No

En la primera parte del evento click del boton exportar de mi formulario tengo la siguiente rutina:
        SELECT * FROM report1 INTO CURSOR tmp3
        SELECT tmp3
        cTmp = Getenv("TEMP")+"" + Sys(2015) + ".Tmp"
        Copy To (cTmp) Delimited With Tab
        _Cliptext = Filetostr(cTmp)
        oExcel= Createobject("Excel.Application")
        oexcel.VISIBLE=.F.
        With oExcel
             .DisplayAlerts = .F.
             .Workbooks.Add()
             .Range("A2").PasteSpecial
             
             .Range("A1:S1").Font.Bold = .T.   
             .Range("A1:S1").Interior.Color = RGB(230, 230, 230)
             
             .Cells(1,1)="Código"                   &&A
             .Cells(1,2)="Nombre/Razón Social"      &&B
             .Cells(1,3)="Cuenta"                   &&C
             .Cells(1,4)="Descrpción de la Cuenta"  &&D
             .Cells(1,5)="Fecha"                    &&E
             .Cells(1,6)="TD"                       &&F
             .Cells(1,7)="Número"                   &&G
             .Cells(1,8)="Ref"                      &&H
             .Cells(1,9)="Debe"                     &&I
             .Cells(1,10)="Haber"                   &&J
             .Cells(1,11)="Saldo"                   &&K
             .Cells(1,12)="Dias"                    &&L
             .Cells(1,13)="D1A30"                   &&M
             .Cells(1,14)="D31A60"                  &&N 
             .Cells(1,15)="D61A90"                  &&O
             .Cells(1,16)="D91A120"                 &&P 
             .Cells(1,17)="D121A180"                &&Q
             .Cells(1,18)="D181A360"                &&R
             .Cells(1,19)="DOVER360"                &&S
             .Columns("A:Z").EntireColumn.AutoFit
             .Columns("I:K").NumberFormat = "#,##0.00;[Red](#,##0.00)"
             .Columns("M:S").NumberFormat = "#,##0.00;[Red](#,##0.00)"
             .Range("A1").Select
        ENDWITH
        oExcel.Workbooks(1).SaveAs("c:\litocxc\xls\LitocxcD.xlsx")
        lcXLSFile="c:\litocxc\xls\LitocxcD.xlsx"
        oExcel.quit()
        RELEASE oExcel

*** Pregunta 1:
       A pesar que uso oExcel.quit() y release oExcel , para cerrar la aplicacion excel, éste se queda pegado en memoria
      ? cual es la forma correcta de cerrar el excel y quitarlo de memoria??


En la segunda parte, en este archivo excel (LitocxcD.xlsx) intento totalizarlo por el campo "CODIGO" ,
realiza el proceso pero lo totaliza por el campo "NUMERO"
(Adjunto la rutina)

*----------------------------------------------------------------------------------------


DIMENSION laSubtotal[7]
laSubtotal[1] = 13  && D1A30
laSubtotal[2] = 14  && D31A60
laSubtotal[3] = 15  && D61A90
laSubtotal[4] = 16  && D91A120
laSubtotal[5] = 17  && D121A180
laSubtotal[6] = 18  && D181A360
laSubtotal[7] = 19  && DOVER360

#DEFINE xlSum -4157
oExcel = CREATEOBJECT("excel.application")
WITH oExcel
  .Workbooks.OPEN(lcXLSFile)
  WITH .ActiveWorkbook.ActiveSheet
    lnRows = .UsedRange.ROWS.COUNT && Get current row count
    .RANGE('M:S').NumberFormat = "#,##0.00" && Format columns
    .UsedRange.Subtotal(1, xlSum, @laSubtotal)
    .UsedRange.Subtotal(2, xlSum, @laSubtotal)
    .UsedRange.Subtotal(3, xlSum, @laSubtotal)
    .UsedRange.Subtotal(4, xlSum, @laSubtotal)
    .UsedRange.Subtotal(5, xlSum, @laSubtotal)
    .UsedRange.Subtotal(6, xlSum, @laSubtotal)
    .UsedRange.Subtotal(7, xlSum, @laSubtotal,.F.,.F.,.F.)
    .UsedRange.COLUMNS.AUTOFIT && Autofit columns
  ENDWITH
  .VISIBLE = .T.
ENDWITH

USE IN tmp3

***Pregunta 2:
Que debo de agregar para conseguir que me totalice por "CODIGO" (el cursor está ordenado por codigo)

Agradeciendo por su ayuda,

Atte,

Walter Salcedo
Lima-Perú

Walter Salcedo

unread,
Jan 7, 2013, 11:12:37 AM1/7/13
to publice...@googlegroups.com
Buen Día amigos:
Primero les pido disculpas por solicitar su ayuda de esta forma (trato de enviar el email desde el explorador por la web pero me pide que ingrese una "etiqueta" y no sé que dato poner alli)
 
 
Paso a explicar mi problema del cual les pido me ayuden pro favor:
-- 
 
 

Miguel Canchas

unread,
Jan 7, 2013, 11:38:29 AM1/7/13
to publice...@googlegroups.com

1.- con  oExcel.quit es mas que suficiente para salir…revisa bien…al parecer cuando grabas no termina el proceso completo, ponle un wait para que espere a grabar completo y pueda salir…

 

2.- Intenta posicionarte en la columna donde deseas hacer el calculo...

 

MK

--
 
 

Walter Salcedo

unread,
Jan 8, 2013, 7:43:53 PM1/8/13
to publice...@googlegroups.com
Hola Miguel,
Muchas Gracias por tu respuesta,
 
Finalmente logré solucionar mi problema de la siguiente manera:
 
Problema 1: Después de oExcel.quit le agregué oExcel=.null. y ya no queda nada en memoria (tomé tu consejo y le agregué un wait "Exportando..." nowait )
Problema 2: Igualmente tomé tu consejo, me posicioné en la primera columna y le quité algunas líneas
 
Copio la rutina completa por si a alguien le sirve
 
 
SELECT * FROM report1 WHERE !"TOTAL"$ref INTO CURSOR tmp3

SELECT tmp3

cTmp = Getenv("TEMP")+"" + Sys(2015) + ".Tmp"

Copy To (cTmp) Delimited With Tab

_Cliptext = Filetostr(cTmp)

oExcel= Createobject("Excel.Application")

oexcel.VISIBLE=.F. && .t.

oExcel=.null.

RELEASE oExcel

*----------------------------------------------------------------------------------------

DIMENSION

laSubtotal[7]

laSubtotal[1] = 13 && D1A30

laSubtotal[2] = 14 && D31A60

laSubtotal[3] = 15 && D61A90

laSubtotal[4] = 16 && D91A120

laSubtotal[5] = 17 && D121A180

laSubtotal[6] = 18 && D181A360

laSubtotal[7] = 19 && DOVER360

#DEFINE xlSum -4157

oExcel = CREATEOBJECT("excel.application")

WITH

oExcel

.Workbooks.OPEN(lcXLSFile)

WITH .ActiveWorkbook.ActiveSheet

lnRows = .UsedRange.ROWS.COUNT && Get current row count

.RANGE('M:S').NumberFormat = "#,##0.00" && Format columns

.Range("A1").Select

.UsedRange.Subtotal(1, xlsum, @laSubtotal)

.UsedRange.COLUMNS.AUTOFIT && Autofit columns

.Columns("I:K").NumberFormat = "#,##0.00;[Red](#,##0.00)"

.Columns("M:S").NumberFormat = "#,##0.00;[Red](#,##0.00)"

ENDWITH

.VISIBLE = .T.

ENDWITH

Atte,
 
Walter Salcedo
--
 
 

Miguel Canchas

unread,
Jan 9, 2013, 8:09:28 AM1/9/13
to publice...@googlegroups.com

Excelente…

--
 
 

Reply all
Reply to author
Forward
0 new messages