?? 實例——以ole方式建立與excel連接.txt
字號:
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.
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -