Jump to content
Fivewin Brasil

Estrutura básica para gerar uma listagem ou relatório em Excel


rochinha

Recommended Posts

Amiguinhos,

Estrutura básica para gerar uma listagem ou relatório em Excel:

Function XLSTotalVendas()
   LOCAL oExcel, oHoja
   LOCAL nRow := 1, nCol
	   M->TOT_0001 := 0
   M->TOT_0002 := 0
   M->TOT_0003 := 0
   M->TOT_0004 := 0
	   M->QUEBRAR  := .T.
   M->RESUMIR  := .T.
	   /* ABERTURA E INDICES */
   USE ITENS // Contem os campos DATAPED(D), CUSTO(N,15,2), VALOR(N,15,2), DESCONTO(N,15,2)
   INDEX ON dataped // Sera usado como ponto de quebra
	   CursorWait()
	   oExcel := TOleAuto():New( "Excel.Application" )
   oExcel:WorkBooks:Add()
	   oHoja := oExcel:Get( "ActiveSheet" )
   nRows := oHoja:UsedRange:Rows:Count()
   nCols := oHoja:UsedRange:Columns:Count()
	   oHoja:PageSetup:Orientation  := 2
   oHoja:PageSetup:PrintGridlines := .t.
	   // Margens
   oHoja:PageSetup:LeftMargin   := 0.25
   oHoja:PageSetup:RightMargin  := 0.25
   oHoja:PageSetup:TopMargin    := 0.25
   oHoja:PageSetup:BottomMargin := 0.25
   oHoja:PageSetup:HeaderMargin := 0.25
   oHoja:PageSetup:FooterMargin := 0.25
	   // Area de Impressao
   oHoja:PageSetup:PrintArea = "$A$1:$O$60"
	   // Define nome para planilha
   oHoja:name:='Tabela de Precos' 
	   // Toda planilha
   oHoja:Cells:Font:Name := "Arial"
   oHoja:Cells:Font:Size := 8
	   // Formatando o titulo
   oHoja:Cells( 1, 1 ):Value := "NOME DO RELATORIO"
   oHoja:Cells( 1, 1 ):Font:Size := 18
   oHoja:Cells( 1, 1 ):Font:Name := "Arial"
   oHoja:Range( "A1:G1" ):HorizontalAlignment := 7
   oHoja:Cells( 1, 1 ):Select()
	   nRow := 2
   nCol := 1
	   DBGOTOP()
   /* USAR COM QUEBRA */
   /* ********************************************** */
   IF M->QUEBRAR
      M->QUEBRA_1 := dtos(dataped) // fara a quebra por data
   ENDIF
   /* */
   DO WHILE .T. // EOF()
      if nRow = 2
         //oSheet:Range("A:A"):Set("ColumnWidth",32) // AJUSTA TAMAÑO UN RANGO A UN VALOR 
         // Colocando nomes nas colunas
         oHoja:Cells( nRow, nCol+0 ):Value := "Data"
         oHoja:Cells( nRow, nCol+1 ):Value := "Desconto"
         oHoja:Cells( nRow, nCol+2 ):Value := "Custo Total"
         oHoja:Cells( nRow, nCol+3 ):Value := "Valor Total"
         oHoja:Cells( nRow, nCol+4 ):Value := "Lucro Total"
         oHoja:Cells( nRow, nCol+5 ):Value := "Lucro (%)"
         // Formatando estilo
         oHoja:Cells( nRow, nCol+0 ):Font:Bold := .T. // :Font:Size / :Font:Color
         oHoja:Cells( nRow, nCol+1 ):Font:Bold := .T.
         oHoja:Cells( nRow, nCol+2 ):Font:Bold := .T.
         oHoja:Cells( nRow, nCol+3 ):Font:Bold := .T.
         oHoja:Cells( nRow, nCol+4 ):Font:Bold := .T.
         oHoja:Cells( nRow, nCol+5 ):Font:Bold := .T.
         // Formatando tamanho
         oHoja:Cells( nRow, nCol+0 ):ColumnWidth := 10
         oHoja:Cells( nRow, nCol+1 ):ColumnWidth :=  8
         oHoja:Cells( nRow, nCol+2 ):ColumnWidth := 14
         oHoja:Cells( nRow, nCol+3 ):ColumnWidth := 14
         oHoja:Cells( nRow, nCol+4 ):ColumnWidth := 14
         oHoja:Cells( nRow, nCol+5 ):ColumnWidth := 10
         // Formatando tamanho
         oHoja:Cells( nRow, nCol+0 ):Interior:ColorIndex := 6
         oHoja:Cells( nRow, nCol+1 ):Interior:ColorIndex := 6
         oHoja:Cells( nRow, nCol+2 ):Interior:ColorIndex := 6
         oHoja:Cells( nRow, nCol+3 ):Interior:ColorIndex := 6
         oHoja:Cells( nRow, nCol+4 ):Interior:ColorIndex := 6
         oHoja:Cells( nRow, nCol+5 ):Interior:ColorIndex := 6
         //
         nRow := 3
      endif
      
      /* USAR COM RESUMO */
      /* ********************************************** */
      IF M->RESUMIR
         M->QUEBRA_1=dtos(dataped)
         M->SUM_0001 := 0
         M->SUM_0002 := 0
         M->SUM_0003 := 0
         M->SUM_0004 := 0
         DO WHILE M->QUEBRA_1=dtos(dataped) // Faz enquanto nao QUEBRA
            M->SUM_0001 := M->SUM_0001 + desconto
            M->SUM_0002 := M->SUM_0002 + custo
            M->SUM_0003 := M->SUM_0003 + valor
            M->SUM_0004 := M->SUM_0004 + valor-custo
            SKIP
         ENDDO
         SKIP -1
      ELSE
         M->SUM_0001 := desconto
         M->SUM_0002 := custo
         M->SUM_0003 := valor
         M->SUM_0004 := valor-custo
      ENDIF
      /* */     
      
      // Formatando um campo
      oHoja:Cells( nRow, nCol+0 ):Value := dataped
      oHoja:Cells( nRow, nCol+1 ):NumberFormat := "###.###.##0,00"; oHoja:Cells( nRow, nCol+1 ):Value := M->SUM_0001
      oHoja:Cells( nRow, nCol+2 ):NumberFormat := "###.###.##0,00"; oHoja:Cells( nRow, nCol+2 ):Value := M->SUM_0002
      oHoja:Cells( nRow, nCol+3 ):NumberFormat := "###.###.##0,00"; oHoja:Cells( nRow, nCol+3 ):Value := M->SUM_0003
      oHoja:Cells( nRow, nCol+4 ):NumberFormat := "###.###.##0,00"; oHoja:Cells( nRow, nCol+4 ):Value := M->SUM_0004
      oHoja:Cells( nRow, nCol+5 ):NumberFormat := "###.###.##0,00"; oHoja:Cells( nRow, nCol+5 ):Value := div(M->SUM_0004,M->SUM_0003)*100
      // Somatoria dos subtotais
      oHoja:Cells( nRow, nCol+0 ):Font:Bold := .T.
      oHoja:Cells( nRow, nCol+2 ):Font:Bold := .T.
      oHoja:Cells( nRow, nCol+3 ):Font:Bold := .T.
      oHoja:Cells( nRow, nCol+4 ):Font:Bold := .T.
      //
      oHoja:Cells( nRow, nCol+2 ):Interior:ColorIndex := 6
      oHoja:Cells( nRow, nCol+3 ):Interior:ColorIndex := 6
      oHoja:Cells( nRow, nCol+4 ):Interior:ColorIndex := 6
      //
	      SKIP
      /* */
      nRow := nRow + 1
      if eof()
         exit
      endif
      //SKIP
   ENDDO
   //
   oHoja:Cells( nRow, nCol+2 ):Value        := "CUSTO TOTAL"
   oHoja:Cells( nRow, nCol+3 ):Value        := "VALOR TOTAL"
   oHoja:Cells( nRow, nCol+3 ):Value        := "LUCRO TOTAL"
   nRow := nRow + 1
   oHoja:Cells( nRow, nCol+2 ):Font:Bold    := .T.
   oHoja:Cells( nRow, nCol+2 ):Font:Italic  := .T.
   oHoja:Cells( nRow, nCol+2 ):Font:Size    := 10
   oHoja:Cells( nRow, nCol+2 ):NumberFormat := "###.###.##0,00"
   oHoja:Cells( nRow, nCol+2 ):Value        := "=soma("+chr(64+nCol+2)+"3:"+chr(64+nCol+2)+alltrim(str(nRow-1))+")"
   //
   oHoja:Cells( nRow, nCol+3 ):Font:Bold    := .T.
   oHoja:Cells( nRow, nCol+3 ):Font:Italic  := .T.
   oHoja:Cells( nRow, nCol+3 ):Font:Size    := 10
   oHoja:Cells( nRow, nCol+3 ):NumberFormat := "###.###.##0,00"
   oHoja:Cells( nRow, nCol+3 ):Value        := "=soma("+chr(64+nCol+3)+"3:"+chr(64+nCol+3)+alltrim(str(nRow-1))+")"
   //
   oHoja:Cells( nRow, nCol+4 ):Font:Bold    := .T.
   oHoja:Cells( nRow, nCol+4 ):Font:Italic  := .T.
   oHoja:Cells( nRow, nCol+4 ):Font:Size    := 10
   oHoja:Cells( nRow, nCol+4 ):NumberFormat := "###.###.##0,00"
   oHoja:Cells( nRow, nCol+4 ):Value        := "=soma("+chr(64+nCol+4)+"3:"+chr(64+nCol+4)+alltrim(str(nRow-1))+")"
   //
   oExcel:Visible := .T.
	   /* CASO QUEIRA IMPRIMIR */
   //oHoja:PrintOut() // Funciona OK
	   /* CASO QUEIRA SALVAR */
   //oExcel:WorkBooks:SaveAs(cFilePath(GetModuleFileName(GetInstance()))+"\planilha.xls")
	   oHoja:End()
   oExcel:End()
   CursorArrow()
   RETURN


 

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