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

SWT OLE 操作 Excel 系列(三):設定儲存格背景色

     編輯:Java 相關知識

前言

第三篇示範透過 Range 的 Interior 物件設定儲存格背景色。支援單一儲存格與多儲存格區域(如 A3:D5)同步著色。

完整程式碼

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 ColorRangeShell {
    public static void main(String[] args) { new ColorRangeShell().open(); }

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

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

    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 cellA1 = worksheet.getProperty(CELL_ID, new Variant[]{new Variant("A1")}).getAutomation();
        OleAutomation cellD1 = worksheet.getProperty(CELL_ID, new Variant[]{new Variant("D1")}).getAutomation();
        // 取得儲存格區域 A3:D5
        OleAutomation areaA3D5 = worksheet.getProperty(CELL_ID, new Variant[]{new Variant("A3"), new Variant("D5")}).getAutomation();

        colorRangeByRed(cellA1);
        colorRangeByRed(cellD1);
        colorRangeByRed(areaA3D5);
    }

    private static final int INTERIOR = 0x00000081;
    private static final int COLOR_INDEX = 0x00000061;
    private static final int RED = 3;  // Excel 色碼索引 3 = 紅色

    private void colorRangeByRed(OleAutomation range) {
        OleAutomation interior = range.getProperty(INTERIOR).getAutomation();  // 取得 Interior 物件
        interior.setProperty(COLOR_INDEX, new Variant(RED));                   // 設定 ColorIndex
    }
}

執行結果

A1、D1、A3:D5 區域背景皆為紅色

原理解析

  1. Interior 物件:代表儲存格內部格式,ID 0x00000081。
  2. ColorIndex 屬性:0x00000061,接受 Excel 56 色調色盤索引(非 RGB)。
  3. 紅色索引 = 3:可透過巨集錄製或 OleView.exe 查詢完整索引表。
  4. 適用範圍:單一儲存格、多儲存格區域(Range("A3:D5"))皆可套用。

擴充:設定 RGB 顏色

若需精確 RGB,可使用 Interior.Color 屬性(ID 0x00000063),值為 RGB(紅,綠,藍) 的長整數(紅 + 綠*256 + 藍*65536)。範例:

private static final int COLOR = 0x00000063;
interior.setProperty(COLOR, new Variant(0xFF0000)); // 紅色 RGB(255,0,0)

系列文章導覽

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