本文彙整 C++ Builder 透過 OLE 自動化操控 Excel 2000 的核心技術,涵蓋物件模型、常用屬性、方法、格式設定、圖表操作與排序實戰,適合作為開發時的速查手冊。
#include "Comobj.hpp"
#define PG OlePropertyGet
#define PS OlePropertySet
#define FN OleFunction
#define PR OleProcedure
Variant ExcelApp, WorkBook1, Sheet1, Range1;
// 建立 Excel 應用程式物件
ExcelApp = Variant::CreateObject("Excel.Application");
// 或使用 CreateOleObject("Excel.Application")
// 取得作用中活頁簿與工作表
WorkBook1 = ExcelApp.PG("ActiveWorkBook");
Sheet1 = WorkBook1.PG("ActiveSheet");
// 取得儲存格範圍
Range1 = Sheet1.PG("Range", "A1:A10");
| 操作 | 程式碼範例 |
|---|---|
| 隱藏 Excel 視窗 | ExcelApp.PS("Visible", (Variant)false); |
| 新建預設活頁簿 | ExcelApp.PG("workbooks").FN("Add"); |
| 新建單工作表活頁簿 | ExcelApp.PG("workbooks").FN("Add", 1); |
| 開啟現有檔案 | ExcelApp.PG("workbooks").FN("open", "C:\\path\\file.xls"); |
| 儲存活頁簿 | WorkBook1.FN("Save"); |
| 另存新檔 | WorkBook1.FN("SaveAs", "C:\\new.xls"); |
| 結束 Excel | ExcelApp.FN("Quit"); ExcelApp = Unassigned; |
// 選取第一張工作表
Workbook1.PG("Sheets", 1).PR("Select");
Sheet1 = Workbook1.PG("ActiveSheet");
// 重命名工作表
Sheet1.PS("Name", "新名稱");
// 取得工作表總數
int nSheetCount = Workbook1.PG("Sheets").PG("Count");
Sheet1.PG("Cells", 1, 1).PG("Font").PS("Name", "標楷體");
Sheet1.PG("Cells", 2, 3).PG("Font").PS("Size", 28);
Range.PG("Cells").PG("Font").PS("Color", RGB(0,0,255));
Range.PG("Cells").PG("Font").PS("Bold", true);
// 百分比格式
Sheet1.PG("Cells", 1, 1).PS("NumberFormatLocal", "0.00%");
Range = ExcelApp.PG("Cells", 3, 4);
Range.PS("VerticalAlignment", 2); // 1=靠上 2=置中 3=靠下
Range.PS("WrapText", true); // 自動換行
Range = Sheet1.PG("Range", "A1:A2");
Range.FN("Merge", false);
// 動態範圍合併
String strRange = "A" + IntToStr(j) + ":" + "C" + IntToStr(j);
Range1 = Sheet1.PG("Range", strRange.c_str());
Range1.FN("Merge", false);
// 寫入
Sheet1.PG("Cells", 3, 6).PS("Value", "abcdefg");
Sheet1.PG("Cells", j, 1).PS("Value", "總記錄:" + String(j-6));
// 讀取
String strValue = Sheet1.PG("Cells", 3, 5).PG("Value");
// 視窗狀態:1=正常 2=最小化 3=最大化
ExcelApp.PS("Windowstate", 3);
// 狀態列文字
ExcelApp.PS("StatusBar", "正在查詢,請稍候...");
// 還原預設
ExcelApp.PS("StatusBar", false);
// 視窗標題
ExcelApp.PS("Caption", "查詢系統");
Variant Chart;
Chart = ExcelApp.Exec(PropertyGet("Charts")).Exec(Function("Add"));
ExcelApp.Exec(PropertySet("Visible") << true);
Chart.Exec(PropertySet("Type") << -4100); // xlLine
// 3D 圖表旋轉動畫
for(int nRotate=5; nRotate <= 180; nRotate += 5)
Chart.Exec(PropertySet("Rotation") << nRotate);
for(int nRotate=175; nRotate >= 0; nRotate -= 5)
Chart.Exec(PropertySet("Rotation") << nRotate);
#include "comobj.hpp"
void SortExcelColumn(String strExcelFileName, int nCol, int nSortStyle)
{
Variant vExcelApp, vWorkbook, vRange;
try {
vExcelApp = Variant::CreateObject("Excel.Application");
} catch(...) {
ShowMessage("啟動 Excel 失敗,請確認已安裝 Office");
return;
}
vExcelApp.OlePropertySet("Visible", false);
vExcelApp.OlePropertyGet("WorkBooks").OleProcedure("Open", strExcelFileName.c_str());
vWorkbook = vExcelApp.OlePropertyGet("ActiveWorkbook");
vExcelApp.OlePropertyGet("Columns", nCol).OleProcedure("Select");
vExcelApp.OlePropertyGet("ActiveSheet").OlePropertyGet("Cells", 1, nCol).OleProcedure("Select");
vRange = vExcelApp.OlePropertyGet("Selection");
vRange.Exec(Function("Sort") << vExcelApp.OlePropertyGet("Selection") << nSortStyle);
vWorkbook.OleProcedure("Save");
vWorkbook.OleProcedure("Close");
vExcelApp.OleFunction("Quit");
vWorkbook = Unassigned;
vExcelApp = Unassigned;
ShowMessage("排序完成");
}
// 呼叫範例:對 C:\123\123.xls 第四欄升序排序
// SortExcelColumn("C:\\123\\123.xls", 4, 1);
try {
ExcelApp = Variant::CreateObject("Excel.Application");
} catch(...) {
ShowMessage("無法啟動 Excel,請確認 Office 安裝狀態");
return;
}
// 後續操作皆建議包裝在 try...catch 中
// 最後務必釋放 Variant
ExcelApp = Unassigned;
掌握 Application → Workbooks → Workbook → Sheets → Sheet → Range/Cells 物件層級,善用 PG/PS/FN/PR 巨集,並嚴格執行 Unassigned 釋放流程,即可在 C++ Builder 中高效、穩定地自動化 Excel 報表產生與資料處理。