Gerar planilha excel formatada
Enviado: 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.
Foi testado em Fivewin, mas não tem comandos especificos da biblioteca usando somente o TOleAuto() do Harbour tradicional.
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.
Código: Selecionar todos
#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