《建立与execel联系.pdf》由会员分享,可在线阅读,更多相关《建立与execel联系.pdf(16页珍藏版)》请在得力文库 - 分享文档赚钱的网站上搜索。
1、建立与建立与 execelexecel 联系联系 unit UJoin_excel; interface uses /定义使用系统的单元文件 Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms,Dialogs, StdCtrls, Buttons, OleServer, ExtCtrls, DBCtrls, Grids, DBGrids, Db, DBTables, Excel2000,Variants; /如果用户使用的不是 Delphi 7(如 Delphi 5),则可能要指定 Excel97,而非 Excel20
2、00 type TForm1 = class(TForm) /定义在窗体上使用的组件,包括作为自动化服务器的 Excel 组件 ExcelApplication1: TExcelApplication; ExcelWorksheet1: TExcelWorksheet; ExcelWorkbook1: TExcelWorkbook; SaveDialog1: TSaveDialog; ConnectBtn: TBitBtn; Query1: TQuery; DataSource1: TDataSource; DBGrid1: TDBGrid; DBNavigator1: TDBNavigato
3、r; InsertDataBtn: TBitBtn; SaveBtn: TBitBtn; QuitBtn: TBitBtn; CloseAppBtn: TBitBtn; Label1: TLabel; /如下定义按钮的事件处理过程 procedure FormCreate(Sender: TObject); procedure ConnectBtnClick(Sender: TObject); procedure InsertDataBtnClick(Sender: TObject); procedure SaveBtnClick(Sender: TObject); procedure Qui
4、tBtnClick(Sender: TObject); procedure CloseAppBtnClick(Sender: TObject); private Private declarations public Public declarations end; var Form1: TForm1; implementation uses ComObj; $R *.DFM procedure TForm1.FormCreate(Sender: TObject); /建立窗体时的事件处理过程 begin Query1.close; Query1.Open; /获取用于制作电子表的数据库信息
5、showmessage(欢迎你使用 Excel 自动化服务器!); /showmessage(IntToStr(Query1.FieldCount); end; procedure TForm1.ConnectBtnClick(Sender: TObject); /连接 Excel 按钮的事件处理过程,启动 Excel,并设置相关标题 begin Try ExcelApplication1.Connect; /建立与 Excel 的连接,如果出错则显示提示信息 Except MessageDlg(Excel2000 是否已正确安装,mtError, mbOk, 0); Abort; /如果出错
6、,不报告错误信息,结束当前程序的执行 End; ExcelApplication1.Visible0:=True; /设置成可视状态 ExcelApplication1.Caption:=Delphi 连接 Excel 应用; /设置 Excel 的标题 ExcelApplication1.Workbooks.Add(Null,0); ExcelApplication1.Workbooks.Add(emptyParam,0); /增加一个新的空工作簿 ExcelWorkbook1.ConnectTo(ExcelApplication1.Workbooks1); /连接工作簿 ExcelWork
7、sheet1.ConnectTo(ExcelWorkbook1.Worksheets1 as_Worksheet); /连接工作表 end; procedure TForm1.InsertDataBtnClick(Sender: TObject); /插入数据按钮的事件处理过程,在 Excel 工作表中生成需要表格 /并将数据库的内容写入 Excel 的工作表中 var i,row,column:integer; /定义用于控制单元格的变量 begin /如下设置单元格第一行的内容,即各列的标题,本表有 7 列组成 ExcelWorksheet1.Cells.Item1,1:=时间; /设置第
8、一列的标题 ExcelWorksheet1.Cells.Item1,2:=年度指标; ExcelWorksheet1.Cells.Item1,3:=当年拨款; ExcelWorksheet1.Cells.Item1,4:=支出数额; ExcelWorksheet1.Cells.Item1,5:=余额; ExcelWorksheet1.Cells.Item1,6:=超支; ExcelWorksheet1.Cells.Item1,7:=上年节余; /设置第 7 列的标题 row:=2; While Not(Query1.Eof) do /此循环将满足条件的数据库记录写入 Excel 的工作表中 b
9、egin column:=1; for i:=1 to Query1.FieldCount do /此循环控制写出的列数 beginExcelWorksheet1.Cells.Itemrow,column:=Query1.fieldsi-1.AsString; /为工作表的单元格赋值,row,column 用于控制行和列 column:=column+1; end; Query1.Next; /获取下一个数据库记录 row:=row+1; end; end; procedure TForm1.SaveBtnClick(Sender: TObject); /保存按钮的事件处理过程, 将 Exce
10、l 表中的数据保存到用户指定的文件中 begin if SaveDialog1.Execute then ExcelWorksheet1.SaveAs(SaveDialog1.FileName); /根据对话框输入的文件名,保存工作表 end; procedure TForm1.QuitBtnClick(Sender: TObject); /退出 Excel 按钮对话框,关闭 Excel,并解除与 Excel 的连接 begin ExcelApplication1.Quit; /关闭 Excel,并显示是否保存的提示信息 ExcelApplication1.Disconnect; /解除与 E
11、xcel 自动化服务器的连接 end; procedure TForm1.CloseAppBtnClick(Sender: TObject); /关闭应用,结束程序的运行 begin close; /调用窗体的 close 方法。 end; end.在在 delphidelphi 中操作中操作 wordword unit UJoin_word; interface uses /指定使用系统的单元文件 Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms,Dialogs, StdCtrls, OleServer, Butt
12、ons, DBCtrls, Db, Grids, DBGrids, DBTables, ExtCtrls, Menus, Word2000,Variants; 根据用户在安装 Delphi 时,指定使用Office97 还是 Office2000,使用的单元文件不同如果指定使用 Office97,则应包括 Word97,如果指定使用Office2000,则应包括 Word2000; type TForm1 = class(TForm) /定义在窗体上使用的组件 WordApplication1: TWordApplication; /连接 Word 应用组件 WordDocument1: TW
13、ordDocument; /连接 Word 文档组件 Edit1: TEdit; ConnectWordBtn: TBitBtn; DataSource1: TDataSource; Table1: TTable; DBGrid1: TDBGrid; DBMemo1: TDBMemo; DBNavigator1: TDBNavigator; WordFont1: TWordFont; /该变字体组件 ComboBoxFont: TComboBox; MainMenu1: TMainMenu; /菜单组件 Word1: TMenuItem; Start: TMenuItem; N2: TMenu
14、Item; Word2: TMenuItem; N3: TMenuItem; Append: TMenuItem; Save: TMenuItem; PreView: TMenuItem; print: TMenuItem; Insert: TMenuItem; Close: TMenuItem; QuitRun: TMenuItem; /定义使用数据库的组件 Table1BDEDesigner: TIntegerField; Table1BDEDesigner2: TStringField; Table1BDEDesigner3: TDateField; Table1BDEDesigner4
15、: TStringField; Table1BDEDesigner5: TStringField; Table1BDEDesigner6: TStringField; Table1BDEDesigner7: TStringField; /如下定义事件处理过程 procedure FormCreate(Sender: TObject); procedure ComboBoxFontChange(Sender: TObject); procedure AppendClick(Sender: TObject); procedure StartClick(Sender: TObject); proce
16、dure InsertClick(Sender: TObject); procedure SaveClick(Sender: TObject); procedure PreViewClick(Sender: TObject); procedure printClick(Sender: TObject); procedure CloseClick(Sender: TObject); procedure QuitRunClick(Sender: TObject); private Private declarations public Public declarations end; var Fo
17、rm1: TForm1; implementation $R *.DFM procedure TForm1.FormCreate(Sender: TObject); /当建立窗体时,调用该过程,它完成菜单初始设置 begin Table1.close; Table1.open; ComboBoxFont.Items:=Screen.Fonts; /将各种字体保存到带有下拉列表的组合框中 WordFont1.ConnectTo(WordDocument1.Sentences.Get_last.Font); WordFont1.Bold:=1; MainMenu1.Items0.enabled:=
18、True; /开始时,允许进行连接 Word 操作 MainMenu1.Items1.enabled:=False; /在连接 Word 之前,不允许进行文档处理 MainMenu1.Items2.enabled:=False; /在连接 Word 之前,不允许关闭 Word 的操作 edit1.selectAll; edit1.CopyToClipboard; /将编辑框的内容拷贝到粘贴板上 end; procedure TForm1.AppendClick(Sender: TObject); /该事件处理过程将粘贴板上的内容拷贝到 Word 文档中 begin WordDocument1.
19、Sentences.Last.Paste; /将粘贴板上的内容拷贝到文档中 WordDocument1.Range.InsertAfter(#13); /在文档的后面加上换行符 end; procedure TForm1.StartClick(Sender: TObject); var Template,NewTemplate,ItemIndex:OleVariant; /开始菜单事件处理过程, 连接 Word 应用,设置 Word 文档的标题和菜单选项 begin Template:=EmptyParam; NewTemplate:=true; ItemIndex:=1; try WordA
20、pplication1.connect; /使用 WordApplication 组件的 connect 方法,启动 Word except /如出现异常,显示提示信息并终止连接 messageDlg(Word2000 是否已正确安装,mtError,mbOk,0); abort; end; WordApplication1.Visible:=True; /使 Word 应用成为可视 WordApplication1.caption:=Delphi 连接 Word(Automation 应用); /设置文档标题WordDocument1.connectTo(WordApplication1.D
21、ocuments.Item(ItemIndex); /将文档组件 WordDocument1 和应用组件 WordApplication1 相关联 MainMenu1.Items0.enabled:=False; /在连接 Word 之后至关闭之前,不允许再连接 Word MainMenu1.Items1.enabled:=True; /在连接 Word 之后,可完成文档处理操作 MainMenu1.Items2.enabled:=True; /在连接 Word 之后,允许关闭 Word end; procedure TForm1.InsertClick(Sender: TObject); /
22、在 Word 文档的后面插入信息,如果出现错误,则显示错误信息并解除与Word 的连接 begin tryWordDocument1.Range.InsertAfter(table1.fields3.AsString+#13); /在文档的最后插入 Except on E:Exception do /如出现异常,显示提示信息 begin Showmessage(E.Message); WordApplication1.Disconnect; /如插入出错,则解除连接 Table1.close; /关闭数据库表 end; end; end; procedure TForm1.SaveClick(
23、Sender: TObject); /保存文档菜单项事件处理过程,将新建的文档保存到指的位置和文件中 var SaveDocFile,EmptyPara:OleVariant; begin SaveDocFile:=c:file1.doc; /指定保存文档的位置 EmptyPara:=EmptyParam; WordDocument1.SaveAs(SaveDocFile,EmptyPara); /调用 SaveAs 方法,保存 Word 文档 end; procedure TForm1.PreViewClick(Sender: TObject); /预览打印文档菜单项事件 begin Wor
24、dDocument1.PrintPreView; /调用 PrintPreView 方法,预览打印文档 end; procedure TForm1.printClick(Sender: TObject); / 打印文档菜单项事件处理过程 begin WordDocument1.PrintOut; /调用 PrintOut 方法,完成 Word 文档的打印 end; procedure TForm1.CloseClick(Sender: TObject); /关闭菜单项事件处理过程 var SaveChanges,OriginalFormat,RouteDocument:OleVariant;
25、begin SaveChanges:=wdDoNotSaveChanges; /wdDoNotSaveChanges 是一个常数,在 Word2000.pas 单元中定义, / 其值为$00000000 OriginalFormat:=UnAssigned; RouteDocument:=UnAssigned; try WordApplication1.Disconnect; /解除与 Word 程序建立的连接WordApplication1.Quit(SaveChanges,OriginalFormat,RouteDocument); /Quit 方法在 Word2000.pas 单元中定义
26、, 调用它将退出并关闭 Word程序, /此处调用将不保存文档的内容 finally end; end; procedure TForm1.ComboBoxFontChange(Sender: TObject); /改变字体事件处理过程 begin WordFont1.ConnectTo(WordDocument1.Sentences.Get_last.Font); WordFont1.Name:=ComboBoxFont.text; WordFont1.Bold:=2; /将显示文档的字体改成黑体 WordFont1.Italic:=1; /将显示文档的字体改成斜体 WordFont1.Si
27、ze:=16; /将显示文档的字体大小改成 16 end; procedure TForm1.QuitRunClick(Sender: TObject); /退出菜单项事件处理过程 begin exit; end; end.用用 oleole 方式建立方式建立 excelexcel 联系联系 unit UExcel_Sum; interface /在接口部分定义使用系统的程序单元文件 uses Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms,Dialogs, Db, DBTables, StdCtrls, Butt
28、ons, Grids, DBGrids, OleServer, Excel2000,ExtCtrls, DBCtrls; /如果安装 Delphi 时,指定的不是 Office 2000(如 Office 97) /则应指定 Excel97,而不是 Excel2000 type TForm1 = class(TForm) /如下定义本窗体使用的组件 TitleLabel: TLabel; DataSource1: TDataSource; Query1: TQuery; /查询组件,完成数据库查询和工作量统计 DBGrid1: TDBGrid; Query1SUMOF: TFloatField
29、; Query1SUMOF2: TFloatField; Query1SUMOF3: TFloatField; Query1BDEDesigner: TStringField; YearComboBox: TComboBox; /定义保存动态年度的组合框,供用户选择 SeasonComboBox: TComboBox; /定义供用户选择季度、半年、全年的组合框 SeasonBtn: TBitBtn; PrintBtn: TBitBtn; YearBtn: TBitBtn; CloseBtn: TBitBtn; DBNavigator1: TDBNavigator; procedure Clos
30、eBtnClick(Sender: TObject); /定义事件处理过程, procedure FormCreate(Sender: TObject); procedure YearComboBoxChange(Sender: TObject); procedure SeasonComboBoxChange(Sender: TObject); procedure PrintBtnClick(Sender: TObject); private Private declarations /定义私有的全局变量,供各过程使用 year1,date1:string; month1,month2:str
31、ing; XLApp:Variant; public Public declarations end; var Form1: TForm1; implementation uses ComObj; $R *.DFM procedure TForm1.FormCreate(Sender: TObject); /在窗体建立的过程中, 获取计算机系统的当前时钟,并存入组合框中形成年度选择串 var Year, Month, Day:Word; /定义年、月、日变量 Present:TDateTime; i:integer; begin Present:= Now; /使用 Now 函数,获取系统日期
32、和时间 DecodeDate(Present, Year, Month, Day); /分解从系统获取的时间,即拆成年、月、日时间串 YearComboBox.items.clear; YearComboBox.itemindex:=0; YearComboBox.text:=inttostr(Year); /指定当前年份 for i:=1 to 6 do begin YearComboBox.items.add(inttostr(year); /形成年度下拉列表,从当前时间开始,最多 6 年 year:=year-1; end; year1:=YearComboBox.text; /设置当前
33、年份 month1:=01/01/; month2:=12/31/; date1:=+month1+year1+ and +month2+year1+; /形成查询条件的时间区间, Query1.Close; Query1.sql.clear;Query1.sql.Add(Select 名称,sum(数量),sum(次数),sum(参加人次) from GongZuoLiang where 时间 between +date1+ group by 名称 order by 名称); /形成查询统计的 SQL 语句 Query1.open; /从数据库统计数据 end; procedure TFor
34、m1.SeasonComboBoxChange(Sender: TObject); /当用户重新选择季度后,调用本过程。 begin if SeasonComboBox.text=一季度 then /当用户选择季度后,确定查询的月、日起止区间 begin month1:=01/01/; month2:=03/31/; end; if SeasonComboBox.text=二季度 then begin month1:=04/01/; month2:=06/30/; end; if SeasonComboBox.text=三季度 then begin month1:=07/01/; month2
35、:=09/30/; end; if SeasonComboBox.text=四季度 then begin month1:=10/01/; month2:=12/31/; end; if SeasonComboBox.text=上半年 then /选择上半年后,确定查询的起止日期 begin month1:=01/01/; month2:=06/30/; end; if SeasonComboBox.text=全年 then /选择全年后,确定查询的起止日期 begin month1:=01/01/; month2:=12/31/; end; date1:=+month1+year1+ and
36、+month2+year1+; /重新合成动态查询的年、月、日的起止区间 Query1.Close; Query1.sql.clear;Query1.sql.Add(Select 名称,sum(数量),sum(次数),sum(参加人次) from GZLiang where 时间 between +date1+ group by 名称 order by 名称); /根据新的条件,生成新的 SQL 语句 Query1.open; end; procedure TForm1.YearComboBoxChange(Sender: TObject); /当用户重新选择年度后,调用本过程。 begin
37、year1:=YearComboBox.text; date1:=+month1+year1+ and +month2+year1+; /根据用户选择的年度,重新生成查询条件 Query1.Close; Query1.sql.clear;Query1.sql.Add(Select 名称,sum(数量),sum(次数),sum(参加人次) from GongZuoLiang where 时间 between +date1+ group by 名称 order by 名称); /根据新的查询条件,动态生成新的 SQL 语句 Query1.open; end; procedure TForm1.Pr
38、intBtnClick(Sender: TObject); 当用户单击打印按钮后,调用该过程。其功能是:根据当前的查询条件,重新从数据库中查询和统计各项目的工作量,并将其存入 Excel 中,此外,可根据用户的需要对其进行修改、保存、打印等操作。 var i,j,k:integer; Range1:Variant; begin Query1.close; Query1.Open; j:=Query1.RecordCount; /获取查询和统计的记录总数,如果查询记录数0,则启动 Excel,拷贝数据输出和报表 if j0 then begin k:=j+1; XLApp:=CreateOleO
39、bject(Excel.Application); /建立一个 Excel 应用的 OLE 对象 XLApp.Visible:=true; /使得 Excel 对象可视 XLApp.Application.caption:=工作量统计; /设置 Excel 应用的标题 XLApp.Workbooks.add(xlWBatWorkSheet); /在 Excel 中,增加一个空白页 XLApp.Workbooks1.WorkSheets1.name:=工作量统计; /设置工作簿中的工作表的名字 Range1:=XLApp.Workbooks1.WorkSheets工作量统计.RangeA1:E+
40、inttostr(k); /设置工作表的存取范围 Range1.Borders.LineStyle:=xlContinuous; /在工作表的范围内,增加表格线 /如下几行是设置工作表的表头 XLApp.Range(A1):=序号 ; XLApp.Range(B1):=项目名称 ; XLApp.Range(C1):=数量 ; XLApp.Range(D1):=次数; XLApp.Range(E1):=参加人次; for i:=2 to j+1 do /第 1 行为标题, 从第 2 行起为表格的正式内容一次循环,填写一行表格的数据 begin XLApp.cellsi,1:=IntToStr(i
41、-1); XLApp.cellsi,2:=Query1.fields0.AsString; /依次将字段值存入表中的各列 XLApp.cellsi,3:=Query1.fields1.value; XLApp.cellsi,4:=Query1.fields2.value; XLApp.cellsi,5:=Query1.fields3.value; Query1.next; end; /for i:=2 XLApp.Workbooks1.WorkSheets1.printout; /完成表格生成后,打印输出 end; / if j0 XLApp.quit; /释放 OLE 对象的 Excel e
42、nd; procedure TForm1.CloseBtnClick(Sender: TObject); /当用户单击了关闭按钮后,调用该过程 begin close; /关闭窗体,终止应用程序的执行 end; end.用用 tworddocumenttworddocument 组件连接组件连接 wordword 自动自动化服务器化服务器 unit UJoin_Word2; interface uses /使用系统的单元文件 Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls,Forms, Dialogs, St
43、dCtrls, OleServer, Word2000, Buttons; type TForm1 = class(TForm) /定义窗体上使用的组件 WordDocument1: TWordDocument; Join_Btn: TBitBtn; Save_Btn: TBitBtn; Quit_Btn: TBitBtn; Close_Btn: TBitBtn; Label1: TLabel; /定义事件处理过程 procedure Button1Click(Sender: TObject); procedure Save_BtnClick(Sender: TObject); procedu
44、re Quit_BtnClick(Sender: TObject); procedure Close_BtnClick(Sender: TObject); private Private declarations public Public declarations end; var Form1: TForm1; implementation $R *.dfm procedure TForm1.Button1Click(Sender: TObject); /连接 Word 自动化服务器 begin WordDocument1.Connect; /调用 TOleServer 的 Connect
45、方法,启动作为自动化服务器的 Word 程序 try WordDocument1.Content.Text := 欢迎你使用 Word 自动化服务器!; /设置 Word 文档的内容 finally / WordDocument1.Disconnect; end; end; procedure TForm1.Save_BtnClick(Sender: TObject); var FileName : OleVariant; /定义一个文件名变量 begin FileName := c:Mydocument.doc; /设置输出文档的文件名 WordDocument1.SaveAs(FileName); /调用 SaveAs 方法将文档按指定的文件名保存 end; procedure TForm1.Quit_BtnClick(Sender: TObject); begin WordDocument1.Disconnect; /断开与 Word 自动化服务器的连接 end; procedure TForm1.Close_BtnClick(Sender: TObject); begin Form1.Close; /关闭窗体,退出应用 end; end.
限制150内