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

SWT OLE 操作 Excel 系列(四):儲存格與欄位自動調整寬度

     編輯:Java 相關知識

前言

實務上常遇到儲存格內容過長被截斷,需手動調整欄寬。透過 OLE 呼叫 Range 的 AutoFit 方法(ID 0x000000ED)可自動調整。本文示範針對單一儲存格與整欄自動調寬。

完整程式碼

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

    public void open() {
        Display display = Display.getDefault();
        Shell shell = new Shell();
        shell.setText("Auto Fit");
        shell.setSize(700, 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 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("if you don't fit the width of the cell, you couldn't see all."));
        autoFitWidth(cellA3);
        // autoFitWidth(getColumnOfCell(cellA3));  // 取消註解改為調整整欄
    }

    private static final int AUTO_FIT_RANGE = 0x000000ed;

    private void autoFitWidth(OleAutomation range) {
        // 兩種寫法效果相同
        range.invoke(AUTO_FIT_RANGE);
        // range.getProperty(AUTO_FIT_RANGE);  // 也可用 getProperty 觸發
    }

    private static final int COLUMN_OF_CELL = 0x000000f6;

    private OleAutomation getColumnOfCell(OleAutomation cell) {
        // Range.EntireColumn 屬性 ID
        return cell.getProperty(COLUMN_OF_CELL).getAutomation();
    }
}

執行結果

調整單一儲存格 A3 寬度

A3 欄寬自動展開以容納長字串

調整 A3 所在整欄寬度(取消註解 getColumnOfCell)

整欄 A 自動調寬

關鍵技術點

  1. AutoFit 方法屬於 Range 物件,ID 0x000000ED。
  2. 呼叫方式:range.invoke(AUTO_FIT_RANGE) 或 range.getProperty(AUTO_FIT_RANGE) 效果相同。
  3. 參數型別:OleAutomation 代表 Range,可為單一儲存格、多儲存格區域、整列、整欄。
  4. 取得整欄:Range.EntireColumn 屬性 ID 0x000000F6。

應用場景

  • 匯出報表後自動調整所有欄寬,提升可讀性。
  • 動態產生內容長度不固定的儲存格時,呼叫 AutoFit 確保完整顯示。
  • 批次處理:遍歷 Worksheet.UsedRange.Columns 逐欄 AutoFit。

系列文章導覽

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