FiveTech Support Forums

FiveWin / Harbour / xBase community
Board index FiveWin para Harbour/xHarbour ExportAsFixedFormat XLSTOPDF
Posts: 1144
Joined: Mon Feb 05, 2007 07:15 PM
ExportAsFixedFormat XLSTOPDF
Posted: Tue May 02, 2023 01:04 AM
Por muy sencillo que parezca
en este foro no hay una función que exporte un archivo.XLSX a PDF
aquí les comparto el código
Code (fw): Select all Collapse
METHOD SaveToPdf() CLASS Resumen_Movtos
   LOCAL cFilePdf := cTempFile(  "\"+CurDir()+'\tmp\'  , ".PDF" )

   ::oExcel := CREATEOBJECT( "Excel.Application" )
   ::oExcel:WorkBooks:Open( cFileXls )
   ::oExcel:Sheets( cHoja1  ):Select()
   ::oSheet := ::oExcel:Get( "ActiveSheet" )
   ::oSheet:PageSetup:PaperSize   := 1 
   ::oSheet:PageSetup:Orientation := 2 
   [b]::oSheet:ExportAsFixedFormat(0, cFilePdf, 0, .t., .f. )[/b]
   ::oExcel:Quit()
   MSGRUN( "Abriendo PDF FILE, por favor espere un momento: ", "Usuario", {|| Shellexecute( 0,"open", cFilePDF )  } )
RETURN (NIL)
Saludos Shesare.
Cesar Cortes Cruz

SysCtrl Software

Mexico



' Sin +- FWH es mejor "
Posts: 8515
Joined: Tue Dec 20, 2005 07:36 PM
Re: ExportAsFixedFormat XLSTOPDF
Posted: Mon Oct 30, 2023 03:38 PM

Tarde César. És posible un ejemplo completo por favor?

Gracias.

Saludos.

João Santos - São Paulo - Brasil - Phone: +55(11)95150-7341
Posts: 1144
Joined: Mon Feb 05, 2007 07:15 PM
Re: ExportAsFixedFormat XLSTOPDF
Posted: Mon Oct 30, 2023 03:48 PM

con gusto lo hago amigo y lo pongo aquí

saludos

Cesar Cortes Cruz

SysCtrl Software

Mexico



' Sin +- FWH es mejor "
Posts: 8515
Joined: Tue Dec 20, 2005 07:36 PM
Re: ExportAsFixedFormat XLSTOPDF
Posted: Mon Oct 30, 2023 03:50 PM
sysctrl2 wrote:con gusto lo hago amigo y lo pongo aquí
saludos

Gracias César.

Regards, saludos.
João Santos - São Paulo - Brasil - Phone: +55(11)95150-7341
Posts: 1144
Joined: Mon Feb 05, 2007 07:15 PM
Re: ExportAsFixedFormat XLSTOPDF
Posted: Mon Oct 30, 2023 07:38 PM

buildh xlstopdf
Code (fw): Select all Collapse
#include <fivewin.ch>

#DEFINE xlPortrait 1
#DEFINE xlLandscape 2

STATIC oClip
STATIC cFileXls
STATIC cText
STATIC cHoja1
STATIC nSize
STATIC nPasteRow
STATIC nFormat

FUNCTION Main()
   WITH OBJECT XlsFileToPdf()
      :New()
   END OBJECT
RETURN (NIL)

CLASS XlsFileToPdf
   data oExcel, oSheet, oBook, nRow
   METHOD New()
   METHOD InitObjectXls()
   METHOD SendToExcel()
   METHOD InitObjectXls()
   METHOD BuildData()
   METHOD EndObjectXls()
   METHOD LoadLogo()
   METHOD Headers1()
   METHOD Headers2()
   METHOD SaveToPdf()
ENDCLASS

METHOD New() CLASS XlsFileToPdf
   if !File( ".\tmp" )
      lMkDir( ".\tmp" )
   endif
   MSGRUN( "send to xls and PDF... ", "User", {|| ::SendToExcel() } )
RETURN (NIL)

METHOD SendToExcel() CLASS XlsFileToPdf

   cFileXls := cTempFile(  "\"+CurDir()+'\tmp\'  , ".xlsx" )
   cText := ''
   nPasteRow := 0
   nFormat := 0

   ::InitObjectXls()

   nSize := 11

   ::LoadLogo()
   ::Headers1()
   ::Headers2()
   ::BuildData()

   ::EndObjectXls()

   ::SaveToPdf()


RETURN NIL

METHOD InitObjectXls() CLASS XlsFileToPdf
   LOCAL oRange

   cHoja1 := "DATA"

   cFileXls := cTempFile(  "\"+CurDir()+'\tmp\'  , ".xlsx" )

   ::nRow := 1
   ::oExcel := CREATEOBJECT( "Excel.Application" )

   ::oBook  := ::oExcel:WorkBooks:Add()
   ::oSheet := ::oBook:Worksheets(1)
   nFormat     :=   ::oBook:Get("FileFormat")

   ::oSheet:name := cHoja1

   ::oExcel:Sheets( cHoja1  ):Select()

   ::oSheet := ::oExcel:Get( "ActiveSheet" )

   ::oSheet:PageSetup:PaperSize   := 1 //carta
   ::oSheet:PageSetup:Orientation := xlPortrait
   ::oSheet:PageSetup:Zoom = 100

   ::oSheet:PageSetup:LeftMargin    := 0.5
   ::oSheet:PageSetup:RightMargin   := 0.75
   ::oSheet:PageSetup:TopMargin     := 1.5
   ::oSheet:PageSetup:BottomMargin  := 2.5
   ::oSheet:PageSetup:HeaderMargin  := 0.5
   ::oSheet:PageSetup:FooterMargin  := 0.5
   ::oSheet:PageSetup:FitToPagesWide := 1
   ::oSheet:PageSetup:FitToPagesTall := .F.

   oRange := ::oSheet:Range("A1")

RETURN (NIL)

METHOD LoadLogo() CLASS XlsFileToPdf
   LOCAL cRange,oRange
   local cFile :=  "\"+CurDir()+'\fivetech.jpg'
   IF file(cFile)
      cRange :=  "A2:C2"
      oRange := ::oSheet:Range( cRange )
      ::oSheet:Shapes:AddPicture(cFile,.F., .T., oRange:Left, oRange:top, 150, 70 )
   ENDIF

RETURN (NIL)

METHOD EndObjectXls() CLASS XlsFileToPdf
   LOCAL nFor
   LOCAL cRange, oRange
   ::oExcel:Sheets( cHoja1  ):Select()
   ::oSheet := ::oExcel:Get( "ActiveSheet" )

   ::oSheet:Columns("A:A"):ColumnWidth := 4
   ::oSheet:Columns("B:B"):ColumnWidth := 10
   ::oSheet:Columns("C:C"):ColumnWidth := 10
   ::oSheet:Columns("D:D"):ColumnWidth := 10


   /*FOR nFor := 1 TO 40
      cRange :=  "A" + ALLTRIM(CSTR( nFor )) + ":" + "F"+ALLTRIM(CSTR( nFor ))
      oRange := ::oSheet:Range( cRange )
      oRange:Font:Name  := "Arial"
      oRange:Font:Size  := 8
   NEXT*/

  // --------------------------------------------------
   ::oExcel:Range( "9:9" ):Select() //despues de los titulos hace un freeze
   //::oSheet:Columns( "E:AE" ):AutoFit()
   ::oExcel:Application:ActiveWindow:FreezePanes := .T.

   TRY
      ::oBook:saveAS( cFileXls, nFormat )
   CATCH
   END

   ::oExcel:Quit()
RETURN NIL

METHOD SaveToPdf() CLASS XlsFileToPdf
   LOCAL cFilePdf := cTempFile(  "\"+CurDir()+'\tmp\'  , ".PDF" )
   ::oExcel := CREATEOBJECT( "Excel.Application" )
   ::oExcel:WorkBooks:Open( cFileXls )
   ::oExcel:Sheets( cHoja1  ):Select()
   ::oSheet := ::oExcel:Get( "ActiveSheet" )
   ::oSheet:PageSetup:PaperSize   := 1 //::oSheet:PageSetup:Set( "PaperSize" , nPage )
   ::oSheet:PageSetup:Orientation := 2 //::oSheet:PageSetup:Set("Orientation",2 )
   ::oSheet:ExportAsFixedFormat(0, cFilePdf, 0, .t., .f. )
   ::oExcel:Quit()
   MSGRUN( "Exportando a PDF, por favor espere un momento: ", "Usuario", {|| Shellexecute( 0,"open", cFilePDF )  } )
RETURN (NIL)

METHOD Headers1() CLASS XlsFileToPdf
   LOCAL nRowIni
   LOCAL cRange, oRange

   ::nRow++
   ::oSheet:Rows(::nRow):RowHeight = 25
   ::oSheet:Cells( ::nRow, 6 ):Value = "Company Name"

   cRange :=  "F" + alltrim(CSTR(::nRow )) + ":" + "I"+alltrim(CSTR( ::nRow ))
   oRange := ::oSheet:Range( cRange )
   oRange:Interior:Color:= RGB(192,0,0)
   oRange:Font:Color    := CLR_WHITE
   oRange:Font:Name := "Arial"
   oRange:Font:Bold     := .t.
   oRange:Font:Size     := 20
   oRange:HorizontalAlignment := 7 //CENTRA EL RANGO DE CELDAS

   ::nRow+=2
   nRowIni := ::nRow
   ::oSheet:Rows(::nRow):RowHeight = 18
   ::oSheet:Cells( ::nRow, 6 ):Value = "ACUMULACION DE SALARIOS"

   ::nRow++
   ::oSheet:Rows(::nRow):RowHeight = 18
   ::oSheet:Cells( ::nRow, 6 ):Value = "INITIAL DATE " + dtoc( date() )

   ::nRow++
   ::oSheet:Rows(::nRow):RowHeight = 18
   ::oSheet:Cells( ::nRow, 6 ):Value = "END DATE " + dtoc( date() )

   cRange :=  "F" + alltrim(CSTR(nRowIni )) + ":" + "I"+alltrim(CSTR( ::nRow ) )
   oRange := ::oSheet:Range( cRange )
   // oRange:Interior:Color:= RGB(192,0,0)
   //oRange:Font:Color    := CLR_WHITE
   oRange:Font:Name := "Arial"
   //oRange:Font:Bold     := .t.
   oRange:Font:Size     := 12
   oRange:HorizontalAlignment := 7 //CENTRA EL RANGO DE CELDAS
   ::nRow+=2

RETURN (nil)

METHOD Headers2() CLASS XlsFileToPdf
   LOCAL nRowIni := ::nRow
   LOCAL nCol
   LOCAL cRange, oRange

   FOR nCol := 2 TO 10
      ::oSheet:Cells( ::nRow, nCol ):Value = "COL " + alltrim(cstr(nCol))
   NEXT

   cRange :=  "A" + alltrim(CSTR(nRowIni )) + ":" + "U"+alltrim(CSTR( ::nRow ))
   oRange := ::oSheet:Range( cRange )
   oRange:Font:Name := "Arial"
   oRange:Font:Bold     := .t.
   oRange:Font:Size     := nSize
   oRange:HorizontalAlignment := -4108
   ::nRow++

RETURN (nil)

METHOD BuildData() CLASS XlsFileToPdf
   LOCAL nRowIni := ::nRow
   LOCAL nCol,i
   LOCAL cRange, oRange

   nPasteRow := ::nRow
   oClip := TClipBoard():New()
   cText := ""


   FOR i := 1 TO 10
      FOR nCol := 1 TO 10
         IF nCol=1
            cText +=   cstr(i)
         else
            cText +=  CHR(9) + "DATA "+alltrim(cstr(nCol))
         ENDIF
      NEXT
      cText += CRLF
      ::nRow ++

   NEXT

  if ! Empty( cText )
      oClip:SetText( cText )
      ::oSheet:Cells( nPasteRow, 1 ):Select()
      ::oSheet:Paste()
      oClip:Clear()
      cText    := ""
   endif


RETURN (nil)
se puede mejorar
saludos
Cesar Cortes Cruz

SysCtrl Software

Mexico



' Sin +- FWH es mejor "
Posts: 8515
Joined: Tue Dec 20, 2005 07:36 PM
Re: ExportAsFixedFormat XLSTOPDF
Posted: Tue Oct 31, 2023 02:10 PM
César, lo siento, pero no entendí. Este ejemplo "genera" un .PDF, no TOMA un archivo.XLSX y lo transforma en un archivo .PDF. Por ejemplo:
Code (fw): Select all Collapse
   cFileXls  := cTempFile(  "\"+CurDir()+'\tmp'  , "TESTSAYX.xlsx" )
Quiero tomar este archivo: "TESTSAYX.xlsx" y transformarlo a .PDF, desde XLSTOPDF, ¿entiendes?

Gracias.

Regards, saludos.
João Santos - São Paulo - Brasil - Phone: +55(11)95150-7341
Posts: 1144
Joined: Mon Feb 05, 2007 07:15 PM
Re: ExportAsFixedFormat XLSTOPDF
Posted: Tue Oct 31, 2023 02:49 PM
Joao
es exactamente lo que comentas
puedes tomar un xls existente
saludos
Code (fw): Select all Collapse
METHOD SaveToPdf() CLASS XlsFileToPdf
   LOCAL cFilePdf := cTempFile(  "\"+CurDir()+'\tmp\'  , ".PDF" ) 
  
  [b]// cFileXls   // archivo .xlsx ORIGEN
  // cFilePdf NUEVO ARCHIVO PDF  DESTINO[/b]

   ::oExcel := CREATEOBJECT( "Excel.Application" )
   ::oExcel:WorkBooks:Open( cFileXls )
   ::oExcel:Sheets( cHoja1  ):Select()
   ::oSheet := ::oExcel:Get( "ActiveSheet" )
   ::oSheet:PageSetup:PaperSize   := 1 //::oSheet:PageSetup:Set( "PaperSize" , nPage )
   ::oSheet:PageSetup:Orientation := 2 //::oSheet:PageSetup:Set("Orientation",2 )
   ::oSheet:ExportAsFixedFormat(0, cFilePdf, 0, .t., .f. )
   ::oExcel:Quit()
   ::oExcel := NIL
   MSGRUN( "Exportando a PDF, por favor espere un momento: ", "Usuario", {|| Shellexecute( 0,"open", cFilePDF )  } )
RETURN (NIL)
Cesar Cortes Cruz

SysCtrl Software

Mexico



' Sin +- FWH es mejor "
Posts: 8515
Joined: Tue Dec 20, 2005 07:36 PM
Re: ExportAsFixedFormat XLSTOPDF
Posted: Tue Oct 31, 2023 03:14 PM
Gracias César. Sinceramente, no me gustan las CLASES. Lo prefiero así: mucho más fácil de entender y dominar según mi gusto personal.

Vea qué código tan simple de leer:
Code (fw): Select all Collapse
// \SAMPLES\XLSATPDF.PRG

#include "FiveWin.ch"

STATIC INCLUDEDOCPROPERTIES := .F.
STATIC IGNOREPRINTAREAS     := .F.

FUNCTION Main()

   LOCAL vArchivoX, cArchivoPdf, oExcel, oSheet, Type, Quality, FileName

   // Donde tomar el .XLXS
   vArchivoX := "C:\FWH1905\SAMPLES\MATERIAL.xlsx" // nome completo do caminho com a unidade tambem

   IF .NOT. FILE( vArchivoX )

      MsgInfo( "Archivo de Origen no ejiste", "ERROR!" )

      RETURN NIL

   ENDIF

   // Donde Grabar: destino.
   cArchivoPdf := 'C:\TMP\ARCHIVO.pdf' // nome completo do caminho com a unidade tambem

   TRY
      oExcel = GetActiveObject( "Excel.Application" )
   CATCH
      TRY
         oExcel = CreateObject( "Excel.Application" )
      CATCH
         MsgInfo( "Excel is not installed on this PC. You need Excel to continue", "ERROR" )
      END
   END

   oExcel:WorkBooks:Open( vArchivoX )

   oExcel:Visible := .F.  // se .t. vai abrir o arquivo na tela

   oSheet = oExcel:ActiveSheet

   oExcel:WorkBooks( 1 ):Save()  // salva no excell
   oExcel:WorkBooks( 1 ):ExportAsFixedFormat( Type := 0, FileName := cArchivoPdf, Quality := 1, IncludeDocProperties := .T., IgnorePrintAreas := .T., 1, 1 ) // exporta em pdf

   oExcel:Quit()

   oExcel := NIL

   /*
   MsgRun( "AGUARDE... ABRINDO O PROGRAMA DO ADOBE...",                      ;
           "Por Favor, Espere Abertura. Pode Demorar.",                      ;
           { || WinExec( ShellExecute( 0, "Open", cArchivoPdf ) ), 5 } )
   */

   MsgRun( "Exportando a PDF, por favor espere un momento: ",                ;
           "Usuario", {|| Shellexecute( 0,"open", cArchivoPdf ), 3.5  } )

RETURN NIL
Gracias.

Regards, saludos.
João Santos - São Paulo - Brasil - Phone: +55(11)95150-7341
Posts: 1144
Joined: Mon Feb 05, 2007 07:15 PM
Re: ExportAsFixedFormat XLSTOPDF
Posted: Tue Oct 31, 2023 03:47 PM

Te quedó muy bien

gracias

Cesar Cortes Cruz

SysCtrl Software

Mexico



' Sin +- FWH es mejor "

Continue the discussion