Jump to content
Fivewin Brasil

exportar para excel


EDUTEK

Recommended Posts

 

Boa tarde

 

Como faço para exportar uma tabela para o excel,

Hoje eu abro o xBrowse e Exporto, como eu faço para não mostrar os dados no browse mas mandar direto para o Excel.

tem como fazer isso no xbrowse ?

Eu tenho uma rotina que interage com o meu dicionario de cadastros, onde constam todas as propriedades das tabelas, mas não é muito difícil de entender...:

Note que o meu array  aStruExcel  contem varias colunas preenchidas a partir do meu dicionario de cadastro. no seu caso consider somente as colunas de Dbstruct: NOME DO CAMPO, TIPO DO CAMPO, TAMANHO DO CAMPO E QUANT CASAS DECIMAIS

espero que possa ajudá-lo. qualquer coisa joaosiscom@hotmail.com

FUNCTION GeraExcel(cCONDW,cCONDF)
/*
By João F. Aguiar e os amigos do forum www.fivewin.com.br

*/

LOCAL oDlg,oCbx,oExcel, oSheet,nREC := ARQCAD->( RECNO() )
LOCAL nPOSII,nPOSIF,cFORMAT,oText,aStruExcel := { }
PRIVATE cCONDWHILE :=  IF(cCONDW == Nil,"!EOF()",cCONDW ),cTBLSELEC

... ARQCAD é a tabela a ser exportada para o excel

//
// ARQDICID é a tabela do meu dicionario de cadastro onde constam todas as propriedades das minhas tabelas. no seu caso pegue os dados a partir de dbstruct()...
//


//
// .. aStruExcel no seu caso deve ser preenchida com os dados de dbstruct()
//

DbselectArea("ARQDICID")
SEEK cTABELA
WHILE TABELA == cTABELA .AND. !EOF()
   IF USO # " " .AND. cNIVEL >= ARQDICID->USO .AND. EMPTY(NOMCAD) .AND. !EMPTY(TITCAMPO)
      AADD(aStruExcel,{TRIM(NOMCAMPO),TRIM(TITCAMPO) +IF(!"MSEEK" $ UPPER(VALID),""," (P)") ,NOMOBJ,PICTURE,TRIM(VALID)+CONDIREL,;
      IF( cNIVEL < ARQDICID->EDITAVEL .OR. ARQDICID->EDITAVEL = " " ,.F.,.T.),;
      TAMCAMPO,.T.,0,AUTOINCR,AUTOATRIB,TIPCAMPO,PADR(POSICAMP,8),REALVIRT,FLUTUANTE,;
      DECIMAIS,GERAOCOR})
   ENDIF
   DbSkip()
ENDDO
//
IF !MsgYesNo("Confirma o envio desta tabela para o Excel ?",'Confirmação')
   RETURN Nil
ENDIF


//
// .. neste momento aStruExcel já deverá está preenchida com as propriedades da tabela a ser enviada para o excel
//

oExcel := TOleAuto():New( "Excel.Application" )
oExcel:WorkBooks:Add()
oSheet := oExcel:Get( "ActiveSheet" )
oSheet:Name := "Aba 1"
nL := 1
nC := 0
FOR n = 1 TO LEN(aStruExcel)
   ++ nC
   IF aStruExcel[n,12] = "D"  // 12 É o tipo do campo... voce pode monta esse array com directory() e constumizar este código.
      nLENFIELD := 10
   ELSEIF aStruExcel[n,12] = "M"
      nLENFIELD := 80
   ELSE
      nLENFIELD := aStruExcel[n,7] + 1
   ENDIF
   IF LEN( aStruExcel[n,2] ) > nLENFIELD .AND. !aStruExcel[n,12] == "M"
      nLENFIELD := LEN( aStruExcel[n,2] )
   ENDIF
   oSheet:Columns(nC):ColumnWidth := nLENFIELD
   oSheet:Cells(1,nC):Value := aStruExcel[n,2]
