GARANTIR DESCONTO

Fórum Componente da palheta SERVERS para exportar para o Excel #353498

12/02/2008

0

Bom dia !

steve_narancic,

qual componente da palheta SERVERS para exportar para o Excel e como utilizá-lo ?



Obrigado.
Hilton


Hilton

Hilton

Responder

Posts

12/02/2008

Paullsoftware

aqui vai uma unit completa de um exemplo que fiz a algum tempo quando tava praticando, na época usava o office xp e funcionava perfeitamente...

Unit do Form:
unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls, OleServer, DB, DBTables, Excel2000, ExcelXP;

type
  TForm1 = class(TForm)
    Label1: TLabel;
    Table1: TTable;
    Table1Name: TStringField;
    Table1Capital: TStringField;
    Table1Continent: TStringField;
    Table1Area: TFloatField;
    Table1Population: TFloatField;
    btCopiar: TButton;
    ExcelApplication1: TExcelApplication;
    ExcelWorkbook1: TExcelWorkbook;
    ExcelWorksheet1: TExcelWorksheet;
    procedure btCopiarClick(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}
procedure TForm1.btCopiarClick(Sender: TObject);
var
  oleCelula: OleVariant;
  intContaLinha: cardinal;
begin
  intContaLinha := 1;
  ExcelApplication1.Caption         := ´Delphi conquista o Excel!´;
  ExcelApplication1.Visible[0]      := True;

  // Novo documento
  ExcelApplication1.Workbooks.Add(EmptyParam, 0);
  // Conecta o workbook
  ExcelWorkBook1.ConnectTo(ExcelApplication1.ActiveWorkbook);
  // Conecta o worksheet (note o typecast)
  ExcelWorksheet1.ConnectTo(ExcelWorkbook1.Sheets.Item[1] as ExcelWorkSheet);

  Application.BringToFront;

  // Formatar o cabeçalho
  ExcelWorkSheet1.Range[´A1´, ´A1´].Value[´País´];
  ExcelWorkSheet1.Range[´B1´, ´B1´].Value[´Capital´];
  ExcelWorkSheet1.Range[´C1´, ´C1´].Value[´Continente´];
  ExcelWorkSheet1.Range[´D1´, ´D1´].Value[´Área´];
  ExcelWorkSheet1.Range[´E1´, ´E1´].Value[´População´];

  ExcelWorkSheet1.Range[´A1´, ´E1´].Font.Bold       := 1;
  ExcelWorkSheet1.Range[´A1´, ´E1´].Font.Italic     := 1;
  ExcelWorkSheet1.Range[´A1´, ´E1´].Font.Underline  := 1;
  ExcelWorkSheet1.Range[´A1´, ´E1´].Font.Color      := RGB(0, 0, 255);
  ExcelWorkSheet1.Range[´A1´, ´E1´].Font.Name       := ´Verdana´;
  ExcelWorkSheet1.Range[´A1´, ´E1´].Interior.Color  := clSilver;

  Table1.First;
  while not Table1.Eof do
  begin
    Inc(intContaLinha);

    oleCelula := ´A´ + IntToStr(intContaLinha);
    ExcelWorkSheet1.Range[oleCelula, oleCelula].Value
    [Table1Name.Value];

    oleCelula := ´B´ + IntToStr(intContaLinha);
    ExcelWorkSheet1.Range[oleCelula, oleCelula].Value
    [Table1Capital.Value];

    oleCelula := ´C´ + IntToStr(intContaLinha);
    ExcelWorkSheet1.Range[oleCelula, oleCelula].Value
    [Table1Continent.Value];

    oleCelula := ´D´ + IntToStr(intContaLinha);
    ExcelWorkSheet1.Range[oleCelula, oleCelula].Value
    [Table1Area.Value];

    oleCelula := ´E´ + IntToStr(intContaLinha);
    ExcelWorkSheet1.Range[oleCelula, oleCelula].Value
    [Table1Population.Value];

    Table1.Next;
  end;

  // Calcular totais das colunas D e E
  Inc(intContaLinha);
  oleCelula := ´D´ + IntToStr(intContaLinha);
  ExcelWorkSheet1.Range[oleCelula, oleCelula].Value
  [´=SOMA(D2:D´ + IntToStr(intContaLinha - 1) + ´)´];

  ExcelWorkSheet1.Range[oleCelula, oleCelula].Font.Color  := clRed;

  oleCelula := ´E´ + IntToStr(intContaLinha);
  ExcelWorkSheet1.Range[oleCelula, oleCelula].Value
  [´=SUM(E2:E´ + IntToStr(intContaLinha - 1) + ´)´];

  ExcelWorkSheet1.Range[oleCelula, oleCelula].Font.Color  := clRed;

end;

end.

DFM do Form:
object Form1: TForm1 Left = 192 Top = 107 Width = 358 Height = 151 Caption = ´Form1´ Color = clBtnFace Font.Charset = DEFAULT_CHARSET Font.Color = clWindowText Font.Height = -11 Font.Name = ´Arial´ Font.Style = [] OldCreateOrder = False Position = poDesktopCenter PixelsPerInch = 96 TextHeight = 14 object Label1: TLabel Left = 8 Top = 8 Width = 256 Height = 27 Caption = ´D6: MS-Excel XP tabela´ Font.Charset = DEFAULT_CHARSET Font.Color = clWindowText Font.Height = -24 Font.Name = ´Arial´ Font.Style = [] ParentFont = False end object btCopiar: TButton Left = 8 Top = 48 Width = 113 Height = 25 Caption = ´Copiar tabela´ TabOrder = 0 OnClick = btCopiarClick end object Table1: TTable Active = True DatabaseName = ´DBDEMOS´ TableName = ´country.db´ Left = 264 Top = 48 object Table1Name: TStringField FieldName = ´Name´ Size = 24 end object Table1Capital: TStringField FieldName = ´Capital´ Size = 24 end object Table1Continent: TStringField FieldName = ´Continent´ Size = 24 end object Table1Area: TFloatField FieldName = ´Area´ end object Table1Population: TFloatField FieldName = ´Population´ end end object ExcelApplication1: TExcelApplication AutoConnect = False ConnectKind = ckRunningOrNew AutoQuit = False Left = 144 Top = 48 end object ExcelWorkbook1: TExcelWorkbook AutoConnect = False ConnectKind = ckRunningOrNew Left = 184 Top = 48 end object ExcelWorksheet1: TExcelWorksheet AutoConnect = False ConnectKind = ckRunningOrNew Left = 224 Top = 48 end end
espero ter ajudado :wink:


Responder

Gostei + 0

Utilizamos cookies para fornecer uma melhor experiência para nossos usuários, consulte nossa política de privacidade.

Aceitar