java輔導(dǎo):使Excel嵌入到SWT窗口中

字號(hào):

使用的Eclipse版本:3.3.1
    使用的jdk版本:5.0
    packagecom.jrkui.example.excel;
    importorg.eclipse.swt.SWT;
    importorg.eclipse.swt.layout.FillLayout;
    importorg.eclipse.swt.ole.win32.OLE;
    importorg.eclipse.swt.ole.win32.OleClientSite;
    importorg.eclipse.swt.ole.win32.OleFrame;
    importorg.eclipse.swt.widgets.Display;
    importorg.eclipse.swt.widgets.Menu;
    importorg.eclipse.swt.widgets.Shell;
    publicclassExcelShell{
    publicstaticvoidmain(String[]args){
    newExcelShell().open();
    }
    publicvoidopen()
    {
    Displaydisplay=Display.getDefault();
    Shellshell=newShell();
    shell.setSize(600,400);
    shell.setText("ExcelWindow");
    shell.setLayout(newFillLayout());
    //顯示Excel的菜單欄
    shell.setMenuBar(newMenu(shell,SWT.BAR));
    createExcelPart(shell);
    shell.open();
    while(!shell.isDisposed()){
    if(!display.readAndDispatch())
    display.sleep();
    }
    display.close();
    }
    /**
    *使Excel嵌入到shell中
    *@paramshell
    */
    privatevoidcreateExcelPart(Shellshell)
    {
    //OleFrame實(shí)際上是一個(gè)Composite,用于放置OLE控件
    OleFrameoleFrame=newOleFrame(shell,SWT.NONE);
    //OleClientSite提供一個(gè)場(chǎng)所用于把OLE對(duì)象嵌入到容器中,在這里“Excel.Sheet”表示的OLE對(duì)象是Excel
    OleClientSiteclientSite=newOleClientSite(oleFrame,SWT.NONE,"Excel.Sheet");
    //OleClientSite在顯示OLE對(duì)象時(shí)所做的動(dòng)作,這里的動(dòng)作是OLEIVERB_SHOW,表示顯示clientSite.doVerb(OLE.OLEIVERB_SHOW);
    }
    } 注意
    嵌入Excel的方法是createExcelPart(Shell shell)
    Excel.Sheet為Excel的Id,如果要嵌入Word,則其Id為Word.Document
    x這是OleClientSite#doVerb()的參數(shù)的解釋及可選的值:
    verb – an integer value mapping to one of the following pre-defined verb values:
    - 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.
    - 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.
    - 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.
    - Causes an object to remove its user interface from the view. Applies only to objects that are activated in-place.
    - 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.
    - 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.
    - Used to tell objects to discard any undo state that they may be maintaining without deactivating the object.
    如果光看解釋看不明白的話,可以實(shí)際操作看看效果