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

SWT OLE 操作 Excel 系列(五):設定儲存格字型格式

     編輯:Java 相關知識

前言

本系列第五篇示範如何透過 SWT OLE 設定 Excel 儲存格字型:大小、字體名稱、粗體、顏色。程式碼簡潔,直接展示關鍵屬性 ID 與操作流程。

完整程式碼

package com.jrkui.example.excel;

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.widgets.Display;
import org.eclipse.swt.widgets.Shell;

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

    public void open() {
        Display display = Display.getDefault();
        Shell shell = new Shell();
        shell.setText("Font Style");
        shell.setSize(400, 300);
        shell.setLayout(new FillLayout());
        createExcelPart(shell);
        shell.open();
        while (!shell.isDisposed()) {
            if (!display.readAndDispatch()) display.sleep();
        }
        display.dispose();
    }

    // Excel OLE 常數
    private static final int SHEET_ID = 0x000001e5;
    private static final int CELL_ID = 0x000000c5;
    private static final int CELL_VALUE_ID = 0x00000006;

    private void createExcelPart(Shell shell) {
        OleFrame frame = new OleFrame(shell, SWT.NONE);
        OleClientSite clientSite = new OleClientSite(frame, SWT.NONE, "Excel.Sheet");
        clientSite.doVerb(OLE.OLEIVERB_SHOW);
        OleAutomation workbook = new OleAutomation(clientSite);
        OleAutomation worksheet = workbook.getProperty(SHEET_ID, new Variant[]{new Variant(1)}).getAutomation();
        OleAutomation cellA3 = worksheet.getProperty(CELL_ID, new Variant[]{new Variant("A3")}).getAutomation();
        cellA3.setProperty(CELL_VALUE_ID, new Variant("Hello OLE!"));
        setFontStyle(cellA3);
    }

    // Font 物件屬性 ID
    private static final int FONT_SIZE = 0x00000068;
    private static final int FONT_NAME = 0x0000006e;
    private static final int COLOR_INDEX = 0x00000061;
    private static final int FONT = 0x00000092;
    private static final int BOLD = 0x00000060;
    private static final int RED = 3;  // Excel 色碼索引:3 = 紅色

    private void setFontStyle(OleAutomation cell) {
        OleAutomation font = cell.getProperty(FONT).getAutomation();  // 取得 Font 物件
        font.setProperty(FONT_SIZE, new Variant(32));                 // 字體大小 32 pt
        font.setProperty(FONT_NAME, new Variant("Arial Black"));      // 字體名稱
        font.setProperty(BOLD, new Variant(true));                    // 粗體
        font.setProperty(COLOR_INDEX, new Variant(RED));              // 字體顏色(索引 3)
    }
}

執行結果

A3 儲存格顯示 Arial Black 32pt 粗體紅色文字

關鍵步驟解析

  1. 取得 Font 物件:cell.getProperty(FONT),FONT = 0x00000092。
  2. 設定字體屬性:透過 OleAutomation.setProperty() 依序設定:
  • FONT_SIZE (0x68):點數大小。
  • FONT_NAME (0x6E):字體名稱字串(需系統安裝該字體)。
  • BOLD (0x60):true/false。
  • COLOR_INDEX (0x61):Excel 色碼索引(非 RGB),紅色為 3。

色碼索引參考

Excel 使用 56 色調色盤索引,常用:

完整索引可透過巨集錄製或 OleView.exe 查詢。

系列文章導覽

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式碼之家 All Rights Reserved
索引顏色
1黑
2白
3紅
4綠
5藍
6黃
7洋紅
8青