#define PG OlePropertyGet
#define PS OlePropertySet
#define FN OleFunction
#define PR OleProcedure
要在應用程序中控制Excel2000的運行,首先必須在編制自動化客戶程序時使其頭文件要包含Comobj.hpp和Utilcls.h。
即:#include<Comobj.hpp>
#include<Utilcls.h>
C++ Builder開發者把Excel自動化對象的功能包裝在下面的四個Ole Object Class函數中,應用人員可以很方便地進行調用。
設置對象屬性:Variant OlePropertySet(屬性名,參數……);
獲得對象屬性:void OlePropertyGet(屬性名,參數……);
調用對象方法:1) Variant OleFunction(函數名,參數……);
2) void OleProcedure(過程名,參數……);
C++ Builder中使用OLE控制Excel2000,必須掌握Excel2000的自動化對象及Microsoft Word Visual Basic幫助文件中的關於Excel的對象、方法和屬性。對象是一個Excel元素,屬性是對象的一個特性或操作的一個方面,方法是對象可以進行的動作。
1、Excel中常用的對象是:Application,Workbooks,Worksheets等。
(1) 創建應用對象:如:
Variant ex;
ex=Variant::CreateObject ("Excel.Application");
或者 ex=CreateOleObject ("Excel.Application");
(2) 創建工作簿對象:
Variant wb;
wb=ex.OlePropertyGet("ActiveWorkBook");
(3) 創建工作表對象:
Variant sheet;
sheet=wb.OlePropertyGet("ActiveSheet");
(4) 創建區域對象:
Variant range;
range=sheet.OlePropertyGet("Range","A1:A10");
2、常用的屬性操作:
(1)新建EXCEL文件:
(a):新建系統模板的工作簿
ex.OlePropertyGet("workbooks").OleFunction("Add") //默認工作簿
ex.OlePropertyGet("workbooks").OleFunction("Add",1) //單工作表
ex.OlePropertyGet("workbooks").OleFunction("Add",2) //圖表
ex.OlePropertyGet("workbooks").OleFunction("Add",3) //宏表
ex.OlePropertyGet("workbooks").OleFunction("Add",4) //國際通用宏表
ex.OlePropertyGet("workbooks").OleFunction("Add",5) //與默認的相同
ex.OlePropertyGet("workbooks").OleFunction("Add",6) //工作簿且只有一個表
(b):新建自己創建的模板的工作簿
ex.OlePropertyGet("workbooks").OleFunction("Add","C://WINDOWS//Profiles//test2//Application Data//Microsoft//Templates//result.xlt"); // 後面寫上模板的完全路徑,注意“//”
(2)打開工作簿:
ex.OlePropertyGet("workbooks").OleFunction("open","路徑名.xls")
(3)保存工作簿:
wb.OleFunction("Save"); //表格保存
wb..OleFunction("SaveAs","文件名"); //表格保存爲,文件路徑注意用“//”
(4)退出EXCEL:
ex.OleFunction ("Quit");
(5)設置字體:
(a):設置單元格字體
sheet.OlePropertyGet("Cells",1,1).OlePropertyGet("Font").OlePropertySet("Name","隸書");
sheet.OlePropertyGet("Cells",2,3).OlePropertyGet("Font").OlePropertySet("size",28);
(b):設置所選區域字體
range.OlePropertyGet("Cells").OlePropertyGet("Font").OlePropertySet("size",28); range.OlePropertyGet("Cells").OlePropertyGet("Font").OlePropertySet("Color",
RGB(0,0,255));
其中參數的設置:
Font---Name : “隸書” //字體名稱
----Size : 12 //字體大小
----Color : RGB(*,*,*) //顏色
-----Underline : true/false //下劃線
-----Italic: true/false //斜體
(6)單元格的合併:
(a) range1=sheet.OlePropertyGet("Range", "A1:A2"); //A1和A2單元格合併
(b) AnsiString Str="A"+IntToStr(j)+":"+"C"+IntToStr(j);
range1=sheet.OlePropertyGet("Range",Str); //可以用變量控制單元格合併
range1.OleFunction("Merge" , false);
(7)賦值語句:
(a):指定單元格賦值
sheet.OlePropertyGet("Cells",3,6).OlePropertySet("Value",str);
sheet.OlePropertyGet("Cells",j,1).OlePropertySet("Value","共查到記錄:"+IntToStr(j-6));
(b):所選區域單元格賦值
range.OlePropertyGet("Cells").OlePropertySet("Value",10);
(c):所選區域行賦值
range.OlePropertyGet("Rows",1).OlePropertySet("Value",1234);
(d):工作表列賦值
sheet.OlePropertyGet("Columns",1).OlePropertySet("Value",1234);
(8)取值語句:
AnsiString abc=sheet.OlePropertyGet("Cells",120,1).OlePropertyGet("Value");
(9)區域選擇:
range.OlePropertyGet("Cells").OleFunction("Select");
(10)窗口屬性:
(a)顯示屬性
ex.OlePropertySet("Windowstate",3); //最大化顯示
參數 1---------xlNormal //正常顯示
2---------xlMinimized //最小化顯示
3---------xlMaximized //最大化顯示
(b)狀態欄屬性
ex.OlePropertySet ("StatusBar","您好,請您稍等。正在查詢!");
ex.OlePropertySet ("StatusBar", false); //還原成默認值
(c)標題屬性:
ex.OlePropertySet("Caption","查詢系統");