Anda di halaman 1dari 26

使用 BCB 透過 Ole 操控 Excel 以製作報

製作人: sunwingman
大綱
 建立 Excel 初始環境
 Sheet 操作
 Cell 操作
 表格製作
 產生圖表
 文件保護
 列印版面設定
 參考文件
建立 Excel 初始環境
 Variant Excel,Workbooks,Workbook;
 建立 excel 物件:
 Excel=Variant::CreateObject("Excel.Application");
 Excel.OlePropertySet(“Visible”,true); // 使建立 Excel 程序可

建立 Excel 初始環境
 開啟一個新的 excel 檔案:
 Workbooks=Excel.OlePropertyGet("Workbooks");
 Workbook=Workbooks.OleFunction("Add");
Sheet 操作
 Variant Worksheets,Worksheet;
 添加 sheet :
 Worksheets=Workbook.OlePropertyGet("Worksheets");
 Worksheet=Worksheets.OleFunction("Add");

ResponseTime

 更改 sheet 名稱:
 Worksheet.OlePropertySet("Name","ResponseTime");
Sheet 操作
 設定特定 sheet 作用中:
 Worksheet=Worksheets.OlePropertyGet("Item", 1);
 Worksheet.OlePropertyGet("Activate");

1 2 3 4 5

使「 ResponseTime 」之 Sheet 作用中


Sheet 操作
 新增 sheet 到特定 sheet 位置後面:
 Worksheet=Worksheets.OlePropertyGet("Item",4);
 Workbook.OlePropertyGet("Sheets").OleProcedure("Add"
,Worksheet.NoParam(),Worksheet);

到目前為止,可以發現
共有四個重要的函式:
OlePropertyGet
// 取得類別屬性
OlePropertySet
// 設定類別屬性
以預設名稱「 sheet5 」出現 OleFunction
// 使用類別方法
OleProcedure
// 呼叫類別方法
10

