当前位置:首页 > 网络编程 > 软件语言 > DELPHI > 用自动化往Word、Excel中输出数据库内容

用自动化往Word、Excel中输出数据库内容

点击次数:44 次 发布日期:2008-11-09 08:38:31 作者:源代码网
源代码网推荐
广告载入中
unit OfficeForm;
源代码网推荐
源代码网推荐 interface
源代码网推荐
源代码网推荐 uses
源代码网推荐 SysUtils, Windows, Messages, Classes, Graphics,
源代码网推荐 Controls, Forms, DBCtrls, StdCtrls, DBTables,
源代码网推荐 ExtCtrls, Mask, Db, Dialogs, Excel97, Word97,
源代码网推荐 OleServer;
源代码网推荐
源代码网推荐 type
源代码网推荐 TFormOff = class(TForm)
源代码网推荐 DBEdit3: TDBEdit;
源代码网推荐 Label3: TLabel;
源代码网推荐 Label2: TLabel;
源代码网推荐 DBEdit2: TDBEdit;
源代码网推荐 DBEdit1: TDBEdit;
源代码网推荐 Label1: TLabel;
源代码网推荐 DBNavigator1: TDBNavigator;
源代码网推荐 Table1: TTable;
源代码网推荐 DataSource1: TDataSource;
源代码网推荐 BtnWord: TButton;
源代码网推荐 BtnExcel: TButton;
源代码网推荐 SaveDialog1: TSaveDialog;
源代码网推荐 ExcelApplication1: TExcelApplication;
源代码网推荐 WordDocument1: TWordDocument;
源代码网推荐 procedure BtnWordClick(Sender: TObject);
源代码网推荐 procedure BtnExcelClick(Sender: TObject);
源代码网推荐 end;
源代码网推荐
源代码网推荐 var
源代码网推荐 FormOff: TFormOff;
源代码网推荐
源代码网推荐 implementation
源代码网推荐
源代码网推荐 {$R *.DFM}
源代码网推荐
源代码网推荐 uses
源代码网推荐 ComObj, ActiveX;
源代码网推荐
源代码网推荐 procedure TFormOff.BtnWordClick(Sender: TObject);
源代码网推荐 var
源代码网推荐 Bookmark: TBookmark;


源代码网推荐 RangeW: Word97.Range;
源代码网推荐 v1: Variant;
源代码网推荐 ov1: OleVariant;
源代码网推荐 Row1: Word97.Row;
源代码网推荐 begin
源代码网推荐 WordDocument1.Activate;
源代码网推荐 // insert title
源代码网推荐 WordDocument1.Range.Text := "American Capitals from " Table1.TableName;
源代码网推荐 WordDocument1.Range.Font.Size := 14;
源代码网推荐 // disable the UI
源代码网推荐 Table1.DisableControls;
源代码网推荐 try
源代码网推荐 // store the current position
源代码网推荐 Bookmark := Table1.GetBookmark;
源代码网推荐 try
源代码网推荐 // scan the database table
源代码网推荐 Table1.First;
源代码网推荐 while not Table1.EOF do
源代码网推荐 begin
源代码网推荐 // send the two fields
源代码网推荐 WordDocument1.Range.InsertParagraphAfter;
源代码网推荐 WordDocument1.Paragraphs.Last.Range.Text :=
源代码网推荐 Table1.FieldByName ("Name").AsString #9
源代码网推荐 Table1.FieldByName ("Capital").AsString;
源代码网推荐 Table1.Next;
源代码网推荐 end;
源代码网推荐 finally
源代码网推荐 // go back to the bookmark and destroy it
源代码网推荐 Table1.GotoBookmark (Bookmark);
源代码网推荐 Table1.FreeBookmark (Bookmark);
源代码网推荐 end;
源代码网推荐 finally
源代码网推荐 // re-enable the controls
源代码网推荐 Table1.EnableControls;
源代码网推荐 end;
源代码网推荐 RangeW := WordDocument1.Content; 软件开发网 www.mscto.com
源代码网推荐 v1 := RangeW;
源代码网推荐 v1.ConvertToTable (#9, 19, 2);
源代码网推荐 Row1 := WordDocument1.Tables.Item(1).Rows.Get_First;
源代码网推荐 Row1.Range.Bold := 1;
源代码网推荐 Row1.Range.Font.Size := 30;
源代码网推荐 Row1.Range.InsertParagraphAfter;
源代码网推荐 ov1 := " ";
源代码网推荐 Row1.ConvertToText (ov1);
源代码网推荐 end;
源代码网推荐
源代码网推荐 procedure TFormOff.BtnExcelClick(Sender: TObject);
源代码网推荐 var
源代码网推荐 RangeE: Excel97.Range;
源代码网推荐 I, Row: Integer;
源代码网推荐 Bookmark: TBookmarkStr;
源代码网推荐 begin
源代码网推荐 // create and show
源代码网推荐 ExcelApplication1.Visible [0] := True;
源代码网推荐 ExcelApplication1.Workbooks.Add (NULL, 0);
源代码网推荐 // fill is the first row with field titles
源代码网推荐 RangeE := ExcelApplication1.ActiveCell;
源代码网推荐 for I := 0 to Table1.Fields.Count - 1 do
源代码网推荐 begin
源代码网推荐 RangeE.Value := Table1.Fields [I].DisplayLabel;
源代码网推荐 RangeE := RangeE.Next;
源代码网推荐 end;
源代码网推荐 // add field data in following rows
源代码网推荐 Table1.DisableControls;
源代码网推荐 try
源代码网推荐 Bookmark := Table1.Bookmark;
源代码网推荐 try
源代码网推荐 Table1.First;
源代码网推荐 Row := 2;
源代码网推荐 while not Table1.EOF do
源代码网推荐 begin
源代码网推荐 RangeE := ExcelApplication1.Range ["A" IntToStr (Row),


源代码网推荐 "A" IntToStr (Row)];
源代码网推荐 for I := 0 to Table1.Fields.Count - 1 do
源代码网推荐 begin
源代码网推荐 RangeE.Value := Table1.Fields [I].AsString;
源代码网推荐 RangeE := RangeE.Next;
源代码网推荐 end;
源代码网推荐 Table1.Next;
源代码网推荐 Inc (Row);
源代码网推荐 end;
源代码网推荐 finally
源代码网推荐 Table1.Bookmark := Bookmark;
源代码网推荐 end;
源代码网推荐 finally
源代码网推荐 Table1.EnableControls;
源代码网推荐 end;
源代码网推荐 // format the section
源代码网推荐 RangeE := ExcelApplication1.Range ["A1", "E" IntToStr (Row - 1)];
源代码网推荐 RangeE.AutoFormat (3, NULL, NULL, NULL, NULL, NULL, NULL);
源代码网推荐 end;
源代码网推荐
源代码网推荐 initialization
源代码网推荐 CoInitialize (nil);
源代码网推荐 end.


源代码网推荐

源代码网供稿.
网友评论 (0)
会员中心
网络编程
本站推荐
网络编程之精华