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

SWT OLE 操作 Excel 系列(二):為 Excel A1 儲存格賦值

     編輯:Java 相關知識

前言

系列第二篇:示範在 SWT 中嵌入 Excel,取得 Workbook → Worksheet → Range (A1) → 設定 Value,並讀回驗證。詳細註解每個 OLE 物件 ID 取得來源。

完整程式碼

package com.jrkui.example.excel;

import java.io.File;
import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.ole.win32.OLE;
import org.eclipse.swt.ole.win32.OleAutomation;
import org.eclipse.swt.ole.win32.OleClientSite;
import org.eclipse.swt.ole.win32.OleFrame;
import org.eclipse.swt.ole.win32.Variant;
import org.eclipse.swt.program.Program;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Menu;
import org.eclipse.swt.widgets.Shell;

public class ExcelShell {
    public static void main(String[] args) { new ExcelShell().open(); }

    public void open() {
        Display display = Display.getDefault();
        Shell shell = new Shell();
        shell.setSize(600, 400);
        shell.setText("Excel Window");
        shell.setLayout(new FillLayout());
        // 顯示 Excel 選單列
        shell.setMenuBar(new Menu(shell, SWT.BAR));
        createExcelPart(shell);
        shell.open();
        while (!shell.isDisposed()) {
            if (!display.readAndDispatch()) display.sleep();
        }
        display.close();
    }

    private void createExcelPart(Shell shell) {
        OleFrame oleFrame = new OleFrame(shell, SWT.NONE);
        OleClientSite clientSite = new OleClientSite(oleFrame, SWT.NONE, "Excel.Sheet");
        setValueForA1Cell(clientSite);
        clientSite.doVerb(OLE.OLEIVERB_SHOW);
    }

    private static final int SHEET_ID = 0x000001e5;
    private static final int CELL_ID = 0x000000c5;
    private static final int CELL_VALUE_ID = 0x00000006;

    private void setValueForA1Cell(OleClientSite clientSite) {
        OleAutomation workbook = new OleAutomation(clientSite);  // ClientSite 即 Workbook
        OleAutomation sheet = workbook.getProperty(SHEET_ID, new Variant[]{new Variant(1)}).getAutomation();
        Variant cellA1Variant = sheet.getProperty(CELL_ID, new Variant[]{new Variant("A1")});
        OleAutomation cellA1 = cellA1Variant.getAutomation();
        cellA1.setProperty(CELL_VALUE_ID, new Variant("Hello OLE!"));
        System.out.println(cellA1Variant.getString());  // 主控台輸出驗證
    }
}

執行結果

Excel 視窗顯示 A1 為 Hello OLE!

主控台輸出:Hello OLE!

核心流程解析

  1. OleFrame:Composite 容器,承載 OLE 控制項。
  2. OleClientSite("Excel.Sheet"):建立 Excel OLE 物件站點,ProgID 為 Excel.Sheet(版本無關)。
  3. Workbook:new OleAutomation(clientSite) 直接轉換。
  4. Worksheet:Workbook.Sheets[1],ID 0x000001E5,需傳入索引 1(Variant 陣列)。
  5. Range (A1):Worksheet.Range("A1"),ID 0x000000C5,參數為字串 "A1"。
  6. Value 屬性:ID 0x00000006,setProperty 寫入,getString() 讀出。

如何取得 OLE 物件 ID(使用 OleView.exe)

  1. 安裝 OleView.exe(Visual Studio / Windows SDK 附帶)。
  2. 開啟 Document Objects → Microsoft Excel Worksheet,Registry** 標籤確認 ProgID = Excel.Sheet。
  3. 右鍵 Microsoft Excel Worksheet → View Type Information 開啟 ITypeLib Viewer。
  4. 工具列分類按鈕切換至 Dispinterfaces。

查詢 Sheets (Worksheets 集合)

  • 找 _Workbooks → Methods → Sheets:ID 0x000001E5,型別 propget,無參數。

查詢 Range

  • 找 _Worksheet → Methods → Range:ID 0x000000C5,propget,參數 Cell1 (VARIANT), Cell2 (VARIANT, optional)。

查詢 Value

  • 找 Range → Methods → Value:兩個同 ID 0x00000006,分別為 propget(讀)與 propput(寫)。

OleView 介面

ITypeLib Viewer

關鍵類別職責

類別說明
OleFrame容器 Composite,管理 OLE 控制項生命週期
OleClientSiteOLE 物件站點,綁定特定 ProgID (Excel.Sheet)
OleAutomation通用自動化代理,代表任意 OLE 物件 (Workbook/Worksheet/Range)
VariantOLE 型別包裝器,雙向傳遞值與物件參考

系列文章導覽

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