Jump to content
Fivewin Brasil

TRANSPORTAR UM ARQ EXCELL PARA DBF


edutraini

Recommended Posts

Bom dia

 

Segue Exemplo!!

// Arquivo base 
cFile := "Consu.xls"


If !File(cDirExcel+"\"+cFile)
   MsgAlert("Arquivo não econtrado : "+cDirExcel+"\"+cFile,"Verifique")
   Return .T.
Endif

oExcel:= TOleauto():New( "Excel.Application")
If Ole2TxtError() # "S_OK"
   MsgInfo("EXCEL não está instalado nesta máquina."+CRLF+" Impossível gerar planilha","ATENÇÃO")
   Return nil
Endif
oExcel:Workbooks:Open( cDirExcel+"\"+cFile )

aDados:={}
nCol := 1
nLin := 1
oFolha := oExcel:Get( "ActiveSheet")
While .T.
    If Empty(oFolha:Cells(nLin, 1):Value)
       Exit
    Endif
    cColA := oFolha:Cells(nLin, 1):Value
    cColB := oFolha:Cells(nLin, 2):Value
    cColC := oFolha:Cells(nLin, 3):Value
    
    AADD( aDados, {cColA, cColB, cColC})
    nLin++
End

oExcel:AlertBeforeOverwriting = .F.
oExcel:DisplayAlerts = .F.

oExcel:Visible = .T.

xBrowse(aDados)

 

Att

João Bosco

Link to comment
Share on other sites

Bom-dia

Outro exemplo testando formatações

 

function gerarlote(vTot,oMeter,oText,nOption,numlote)
LOCAL oExcel, oHoja

dbSelectArea( "con" )             // seleciona arquivo contábil

con->( dbGoTop() )

vNomenum := "Lote_"+"&numlote."+".txt"    // usando macro precisa por ponto

if nOption = 2                    // foi selecionado arquivo texto
   copy to &vNomenum delimited    // separado por virgula
   WinExec("Notepad &vNomenum",1) // 1 = diretorio corrente
   return nil
endif
   
nRow := 1                         // reinicia a linha da planilha
nCount := 0                       // Zerar o total do meter
oExcel := TOleAuto():New( "Excel.Application" )
oExcel:WorkBooks:Add()
oHoja := oExcel:Get( "ActiveSheet" )
*::: Seleciona a guia BOLETINS para preenchimento
oExcel:Sheets("Plan1"):Name := "Boletins"   // nomear a 1ª guia
oExcel:Sheets("Boletins"):Select()          // selecionar a guia nomeada
oMeter:cCaption := "Lote dos Boletins"
oHoja := oExcel:Get( "ActiveSheet" )

FOR nCol := 1 TO FCOUNT()  // percorrer todas as colunas
    oHoja:Cells( nRow, nCol ):Value := FieldName( nCol )
//      linhadegrade(oHoja,nRow,nCol) // contorno da célula
NEXT
DO WHILE .NOT. EOF()
   nRow++
   FOR nCol := 1 TO FCOUNT() 
       if ValType( FieldGet( nCol ) ) = "D"  // se for campo data converte p/string  
          oHoja:Cells( nRow, nCol ):Value := dtoc(FieldGet( nCol ))
       else
          if ValType( FieldGet( nCol ) ) = "N"  // se for campo numérico com valor
             oHoja:Cells( nRow, 4  ):NumberFormat = "###.##0,00" // Formata numerico valor  
             oHoja:Cells( nRow, 28 ):NumberFormat = "###.##0,00" // Formata numerico
               oHoja:Cells( nRow, nCol ):Value := FieldGet( nCol )  
          else   
             if ValType( FieldGet( nCol ) ) = "L"  // se for campo lógico
                oHoja:Cells( nRow, nCol ):Value := FieldGet( nCol ) 
             else   
                oHoja:Cells( nRow, nCol ):NumberFormat = "@" // FORMATAR TEXTO
                    oHoja:Cells( nRow, nCol ):Value := alltrim(FieldGet( nCol ))  
                 endif   
             endif
         endif  
//             linhadegrade(oHoja,nRow,nCol) 
   NEXT
    oMeter:Set(++nCount)      
    oMeter:nTotal := con->( RecCount()*1 )
    oText:SetText("Exportando a Guia Boletins!"+chr(13)+;
                  "Processando Registro : "+str(nCount,6)+"/"+str(oMeter:nTotal,6) )
   con->( DBSKIP(1) )
ENDDO
                                                            
FOR nCol := 1 TO FCOUNT()
   oHoja:Columns( nCol ):AutoFit() // ajusta o tamanho das colunas
NEXT
          
cPath := CURDRIVE() + ":\" + GETCURDIR() // caminho do diretorio corrente
  
cFilexls := cPath //+"\"+cFile
   
vNomenum := "Lote_"+"&numlote."+".xls"      // nome do arquivo a gravar
                            
cArqui := cPath + "\" + vNomenum         // caminho do diretorio corrente
              
oExcel:ActiveWorkBook:SaveAs( cArqui )   // salva como o arquivo          

msginfo( "O arquivo foi gerado e gravado com sucesso."+chr(13)+;
         "Será aberto no Excel.","Informação") 
         
cMod := "LOTE GERADO"
cSer := "Foi gerado o arquivo nome: &vNomenum - Lote: &numlote"
registra( cMod , cSer )        

oExcel:Visible := .T. 

hWnd:=oExcel:hWnd

ShowWindow(hWnd, 3) //3 = Maximizada, 1=Normal e 6 = Minimizada

BringWindowToTop(hWnd)
   
con->( dbZap() )

return nil

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...