NEXT
//
DbselectArea("ARQCAD")  // ARQCAD é a tabela a ser exportada
IF cCONDW == Nil
   DbGoTop()
ENDIF

// ... cCAMPO ... nome do campo da estrutura dbf
// StruExcel[n,12]  ...tipo do campo dbf c n d m ..


WHILE &cCONDWHILE  // ou  !EOF()
   ++ nL
   nC := 0
   FOR n = 1 TO LEN(aStruExcel)
      IF aStruExcel[n,14] == "V"  // .OR. !aStruExcel[n,6] // eDITAVEL
         IF "->" $ aStruExcel[n,1] .AND. !"(" $ aStruExcel[n,1]

         ELSE
            LOOP
         ENDIF
      ENDIF
      ++ nC
      IF aStruExcel[n,12] $ "D"
         cCAMPO := "DTOC(" + aStruExcel[n,1] +")"
         oSheet:Cells( nL, nC ):Value := &cCAMPO
      ELSEIF aStruExcel[n,12] $ "N"
         IF !"COMBO" $ aStruExcel[n,4]  // Picture
            nPOSII := nPOSIF := 0
            IF aStruExcel[n,16] > 0 // Deciomais
               cCAMPO := "TRANSF(" + aStruExcel[n,1] +",'@E "+REPLIC("9",aStruExcel[n,7] - (aStruExcel[n,16]-1) )+;
                          "."+REPLIC("9",aStruExcel[n,16]) + "')"
            ELSE
               cCAMPO := "STR(" + aStruExcel[n,1] +","+STR(aStruExcel[n,7],2) + ")"
            ENDIF
            nPOSII := AT("(",cCAMPO)
            nPOSIF := IF( AT(",",cCAMPO) > 0,AT(",",cCAMPO),AT(")",cCAMPO) )
            cFORMAT := GeraNumFormat( SUBSTR(cCAMPO,nPOSIF+1),IF("TRANS" $ cCAMPO,.T.,.F.))
            //   MsgInfo("cCAMPO = "+cCAMPO +CRLF +;
            //           "cFORMAT = "+cFORMAT)
            oSheet:Cells( nL,nC ):Set( "NumberFormat", cFORMAT)
            oSheet:Cells( nL, nC ):Value := &cCAMPO
         ELSE
            cCAMPO := aStruExcel[n,1]
            IF &cCAMPO > 0
               VARAUXCG1 := TRIM( aStruExcel[n,5] )  // Carregando o array contido em VALID
               aITENSCMB := &VARAUXCG1

               //VARAUXCG1
               //
               IF &cCAMPO <= LEN(aITENSCMB)
                  oSheet:Cells( nL, nC ):Value := aITENSCMB[&cCAMPO]
               ENDIF
            ENDIF
         ENDIF
      ELSEIF aStruExcel[n,12] $ "M"
         cCAMPO := TRIM(aStruExcel[n,1]) + " "
         cCAMPO := &cCAMPO
         nLINMEMO := MLCOUNT(cCAMPO,80)
         //
         FOR i = 1 TO nLINMEMO
             VARAUX1 := MEMOLINE(cCAMPO,80,i)
             IF !EMPTY(VARAUX1)
                IF i > 1
                   ++ nL
                ENDIF
                oSheet:Cells( nL, nC ):Value := VARAUX1
             ENDIF
         NEXT
      ELSE // caractere
         cCAMPO := aStruExcel[n,1]
         oSheet:Cells( nL, nC ):Value := &cCAMPO
      ENDIF
   NEXT
   DbSkip()