Cell 操作 Q Hi

 Variant Cell;
 Cell 賦值:
 Cell=Worksheet.OlePropertyGet("Cells",5,5);
 Cell.OlePropertySet("Value",“Hi");
 Cell=Worksheet.OlePropertyGet("Cells",2,2);
 Cell.OlePropertySet("Value",10);

 內容置中&設定底色:
 Cell=Worksheet.OlePropertyGet("Cells",5,2); xlRight
xlLeft
 Cell.OlePropertySet("Value",“Q");
 Cell.OlePropertySet("HorizontalAlignment",xlCenter);
 Cell.OlePropertySet("VerticalAlignment",xlCenter);
 Cell.OlePropertyGet("Interior").OlePropertySet("Color
Index",16);
3,124

Cell 操作 10.50

 設定儲存格數字顯示位數:
 Cell=Worksheet.OlePropertyGet("Cells",2,2);
 Cell.OlePropertySet("Value",3123.6);
 Cell.OlePropertySet(“NumberFormatLocal”,”#,##0”);

 Cell=Worksheet.OlePropertyGet("Cells",4,4);
 Cell.OlePropertySet("Value",10.5);
 Cell.OlePropertySet(“NumberFormatLocal”,”#,##0.00”);
Cell 操作
 內容格式設定:
 Cell.OlePropertyGet(“Font”).OlePropertySet(“Name”,“ 標楷
體 ");
// 設定字體
 Cell.OlePropertyGet("Font").OlePropertySet(“Size",10);
OlePropertySet("Color",RGB(,,));
// 設定字型大小
 Cell.OlePropertyGet("Font").OlePropertySet("ColorIndex",3);
// 設定字體顏色
 Cell.OlePropertyGet("Font"). OlePropertySet("Bold",true);
// 設定字為粗體
 Cell.OlePropertyGet("Font").OlePropertySet("Italic",true);
// 設定字為斜體
 Cell.OlePropertyGet("Font").OlePropertySet("Underline",true)
;
Cell 操作
 Variant Range;
 Range 操作:
 Range=Worksheet.OlePropertyGet("Range",“A1:E5");
 Range.OlePropertySet("HorizontalAlignment",xlCenter);
 Range.OlePropertySet("VerticalAlignment",xlCenter);
 Range.OlePropertySet("Value","Hi");
 Range.OlePropertyGet("Font").OlePropertySet("ColorIndex"
,3);
Cell 操作
 Range 實際演練:
 Range=Worksheet.OlePropertyGet(“Range”,“A1:E8");
 Range.OlePropertySet("HorizontalAlignment",xlCenter);
 Range.OlePropertySet("VerticalAlignment",xlCenter);

 Range=Worksheet.OlePropertyGet("Range",“B1:E1");
 Range.OleFunction("Merge");
 Range.OlePropertySet("Value",“To Gray Level");

 Range=Worksheet.OlePropertyGet("Range",“A2:A8");
 Range.OleFunction("Merge");
 Range.OlePropertySet("Value",“From Gray Level");
 Range.OlePropertySet("Orientation",90);
表格製作

 製作表格:
 Range=Worksheet.OlePropertyGet("Range",“A1:E5")
;
 Range.OlePropertyGet("Borders").OlePropertySet("Li
xlDash,xlDashDot,xlDot…
neStyle",xlContinuous);

 Range.OlePropertyGet("Borders").OlePropertySet("W
eight",xlThin);
xlMedium,xlThick,xlHairline…

 Range.OlePropertyGet("Borders").OlePropertySet("C
olorIndex",xlAutomatic);
表格製作

 Variant Item;

 製作外框:
 Range=Worksheet.OlePropertyGet("Range",“A1:E5");
 Item=Range.OlePropertyGet("Borders").OlePropertyGet
("Item",xlEdgeLeft);
xlEdgeRight,xlEdgeTop,xlEdgeBottom…
 Item.OlePropertySet("LineStyle",xlContinuous);
 Item.OlePropertySet("Weight",xlThin);
 Item.OlePropertySet("ColorIndex",xlAutomatic);
表格製作

 取消框框:
 Range=Worksheet.OlePropertyGet("Range",“A1:C3");
 Range.OlePropertyGet("Borders").OlePropertySet("Line
Style",xlNone);

 合併框框:
 Range=Worksheet.OlePropertyGet("Range",“A1:C3");
 Range.OleFunction("Merge");
產生圖表
 Variant charts,chart;

 新增圖表並旋轉 45 度:
 charts=Workbook.OlePropertyGet("Charts");
 chart=charts.OleFunction("Add");
 chart.OleFunction("SetSourceData",Worksheet.OlePrope
rtyGet("Range",“A1:E5"),1);
 chart.OlePropertySet("ChartType",-4100);
 chart.OlePropertySet("Rotation",45);

100

80
數列1
60 數列2
40 數列3
數列4
20
S4 數列5
0
1 2 S1
3 4 5
100

80

60

產生圖表
40
20
S4
0 To
1 2 S1
3 4 5

圖表座標軸名稱設定:
From

 chart.OlePropertyGet("Axes",3).OlePropertySet("HasTitle"
,true);
 chart.OlePropertyGet("Axes",3).OlePropertyGet("AxisTitle
").OlePropertySet("Text","From");
 chart.OlePropertyGet("Axes",1).OlePropertySet("HasTitle"
,true);
 chart.OlePropertyGet("Axes",1).OlePropertyGet("AxisTitle
").OlePropertySet("Text","To");

 取消圖例:
 chart.OlePropertySet("HasLegend",false);
產生圖表
 將全部圖表產生在同一個 sheet 裡:
 chart.OleFunction("Location",2,Worksheet.OleProperty
Get("Name"));
 改變圖表大小以及位置:
 Worksheet.OlePropertyGet(“ChartObjects",8).OleProp
DrawingObjects
ertySet("Top",500);
(Office 2007 只能用 DrawingObjects)
 Worksheet.OlePropertyGet(“ChartObjects",8).OleProp
ertySet("Left",60);
 Worksheet.OlePropertyGet(“ChartObjects",8).OleProp
ertySet("Width",330); 1,2,3,4,5,6,7…
 Worksheet.OlePropertyGet(“ChartObjects",8).OleProp
ertySet("Height",280);
7 6 5
8

4 3 2 1
列印版面設定
 Variant page;
 page=Worksheet.OlePropertyGet("PageSetup");

 page.OlePropertySet(“PrintArea”,“B2:AE73”); // 列印範圍
 page.OlePropertySet(“PrintGridlines”,false); // 不印格線
 page.OlePropertySet(“PrintQuality”,300); // 列印品質
 page.OlePropertySet(“Orientation”,xlLandscape); // 橫向列印
 page.OlePropertySet("FirstPagenumber",xlAutomatic);
 page.OlePropertySet("Order",xlDownThenOver);
 page.OlePropertySet("Draft",false);
 page.OlePropertySet("CenterHorizontally",true);
 page.OlePropertySet("CenterVertically",false);
 page.OlePropertySet(“LeftFooter”,“Page 1 of &p”); // 頁腳名

PrintArea B2:AE73
列印版面設定
 page.OlePropertySet("PaperSize",xlPaperLetter);
// 將列印範圍縮至一頁列印
 page.OlePropertySet("Zoom",false);
 page.OlePropertySet("FitToPagesWide",1);
 page.OlePropertySet("FitToPagesTall",1);
// 列印邊界設定, 0.6/0.035 表 0.6 公分
 page.OlePropertySet(“LeftMargin”,0.6/0.035);
 page.OlePropertySet("RightMargin",0.6/0.035);
 page.OlePropertySet("TopMargin",1.3/0.035);
 page.OlePropertySet("BottomMargin",0.6/0.035);
 page.OlePropertySet("HeaderMargin",0/0.035);
 page.OlePropertySet("FooterMargin",0/0.035);
FitToPagesWide & FitToPagesTall

邊界
保護資料
不被更改
文件保護
 開啟文件保護功能:
 Worksheet.OleFunction("Protect",“xyz",true,true,true,true);

只允許這範圍才能
被修改,而 Tab 選
取也只限定在此
範圍

 限定可修改的範圍:
 Range=Worksheet.OlePropertyGet("Range",“B2:E5");
 Range.OlePropertySet("Locked",false);
 Worksheet.OlePropertySet("EnableSelection",xlUnlockedCells);
xlNoSelection,xlNoRestrictions…
補充

52 87 2.5
 公式計算:
 Cell=Worksheet.OlePropertyGet(“Cells",6,1);
 Cell.OlePropertySet("Formula","=SUM(A1:B2)");

 Cell=Worksheet.OlePropertyGet(“Cells",6,2);
 Cell.OlePropertySet("Formula","=MAX(A1:E5)");

 Cell=Worksheet.OlePropertyGet(“Cells",6,3);
 Cell.OlePropertySet("Formula","=AVERAGE(B1:C2)");
參考文件
 MSDN -Excel物件模型參考
 Delphi K.Top討論區
 Color Palette and the 56 Excel
ColorIndex Colors
 http://tinyurl.com/2jyo4c
 http://tinyurl.com/2sb3c6
 http://tinyurl.com/2lyck6
 http://tinyurl.com/2xqxld

Anda mungkin juga menyukai