程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> JAVA編程 >> 關於JAVA >> 在SWT中使用OLE操作Excel(五):設置單元格的字體格式

在SWT中使用OLE操作Excel(五):設置單元格的字體格式

編輯:關於JAVA

設置字體格式是常見的需求

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();
  }

  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);
  }
  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;
  private void setFontStyle(OleAutomation cell)
  {
   OleAutomation font = cell.getProperty(FONT).getAutomation();//獲得單元格的font對象
   font.setProperty(FONT_SIZE, new Variant(32));//大小
   font.setProperty(FONT_NAME, new Variant("Arial Black"));//使用Arial Black的字體
   font.setProperty(BOLD, new Variant(true));//粗體
   font.setProperty(COLOR_INDEX, new Variant(RED));//顏色
  }
}

運行效果:

比較簡單,我就不進行解釋了

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved