使Excel嵌入到SWT窗口中

使用的Eclipse版本:3.3.1

使用的jdk版本:5.0

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.OleClientSite;
import org.eclipse.swt.ole.win32.OleFrame;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Menu;
import org.eclipse.swt.widgets.Shell;

public class ExcelShell {
    
public static void main(String[] args) {
        
new ExcelShell().open();
    }
    
    
public void open()
    {
        Display display 
= Display.getDefault();
        Shell shell 
= new Shell();
        shell.setSize(
600,400);
        shell.setText(
"Excel Window");
        shell.setLayout(
new FillLayout());
        
//显示Excel的菜单栏
        shell.setMenuBar(new Menu(shell,SWT.BAR));
        
        createExcelPart(shell);
        
        shell.open();
        
while(!shell.isDisposed()){
            
if(!display.readAndDispatch())
                display.sleep();
        }
        display.close();
    }
    
    
/**
     * 使Excel嵌入到shell中
     * 
@param shell
     
*/
    
private void createExcelPart(Shell shell)
    {
        
//OleFrame实际上是一个Composite,用于放置OLE控件
        OleFrame oleFrame = new OleFrame(shell,SWT.NONE);
        
        
//OleClientSite提供一个场所用于把OLE对象嵌入到容器中,在这里“Excel.Sheet”表示的OLE对象是Excel
        OleClientSite clientSite = new OleClientSite(oleFrame,SWT.NONE,"Excel.Sheet");
        
        
//OleClientSite在显示OLE对象时所做的动作,这里的动作是OLEIVERB_SHOW,表示显示
        clientSite.doVerb(OLE.OLEIVERB_SHOW);
    }
}

显示结果:

使Excel嵌入到SWT窗口中

注意

<!--[if !supportLists]-->嵌入Excel的方法是createExcelPart(Shell shell)

<!--[if !supportLists]-->Excel.SheetExcelId,如果要嵌入Word,则其IdWord.Document

<!--[endif]-->这是OleClientSite#doVerb()的参数的解释及可选的值:

   verb – an integer value mapping to one of the following pre-defined verb values:

<!--[if !supportLists]-->·          <!--[endif]-->OLE.OLEIVERB_PRIMARY - Specifies the action that occurs when an end user double-clicks the object in its container. The object, not the container, determines this action. If the object supports in-place activation, the primary verb usually activates the object in place.

<!--[if !supportLists]-->·           <!--[endif]-->OLE.OLEIVERB_SHOW - Instructs an object to show itself for editing or viewing. Called to display newly inserted objects for initial editing and to show link sources. Usually an alias for some other            object-defined verb.

<!--[if !supportLists]-->·           <!--[endif]-->OLE.OLEIVERB_OPEN - Instructs an object, including one that otherwise supports in-place activation, to open itself for editing in a window separate from that of its container. If the object does not           support in-place activation, this verb has the same semantics as OLEIVERB_SHOW.

<!--[if !supportLists]-->·           <!--[endif]-->OLE.OLEIVERB_HIDE - Causes an object to remove its user interface from the view. Applies only to objects that are activated in-place.

<!--[if !supportLists]-->·           <!--[endif]-->OLE.OLEIVERB_INPLACEACTIVATE - Activates an object in place without displaying tools, such as menus and toolbars, that end users need to change the behavior or appearance of the object.                  Single-clicking such an object causes it to negotiate the display of its user-interface tools with its container. If the container refuses, the object remains active but without its tools displayed.

<!--[if !supportLists]-->·           <!--[endif]-->OLE.OLEIVERB_UIACTIVATE - Activates an object in place, along with its full set of user-interface tools, including menus, toolbars, and its name in the title bar of the container window.

<!--[if !supportLists]-->·           <!--[endif]-->OLE.OLEIVERB_DISCARDUNDOSTATE - Used to tell objects to discard any undo state that they may be maintaining without deactivating the object.

如果光看解释看不明白的话,可以实际操作看看效果

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/wzywfp.html