Clipper On Line • Ver Tópico - Gerar planilha excel formatada

Gerar planilha excel formatada

Aqui você poderá oferecer suas Contribuições, Dicas e Tutoriais (Texto ou Vídeo) que sejam de interesse de todos.

Moderador: Moderadores

 

Gerar planilha excel formatada

Mensagempor rochinha » 18 Fev 2014 22:25

Amiguinhos,

O pequeno exemplo abaixo foi extraido de meus códigos e a sua função é formatar células de uma planilha com negrito, largura, cores e etc.

O objetivo é apenas exemplificar em forma de código como fazer isto.

#include "fivewin.ch"

Function main()
   LOCAL oExcel, oFolha
   LOCAL nRow := 1, nCol

   SET DATE TO BRITISH
   SET DELETE ON
   SET CENTURY ON
   SET EPOCH TO 1960
   SET CONFIRM ON
   SET SOFTSEEK ON
   SET MULTIPLE ON // nFF
   SET 3DLOOK ON
   
   Request DBFCDX
   RddRegister('DBFCDX',1)
   RddSetDefault('DBFCDX')

   Request OrdKeyNo
   Request OrdKeyCount

   USE estoque INDEX estoque
   
   oExcel := TOleAuto():New( "Excel.Application" )
   oExcel:WorkBooks:Add()

   oExcel:Visible := .T.

   oFolha := oExcel:Get( "ActiveSheet" )
   nRows := oFolha:UsedRange:Rows:Count()
   nCols := oFolha:UsedRange:Columns:Count()

   oFolha:PageSetup:Orientation  := 2
   oFolha:PageSetup:PrintGridlines := .t.

   // Margens
   oFolha:PageSetup:LeftMargin   := 0.25
   oFolha:PageSetup:RightMargin  := 0.25
   oFolha:PageSetup:TopMargin    := 0.25
   oFolha:PageSetup:BottomMargin := 0.25
   oFolha:PageSetup:HeaderMargin := 0.25
   oFolha:PageSetup:FooterMargin := 0.25

   // Area de Impressao
   oFolha:PageSetup:PrintArea = "$A$1:$O$60"

   // Define nome para planilha
   oFolha:name:='Tabela de Precos'

   // Toda planilha
   oFolha:Cells:Font:Name := "Arial"
   oFolha:Cells:Font:Size := 8

   dbSelectArea( "ESTOQUE" )

   // Formatando o titulo
   oFolha:Cells( 1, 1 ):Value := "5VOLUTION - WWW.5VOLUTION.COM.BR"
   oFolha:Cells( 1, 1 ):Font:Size := 24
   oFolha:Cells( 1, 1 ):Font:Name := "Arial"
   oFolha:Range( "A1:O1" ):HorizontalAlignment := 7
   oFolha:Cells( 1, 1 ):Select()

   nRow := 2
   nCol := 1
   nXLSLinhas := 1
   DO WHILE nXLSLinhas < 50 // .T. // EOF()
      // Comeca a imprimir a partir da segunda linha deixando a linha de cabecalhos
      if nRow = 2
         // Colocando nomes nas colunas
         oFolha:Cells( nRow, nCol+0 ):Value := "DESCRICAO"
         oFolha:Cells( nRow, nCol+1 ):Value := "PRECO"
         oFolha:Cells( nRow, nCol+2 ):Value := "QT"
         // Formatando estilo
         oFolha:Cells( nRow, nCol+0 ):Font:Bold := .T.
         oFolha:Cells( nRow, nCol+1 ):Font:Bold := .T.
         oFolha:Cells( nRow, nCol+2 ):Font:Bold := .T.
         // Formatando tamanho
         oFolha:Cells( nRow, nCol+0 ):ColumnWidth := 30
         oFolha:Cells( nRow, nCol+1 ):ColumnWidth := 10
         oFolha:Cells( nRow, nCol+2 ):ColumnWidth := 5
         oFolha:Cells( nRow, nCol+3 ):ColumnWidth := 10
         // Formatando tamanho
         oFolha:Cells( nRow, nCol+0 ):Interior:ColorIndex := 6
         oFolha:Cells( nRow, nCol+1 ):Interior:ColorIndex := 6
         oFolha:Cells( nRow, nCol+2 ):Interior:ColorIndex := 6
         //
         nRow := 3
      endif
      // Coloca os dados do campo descricao
      oFolha:Cells( nRow, nCol+0 ):Value := descricao
      //
      // Coloca os dados do campo valorvenda e formata
      oFolha:Cells( nRow, nCol+1 ):Value := valorvenda
      oFolha:Cells( nRow, nCol+1 ):NumberFormat := "##.##0,00"
      //
      // A quarta coluna contera uma formula onde calcula quantidade * valorvenda
      oFolha:Cells( nRow, nCol+3 ):Value := "="+chr(64+nCol+1)+alltrim(str(nRow))+"*"+chr(64+nCol+2)+alltrim(str(nRow))
      oFolha:Cells( nRow, nCol+3 ):NumberFormat := "##.##0,00"
      //
     // A colunas 1 e 4 ficam em negrito
      oFolha:Cells( nRow, nCol+0 ):Font:Bold := .T.
      oFolha:Cells( nRow, nCol+4 ):Font:Bold := .T.
     //
      // A coluna para digitacao tem outra coloracao
      oFolha:Cells( nRow, nCol+2 ):Interior:ColorIndex := 6
      //
      nRow := nRow + 1
      if nRow > 57 .or. eof()
         if eof() .and. nRow < 57
            nRow := 58
         endif
         //oFolha:Cells( nRow, nCol+1 ):NumberFormat := "##.##0,00"
         //oFolha:Cells( nRow, nCol+1 ):Value := "=soma("+chr(64+nCol+3)+"3:"+chr(64+nCol+3)+"57)"
         //oFolha:Cells( nRow, nCol+1 ):Font:Italic := .T.
         nRow := 2
         nCol := nCol + 4
         if eof()
            exit
         endif
      endif
      nXLSLinhas := nXLSLinhas + 1
      SKIP
   ENDDO
   //
   nRow := 58
   oFolha:Cells( nRow, nCol+1 ):NumberFormat := "##.##0,00"
   oFolha:Cells( nRow, nCol+1 ):Value := "=soma("+chr(64+nCol+3)+"3:"+chr(64+nCol+3)+"57)"
   oFolha:Cells( nRow, nCol+1 ):Font:Italic := .T.
   //
   nRow := 59
   oFolha:Cells( nRow, 1 ):Value        := "SUBTOTAL"
   oFolha:Cells( nRow, 1 ):Font:Bold    := .T.
   oFolha:Cells( nRow, 1 ):Font:Size    := 10
   nRow := 60
   oFolha:Cells( nRow, 1 ):NumberFormat := "####.##0,00"
   oFolha:Cells( nRow, 1 ):Value        := "=B58+F58+J58+N58"
   oFolha:Cells( nRow, 1 ):Font:Italic  := .T.
   oFolha:Cells( nRow, 1 ):Font:Bold    := .T.
   //
   nRow := 59
   oFolha:Cells( nRow, 5 ):Value        := "DESCONTO"
   oFolha:Cells( nRow, 5 ):Font:Bold    := .T.
   oFolha:Cells( nRow, 5 ):Font:Size    := 10
   //
   nRow := 60
   oFolha:Cells( nRow, 5 ):NumberFormat := "####.##0,00"
   oFolha:Cells( nRow, 5 ):Font:Italic  := .T.
   oFolha:Cells( nRow, 5 ):Font:Bold    := .T.
   //
   nRow := 59
   oFolha:Cells( nRow, 9 ):Value        := "TOTAL GERAL"
   oFolha:Cells( nRow, 9 ):Font:Bold    := .T.
   oFolha:Cells( nRow, 9 ):Font:Size    := 10
   nRow := 60
   oFolha:Cells( nRow, 9 ):NumberFormat := "####.##0,00"
   oFolha:Cells( nRow, 9 ):Value        := "=A60-(A60*(E60*0,01))"
   oFolha:Cells( nRow, 9 ):Font:Italic  := .T.
   oFolha:Cells( nRow, 9 ):Font:Bold    := .T.
   //
   dbSelectArea( "ESTOQUE" )
   //
   //oExcel:Visible := .T.
   //
   oFolha:End()
   oExcel:End()

   RETURN



Foi testado em Fivewin, mas não tem comandos especificos da biblioteca usando somente o TOleAuto() do Harbour tradicional.
OPS! LINK QUEBRADO? Veja ESTE TOPICO antes e caso não encontre ENVIE seu email com link do tópico para fivolution@hotmail.com. Agradecido.

@braços : ? )

A justiça divina tarda mas não falha, enquanto que a justiça dos homens falha porque tarda.
Avatar de usuário

rochinha
Membro Master

Membro Master
 
Mensagens: 4545
Data de registro: 18 Ago 2003 20:43
Cidade/Estado: São Paulo - Brasil
Curtiu: 806 vezes
Mens.Curtidas: 245 vezes



Retornar para Contribuições, Dicas e Tutoriais

Quem está online

Usuários vendo este fórum: Nenhum usuário registrado online e 2 visitantes


Ola Amigo, espero que meu site e forum tem lhe beneficiado, com exemplos e dicas de programacao.
Entao divulgue o link da Doacao abaixo para seus amigos e redes sociais ou faça uma doacao para o site forum...
MUITO OBRIGADO PELA SUA DOACAO!
Faça uma doação para o forum
cron
v
Olá visitante, seja bem-vindo ao Fórum Clipper On Line!
Efetue o seu login ou faça o seu Registro