首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式碼之家 >> 編程語言 >  >> C語言 >> C++ >> 關於C++

C++ Builder OLE 自動化控制 Excel 完整參考手冊

     編輯:關於C++

簡介

本文彙整 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");
結束 ExcelExcelApp.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);

實用工具函式:Excel 欄位排序


#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 報表產生與資料處理。

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式碼之家 All Rights Reserved