ENDDO
oSheet:Cells( 1, 1 ):Select()
oExcel:Visible := .T.
DbGoTo(nREC)
Return Nil
**************************************************************************************
FUNCTION GeraNumFormat( cPICTURE,lTRANSF)
// "#.##0,00  mascara valida para campos numericos
LOCAL nTAMCAM,nDECIMA,cPICT,cCHRPRE,nPOSIPOINT
cPICTURE := TRIM(cPICTURE)
// Conteudo de cPICTURE
IF lTRANSF
   // Se for uma mascara TRANSF() - "@E 999,999.99"

   IF AT(".",cPICTURE) = 0
      RETURN("Geral")
   ENDIF
   cPICT := " "
   cCHRPRE := "#"
   FOR _i := 1 TO LEN(cPICTURE)
      cCHAR := SUBSTR(cPICTURE,_i,1)
      IF cCHAR == "9"
         IF SUBSTR(cPICTURE,_i+1,1) == "."
            cCHRPRE := "0"
         ENDIF
         cPICT += cCHRPRE
      ELSEIF cCHAR $ ","
         cPICT += "."
      ELSEIF cCHAR $ "."
         cPICT += ","
      ENDIF
   NEXT

   /*
   cCHRPRE := "#"
   nPOSIPOINT := AT(".",cPICTURE)
   IF nPOSIPOINT = 0
      nPOSIPOINT := LEN(cPICTURE)
      cCHRPRE := "0"
   ENDIF
   IF nPOSIPOINT = 0
      RETURN("Geral")
   ENDIF
   cPICT := " "

   FOR _i := 1 TO LEN(cPICTURE)
      cCHAR := SUBSTR(cPICTURE,_i,1)
      IF cCHAR == "9"
         IF _i = nPOSIPOINT-1
            cCHRPRE := "0"
         ENDIF
         cPICT += cCHRPRE
      ELSEIF cCHAR $ ","
         cPICT += "."
      ELSEIF cCHAR $ "."
         cPICT += ","
      ENDIF
   NEXT
   */
   RETURN( LTRIM(cPICT) )
ELSE
   // Se for uma mascara STR()
   // 2)
   cPICTURE := LEFT(cPICTURE,LEN(cPICTURE)-1) // tira o fecha parêntese
   // Fica assim 2
   nPOSI := AT(",",cPICTURE)
   IF nPOSI = 0
      // RETURN("Número")  // dar erro em fwh 7.01
      RETURN("Geral")   // dar erro em fwh 7.01
      //cPICT := REPLIC("9",VAL(cPICTURE) )
   ELSE
      // Exemplo: 10,2
      cPICT := REPLIC("#",VAL(LEFT(cPICTURE,nPOSI-1)) -2 ) + "0," +;
               REPLIC("0",VAL(SUBSTR(cPICTURE,nPOSI+1)) )

      // ########0,00
      RETURN(cPICT)
   ENDIF
ENDIF
RETURN(" ")
*****************************************************************************
 

 

Link to comment
Share on other sites

No caso do Xbrowse to excel, tem uma forma de executar sem mostrar o xbrowse, já chamar e ir direto pro excel ?

Meu Caso é o seguinte :

eu uso o FastReport para meus relatorios, ele exporta pro excel normal, mas vai tudo formatado, o que gostaria era antes de mandar pro FR, gerar uma planilha do excel com a tabela principal do relatorio, pois na maioria dos casos eu gero uma tabela temporária para imprimir, esta tabela que eu gostaria de mandar pro excel, sem precisar abrir o FastReport.

Link to comment
Share on other sites

Se entendi...

      IF lSemPreviewFast // := .T. NAO QUERO PREVIEW - DIRETO NA IMPRESSORA.
	         oFrPrn:PrintOptions:SetShowDialog( .F. ) // .f. Direto na impressora
         oFrPrn:Print( .T. )
	      ELSE  // COM PREVIEW NO FASTREPORT
	         /*
         Theotokos:
         com preview:
         FrPrn:ShowReport(1)
         */
	         oFrPrn:ShowReport(1)   // aqui para gerar o preview do relatorio
	      ENDIF

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
×
×
  • Create New...