当前位置导航:炫浪网>>网络学院>>编程开发>>JAVA教程>>Java进阶

在SWT中使用OLE操纵Excel(三)

    设置单元格背景色

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(
400300);
        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开始的整数。

相关内容
赞助商链接