Page 1 of 1

Exportar a Excel, sin abrir el archivo de resultados

PostPosted: Tue Sep 19, 2017 3:10 pm
by mariordz
Buenos días Fivewineros, estoy exportando un DBF a Excel con el metodo antiguo para mi (Simplemente le cambio la extensión al archivo de .DBF a .XLS y Excel lo abre sin problemas), pero tengo un comportamiento raro, agregué varios campos (que hacen las veces de columnas en Excel), pero al exportar los datos de esas columnas aparecen vacias, las cabeceras si estan en mi archivo de resultados, pero los datos no (bastante raro, ya que antes de darle el comando "copy to" le puse un xbrowse para ver la información y si aparecen los datos en las columnas que acabo de agregar.

Para tratar de darle la vuelta al asunto y de paso usar los metodos correctos hice un Xbrowse y usé la clausula ToExcel(), lo que necesito saber es como le hago para que no me abra el archivo de resultados, solo necesito que lo salve en una ubicación que ya tengo predeterminada.

Uso FW1604+Harbour+bcc7


Primero le pido al usuario un directorio donde salvar los resultados
Code: Select all  Expand view


cDirinfo=cGetdir("Selecciona el directorio para guardar la información generada")
 


Despues genero un dialogo para mostrar los datos con un boton para exportarlo a excel (si hubiera una forma de ejecutar el "TOExcel()" SIN abrir un dialogo estaría mejor)
Code: Select all  Expand view

DEFINE DIALOG oDlg SIZE 800,600 PIXEL //FONT oFont

   @ 10, 10 XBROWSE oBrw ;
            SIZE 120, 70 PIXEL ;
            OF oDlg ;
            AUTOCOLS ;
            LINES
           
   @ 250, 25 BTNBMP oBtn1 OF oDlg ;
    SIZE 80, 15 PIXEL 2007 ;
    NOBORDER ;
    PROMPT " &AExcel() " ;
    FILENAME cDirinfo ;
    ACTION ( oBrw:ToExcel())
    oBtn1:cToolTip = { "Enviar a Excel","Exportar", 1, CLR_BLACK, 14089979 }
    oBtn1:SetColor( 0, )

   oBrw:CreateFromCode()

   ACTIVATE DIALOG oDlg CENTERED
 

Re: Exportar a Excel, sin abrir el archivo de resultados

PostPosted: Tue Sep 19, 2017 6:57 pm
by nageswaragunupudi
No need to create xbrowse. Use FW_DbfToExcel()

Example:
Code: Select all  Expand view
USE CUSTOMER
oSheet := CUSTOMER->( FW_DbfToExcel())

Re: Exportar a Excel, sin abrir el archivo de resultados

PostPosted: Tue Sep 19, 2017 9:21 pm
by nageswaragunupudi
This is working for me with Office 365.

Do not open the dbf file also.
Code: Select all  Expand view
function FW_CopyDBF2XL( cSourceDBF, cDestXLS )

   local oExcel, oBook, oSheet
   local aHead

   oExcel   := ExcelObj()
   oBook    := oExcel:WorkBooks:Open( cSourceDBF )
   oBook:ActiveSheet:UsedRange:Columns:AutoFit()

   if cDestXLS == nil
      oExcel:Visible := .t.
   else
      cDestXLS    := TrueName( cDestXLS )
      oExcel:DisplayAlerts := .f.
      oBook:SaveAs( cDestXLS )
      oBook:Close()
   endif

return nil

Give sourcedbf with full path
Give the destination file name without extension.

Re: Exportar a Excel, sin abrir el archivo de resultados

PostPosted: Wed Sep 20, 2017 9:29 pm
by mariordz
Thanks I'll try to do it this way (looks very promising).

Best regards

Re: Exportar a Excel, sin abrir el archivo de resultados

PostPosted: Tue Oct 10, 2017 9:55 pm
by mariordz
Thanks a lot for your help, it works really great, I have a detail though, A user of mine tried to open a file when a file with the same name was already open, obviously an error poped up, I was worndering if there is a way to catch the error, in order to stop the program from crashing, I mean a way to let the user know the file is already open an give him the oportunity to close the opened file and retry saving the new one.

thanks in advance