首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 计算机考试 > 认证考试 > JAVA认证 >

核心技术:在SWT中使用OLE操纵Excel(3)

2009-02-02 
SWTOLE

    设置单元格背景色
  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();
  //获得单元格区域
  OleAutomation areaA3D5 = worksheet.getProperty(CELL_ID,new Variant[]{new Variant("A3"),new Variant("D5")}).getAutomation();
  colorRangeByRed(cellA1);
  colorRangeByRed(cellD1);
  colorRangeByRed(areaA3D5);
  }
  /**
  * 获得interior的方法在Range中的Id
  */
  private static final int INTERIOR = 0x00000081;
  /**
  * 为ColorIndex赋值的方法在interior中的Id
  */
  private static final int COLOR_INDEX = 0x00000061;
  /**
  * 红色在Excel的Index为3
  */
  private static final int RED = 3;
  /**
  * 用红色作为Range的背景色
  * @param automation
  */
  private void colorRangeByRed(OleAutomation automation)
  {
  //获得interior
  OleAutomation interior = automation.getProperty(INTERIOR).getAutomation();
  //设置颜色
  interior.setProperty(COLOR_INDEX, new Variant(RED));
  }
  }
  原理:
  <!--[if !supportLists]-->ü<!--[endif]-->为Range设置背景色是通过Range的interior属性
  <!--[if !supportLists]-->ü <!--[endif]-->interior也是一个对象,为它的ColorIndex属性赋值可设置其颜色
  <!--[if !supportLists]-->ü <!--[endif]-->Excel里的颜色设置是通过其序列号设定的(不是通RGB),红色的index是3。获得所有颜色的index的方法还没找到,可以一个个试,从0开始的整数。

 


3COME考试频道为您精心整理,希望对您有所帮助,更多信息在http://www.reader8.com/exam/

热点排行