程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> JAVA編程 >> 關於JAVA >> 在SWT中使用OLE操作Excel(一):使Excel嵌入到SWT窗口中

在SWT中使用OLE操作Excel(一):使Excel嵌入到SWT窗口中

編輯:關於JAVA

使用的Eclipse版本:3.3.1

使用的jdk版本:5.0

packagecom.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的方法是createExcelPart(Shell shell)

Excel.Sheet為Excel的Id,如果要嵌入Word,則其Id為Word.Document

x這是OleClientSite#doVerb()的參數的解釋及可選的值:

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.

如果光看解釋看不明白的話,可以實際操作看看效果

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved