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

packagecom.jrkui.example.excel;

import org.eclipse.swt.SWT;<br />import org.eclipse.swt.layout.FillLayout;<br />import org.eclipse.swt.ole.win32.OLE;<br />import org.eclipse.swt.ole.win32.OleAutomation;<br />import org.eclipse.swt.ole.win32.OleClientSite;<br />import org.eclipse.swt.ole.win32.OleFrame;<br />import org.eclipse.swt.ole.win32.Variant;<br />import org.eclipse.swt.widgets.Display;<br />import org.eclipse.swt.widgets.Shell;<br />public class ColorRangeShell {<br />    public static void main(String[] args) {<br />        new ColorRangeShell().open();<br />    }<br />    public void open()<br />    {<br />        Display display = Display.getDefault();<br />        Shell shell = new Shell();<br />        shell.setText("Color Range Shell");<br />        shell.setSize(400, 300);<br />        shell.setLayout(new FillLayout());<br />        createExcelPart(shell);<br />        shell.open();<br />        while(!shell.isDisposed())<br />            if(!display.readAndDispatch())<br />                display.sleep();<br />        display.dispose();<br />    }<br />    private static final int SHEET_ID = 0x000001e5;<br />    private static final int CELL_ID =  0x000000c5;<br />    private void createExcelPart(Shell shell)<br />    {<br />        OleFrame. frame. = new OleFrame(shell,SWT.NONE);<br />        OleClientSite clientSite = new OleClientSite(frame,SWT.NONE,"Excel.Sheet");<br />        clientSite.doVerb(OLE.OLEIVERB_SHOW);<br />        OleAutomation workbook = new OleAutomation(clientSite);<br />        OleAutomation worksheet = workbook.getProperty(SHEET_ID, new Variant[]{new Variant(1)}).getAutomation();<br />        //获得单元格<br />        OleAutomation cellA1 = worksheet.getProperty(CELL_ID, new Variant[]{new Variant("A1")}).getAutomation();<br />        OleAutomation cellD1 = worksheet.getProperty(CELL_ID, new Variant[]{new Variant("D1")}).getAutomation();<br />        //获得单元格区域<br />        OleAutomation areaA3D5 = worksheet.getProperty(CELL_ID,new Variant[]{new Variant("A3"),new Variant("D5")}).getAutomation();<br />        colorRangeByRed(cellA1);<br />        colorRangeByRed(cellD1);<br />        colorRangeByRed(areaA3D5);<br />    }<br />    /**<br />     * 获得interior的方法在Range中的Id<br />     */<br />    private static final int INTERIOR = 0x00000081;<br />    /**<br />     * 为ColorIndex赋值的方法在interior中的Id<br />     */<br />    private static final int COLOR_INDEX = 0x00000061;<br />    /**<br />     * 红色在Excel的Index为3<br />     */<br />    private static final int RED = 3; <br />    /**<br />     * 用红色作为Range的背景色<br />     * @param automation<br />     */<br />    private void colorRangeByRed(OleAutomation automation)<br />    {<br />        //获得interior<br />        OleAutomation interior = automation.getProperty(INTERIOR).getAutomation();<br />        //设置颜色<br />        interior.setProperty(COLOR_INDEX, new Variant(RED));<br />    }<br />}

运行效果:

原理:

为Range设置背景色是通过Range的interior属性

interior也是一个对象,为它的ColorIndex属性赋值可设置其颜色

Excel里的颜色设置是通过其序列号设定的(不是通过RGB),红色的index是3。获得所有颜色的index的方法还没找到,可以一个个试,从0开始的整数。

妩媚动人,让我感受到了大自然的神奇。

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

相关文章:

你感兴趣的文章:

标签云: