unit UExcel_Sum; interface //在接口部分定义使用系统的程序单元文件 uses Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs, Db, DBTables, StdCtrls, Buttons, 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; Query1SUMOF2: TFloatField; Query1SUMOF3: TFloatField; Query1BDEDesigner: TStringField; YearComboBox: TComboBox; //定义保存动态年度的组合框,供用户选择 SeasonComboBox: TComboBox; //定义供用户选择季度、半年、全年的组合框 SeasonBtn: TBitBtn; PrintBtn: TBitBtn; YearBtn: TBitBtn; CloseBtn: TBitBtn; DBNavigator1: TDBNavigator; procedure CloseBtnClick(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:string; 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函数,获取系统日期和时间 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; //设置当前年份 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 TForm1.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:='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 '''+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 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.PrintBtnClick(Sender: TObject); {当用户单击打印按钮后,调用该过程。其功能是:根据当前的查询条件,重新从数据库中查询和统计各项目的工作量,并将其存入Excel中,此外,可根据用户的需要对其进行修改、保存、打印等操作。} var i,j,k:integer; Range1:Variant; begin Query1.close; Query1.Open; j:=Query1.RecordCount; //获取查询和统计的记录总数,如果查询记录数>0,则启动Excel,拷贝数据输出和报表 if j>0 then begin k:=j+1; XLApp:=CreateOleObject('Excel.Application'); //建立一个Excel应用的OLE对象 XLApp.Visible:=true; //使得Excel对象可视 XLApp.Application.caption:='工作量统计'; //设置Excel应用的标题 XLApp.Workbooks.add(xlWBatWorkSheet); //在Excel中,增加一个空白页 XLApp.Workbooks[1].WorkSheets[1].name:='工作量统计'; //设置工作簿中的工作表的名字 Range1:=XLApp.Workbooks[1].WorkSheets['工作量统计'].Range['A1:E'+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.cells[i,1]:=IntToStr(i-1); XLApp.cells[i,2]:=Query1.fields[0].AsString; //依次将字段值存入表中的各列 XLApp.cells[i,3]:=Query1.fields[1].value; XLApp.cells[i,4]:=Query1.fields[2].value; XLApp.cells[i,5]:=Query1.fields[3].value; Query1.next; end; //for i:=2 XLApp.Workbooks[1].WorkSheets[1].printout; //完成表格生成后,打印输出 end; // if j>0 XLApp.quit; //释放OLE对象的Excel end; procedure TForm1.CloseBtnClick(Sender: TObject); //当用户单击了关闭按钮后,调用该过程 begin close; //关闭窗体,终止应用程序的执行 end; end.
以OLE方式建立与Excel连接
最新推荐文章于 2023-11-03 15:16:33 发布