tuskinhu

Manim

Aug 9th, 2017
155
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Delphi 8.24 KB | None | 0 0
  1. //Só vou melhorar um pouco as dicas que tem aqui.. façam bom proveito dela.
  2.  
  3.  
  4. procedure TForm1.Button1Click(Sender: TObject);
  5. var objExcel,Sheet,Chart,s : Variant;
  6. cTitulo : string;
  7. i : integer;
  8. begin
  9. //OBS: Voce deve usar a Clausula ComObj no USES para usar o EXCEL
  10. // Utilize também a Clausula Clipbrd pois neste exemplo faremos uso da memória
  11.  
  12. // Cria uma instancia para utilizar o Excel
  13. cTitulo := 'Planilha de Teste';
  14. objExcel := CreateOleObject('Excel.Application');
  15. objExcel.Visible := True;
  16. objExcel.Caption := cTitulo;
  17.  
  18. // Caso queria abrir uma planilha ao invéz de gera-la use os comandos abaixo
  19. //objExcel.Workbooks.Open('c:teste.xls');
  20. //Sheet := objExcel.Workbooks[1].WorkSheets[cTitulo];
  21. // Daqui em diante os comandos na Planilha são os mesmos do resto do Exemplo
  22. //Sheet.Range['A1','Z70'].Replace('Nome','Alterado');
  23.  
  24. // Adiciona planilha(sheet)
  25. objExcel.Workbooks.Add;
  26. objExcel.Workbooks[1].Sheets.Add;
  27. objExcel.Workbooks[1].WorkSheets[1].Name := cTitulo;
  28. Sheet := objExcel.Workbooks[1].WorkSheets[cTitulo];
  29.  
  30. // É possivel copiar um valor da memória diretamente para a planilha
  31. // mas como não se tem controle de onde ele será colado, este processo pode
  32. // ser meio inconveniente
  33. //Clipboard.AsText := 'Linha Copiada para Memória';
  34. //Sheet.paste;
  35.  
  36. // Nota:
  37. // Pode-se preencher as linhas usando o comando abaixo, desta forma vc usaria
  38. // a planilha como se ela fosse uma Array, em alguns casos pode ser interessante
  39. // mas no geral considero bem mais trabalhoso, abaixo demonstro outra forma de trabalho
  40. // usando o RANGE que torna o trabalho bem mais simples
  41. //Sheet.Cells[1,1] := 'Coluna[1,1]';
  42.  
  43. // Preenchendo as Colunas
  44. // Usando o RANGE vc pode preencher várias colunas ao mesmo tempo usando ['A1','A10']
  45. // Ou preencher apenas uma usando ['A1'], isso torna o processo ágil e simples
  46. Sheet.Range['A1'] := 'Código';
  47. Sheet.Range['B1'] := 'Nome';
  48. Sheet.Range['C1'] := 'Fone';
  49. Sheet.Range['D1'] := 'Salário';
  50.  
  51. // por aqui tu limparias a planilha
  52.  
  53.  
  54. // No caso do FOR se torna interessante usar o SHEET.CELLS (veja como)
  55. // Atente que usei o FOR a partir do numero 2 desta forma pularemos a
  56. // primeira linha que ja contem um cabeçalho preenchido acima
  57. for i := 2 to 10 do begin
  58. Sheet.Cells[i,1] := i;
  59. Sheet.Cells[i,2] := 'Nome Cliente '+inttostr(i);
  60. Sheet.Cells[i,3] := 'Fone Cliente '+inttostr(i);
  61. // Formata o Numero para se tornar MOEDA
  62. Sheet.Cells[i,4].NumberFormat := 'R$ #.##0,00_);(R$ #.##0,00)';
  63. Sheet.Cells[i,4] := 1000*i;
  64. end;
  65.  
  66. // Formatando o Cabeçalho
  67. Sheet.Range['A1','D1'].font.name := 'Verdana'; // Fonte
  68. Sheet.Range['A1','D1'].font.size := 12; // Tamanho da Fonte
  69. Sheet.Range['A1','D1'].font.bold := true; // Negrito
  70. Sheet.Range['A1','D1'].font.italic := true; // Italico
  71. Sheet.Range['A1','D1'].font.color := clYellow; // Cor da Fonte
  72. Sheet.Range['A1','D1'].Interior.Color := $00ffcf9c; // Cor da Célula
  73. // Define o tamanho das Colunas (basta fazer em uma delas e as demais serão alteradas)
  74. Sheet.Range['B1','C1' ].ColumnWidth := 27;
  75. Sheet.Range['B1','C1' ].RowHeight := 25;
  76. Sheet.Range['D1'] .ColumnWidth := 16;
  77.  
  78. // Outras formas de Formatar
  79. Sheet.Range['E1'] := 'Subscrito';
  80. Sheet.Range['E2'] := 'Superescrito';
  81. Sheet.Range['E3'] := 'Sublinhado';
  82. Sheet.Range['E4'] := 'Strike';
  83. Sheet.Range['E1'].font.Subscript := true; // Subscrito
  84. Sheet.Range['E2'].font.Superscript := true; // Superescrito
  85. Sheet.Range['E3'].font.Underline := true; // Sublinhado
  86. Sheet.Range['E4'].font.Strikethrough := true; // Strike
  87.  
  88. // Colocando a Borda em um unico lado da Célula
  89. Sheet.Range['E5'].Font.size := 7;
  90. Sheet.Range['E5'].ColumnWidth := 20;
  91. Sheet.Range['E5'] := 'Só um lado da Celula com Borda';
  92. Sheet.Range['E5'].Borders.Item[$00000009].Weight := $FFFFEFD6;
  93. Sheet.Range['E5'].Borders.Item[$00000009].Color := clRed;
  94. //-- Espessura da Linha
  95. //========================
  96. {xlHairline = $00000001;
  97. xlMedium = $FFFFEFD6;
  98. xlThick = $00000004;
  99. xlThin = $00000002;}
  100. //-- Posição da Celula que se deseja colocar a Borda
  101. //=====================================================
  102. {xlInsideHorizontal = $0000000C;
  103. xlInsideVertical = $0000000B;
  104. xlDiagonalDown = $00000005;
  105. xlDiagonalUp = $00000006;
  106. xlEdgeBottom = $00000009;
  107. xlEdgeLeft = $00000007;
  108. xlEdgeRight = $0000000A;
  109. xlEdgeTop = $00000008;}
  110.  
  111. // Incluindo Formulas (a formula deve ser incluida com seu nome original em Inglês
  112. // caso contrário podera ocorrer problemas)
  113. Sheet.Cells[i,3] := 'Total R$';
  114. // Na linha abaixo inclui uma soma e converti para Moeda atravez do NUMBERFORMAT
  115. Sheet.Cells[i,4].NumberFormat := 'R$ #.##0,00_);(R$ #.##0,00)';
  116. Sheet.Cells[i,4].formula := '=SUM(D210)';
  117.  
  118. // Alinhando as Células
  119. Sheet.Range['A1'].VerticalAlignment := 1; // 1=Top - 2=Center - 3=Bottom
  120. Sheet.Range['B1'].VerticalAlignment := 2;
  121. Sheet.Range['C1'].VerticalAlignment := 3;
  122.  
  123. Sheet.Range['B1'].HorizontalAlignment := 3; // 3=Center - 4=Right
  124. Sheet.Range['C1'].HorizontalAlignment := 3;
  125. Sheet.Range['D1'].HorizontalAlignment := 4;
  126.  
  127. // Adiciona Grade nas Colunas
  128. Sheet.Range['A1','D11'].Borders.LineStyle := 1;
  129. Sheet.Range['A1','D11'].Borders.Weight := 2;
  130. Sheet.Range['A1','D11'].Borders.ColorIndex := 1;
  131.  
  132. // Excecuta um Replace na Celula localizada
  133. // Lembre-se que ele alterara todas as celulas localizadas dentro
  134. // do RANGE escolhido, ou seja, se ouverem 10 Colunas com o texto
  135. // desejado este comando alterara as 10
  136. Sheet.Range['A1','Z70'].Replace('Fone Cliente 10','Repassou o Texto');
  137.  
  138. // Cria Grafico
  139. Sheet.ChartObjects.Add(10,160,450,130); // Coordenadas (Left,Top,Width,Height)
  140. Chart := Sheet.ChartObjects[1]; // Cria o Grafico
  141. Chart.Chart.ChartType := $FFFFEFFA; // Tipo do Grafico (veja tabela abaixo)
  142. Chart.Chart.SeriesCollection.Add(Sheet.Range['D210']);
  143.  
  144. // Pode-se dar um PrintPreview apenas no Gráfico, use o comando abaixo
  145. //Chart.Chart.PrintPreview;
  146.  
  147. //--TIPOS DE GRAFICOS
  148. //======================
  149. {xlColumnClustered = $00000033;
  150. xlColumnStacked = $00000034;
  151. xlColumnStacked100 = $00000035;
  152. xl3DColumnClustered = $00000036;
  153. xl3DColumnStacked = $00000037;
  154. xl3DColumnStacked100 = $00000038;
  155. xlBarClustered = $00000039;
  156. xlBarStacked = $0000003A;
  157. xlBarStacked100 = $0000003B;
  158. xl3DBarClustered = $0000003C;
  159. xl3DBarStacked = $0000003D;
  160. xl3DBarStacked100 = $0000003E;
  161. xlLineStacked = $0000003F;
  162. xlLineStacked100 = $00000040;
  163. xlLineMarkers = $00000041;
  164. xlLineMarkersStacked = $00000042;
  165. xlLineMarkersStacked100 = $00000043;
  166. xlPieOfPie = $00000044;
  167. xlPieExploded = $00000045;
  168. xl3DPieExploded = $00000046;
  169. xlBarOfPie = $00000047;
  170. xlXYScatterSmooth = $00000048;
  171. xlXYScatterSmoothNoMarkers = $00000049;
  172. xlXYScatterLines = $0000004A;
  173. xlXYScatterLinesNoMarkers = $0000004B;
  174. xlAreaStacked = $0000004C;
  175. xlAreaStacked100 = $0000004D;
  176. xl3DAreaStacked = $0000004E;
  177. xl3DAreaStacked100 = $0000004F;
  178. xlDoughnutExploded = $00000050;
  179. xlRadarMarkers = $00000051;
  180. xlRadarFilled = $00000052;
  181. xlSurface = $00000053;
  182. xlSurfaceWireframe = $00000054;
  183. xlSurfaceTopView = $00000055;
  184. xlSurfaceTopViewWireframe = $00000056;
  185. xlBubble = $0000000F;
  186. xlBubble3DEffect = $00000057;
  187. xlStockHLC = $00000058;
  188. xlStockOHLC = $00000059;
  189. xlStockVHLC = $0000005A;
  190. xlStockVOHLC = $0000005B;
  191. xlCylinderColClustered = $0000005C;
  192. xlCylinderColStacked = $0000005D;
  193. xlCylinderColStacked100 = $0000005E;
  194. xlCylinderBarClustered = $0000005F;
  195. xlCylinderBarStacked = $00000060;
  196. xlCylinderBarStacked100 = $00000061;
  197. xlCylinderCol = $00000062;
  198. xlConeColClustered = $00000063;
  199. xlConeColStacked = $00000064;
  200. xlConeColStacked100 = $00000065;
  201. xlConeBarClustered = $00000066;
  202. xlConeBarStacked = $00000067;
  203. xlConeBarStacked100 = $00000068;
  204. xlConeCol = $00000069;
  205. xlPyramidColClustered = $0000006A;
  206. xlPyramidColStacked = $0000006B;
  207. xlPyramidColStacked100 = $0000006C;
  208. xlPyramidBarClustered = $0000006D;
  209. xlPyramidBarStacked = $0000006E;
  210. xlPyramidBarStacked100 = $0000006F;
  211. xlPyramidCol = $00000070;
  212. xl3DColumn = $FFFFEFFC;
  213. xlLine = $00000004;
  214. xl3DLine = $FFFFEFFB;
  215. xl3DPie = $FFFFEFFA;
  216. xlPie = $00000005;
  217. xlXYScatter = $FFFFEFB7;
  218. xl3DArea = $FFFFEFFE;
  219. xlArea = $00000001;
  220. xlDoughnut = $FFFFEFE8;
  221. xlRadar = $FFFFEFC9; }
  222.  
  223. // O PrintPreview abre a tela de visualização do Excel
  224. // e o PrintOut imprime na impressora padrão do Windows
  225. //Sheet.PrintPreview;
  226. //Sheet.PrintOut;
  227.  
  228. // o SaveAs permite que vc salve automáticamente o relatório gerado
  229. //Sheet.SaveAs('c:teste.xls');
  230. end;
Advertisement
Add Comment
Please, Sign In to add comment