在eclipse中集成NTKO
最近接到一个任务,需要在eclipse中集成NTKO控件,用于编辑保存word文档,本人只会java编程,对于ocx的使用没有概念。从网上查找了一些示例代码,有的用于发送短信,但是没有内嵌NTKO的代码,自己写一个吧。
1 首先需要注册NTKO,这个信息需要写入到window的注册表中
注册NTKO: regsvr32 D:\work\NTKO\OfficeControl.ocx 注销NTKO: regsvr32 /u D:\work\NTKO\OfficeControl.ocx
2 swt中相关的四个类
// 用于存放Ole控件的顶层容器
org.eclipse.swt.ole.win32.OleFrame
// 用于管理内嵌的Ole控件
org.eclipse.swt.ole.win32.OleClientSite
// 用于操作内嵌的Ole控件
org.eclipse.swt.ole.win32.OleAutomation
// 值传输对象,可以传输int、string、boolean、子对象等
org.eclipse.swt.ole.win32.Variant
3 示例代码
package com.weifly.ole;
import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
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;
import org.eclipse.swt.widgets.ToolBar;
import org.eclipse.swt.widgets.ToolItem;
public class NTKOTest {
private Display display = Display.getDefault();
private Shell shell = new Shell(display);
private OleAutomation oa;
public NTKOTest() {
shell.setLayout(new GridLayout(1, false));
ToolBar toolBar = new ToolBar(shell, SWT.FLAT | SWT.WRAP | SWT.RIGHT);
toolBar.setLayoutData(new GridData(SWT.LEFT, SWT.CENTER, true, false));
ToolItem openFileItem = new ToolItem(toolBar, SWT.PUSH);
openFileItem.setText("打开文件");
openFileItem.addSelectionListener(new SelectionAdapter(){
public void widgetSelected(SelectionEvent e) {
openFileHandler();
}
});
final ToolItem showToolbarItem = new ToolItem(toolBar, SWT.PUSH);
showToolbarItem.setText("显示工具栏");
showToolbarItem.addSelectionListener(new SelectionAdapter(){
public void widgetSelected(SelectionEvent e) {
showToolbarHandler(showToolbarItem);
}
});
ToolItem getTextItem = new ToolItem(toolBar, SWT.PUSH);
getTextItem.setText("文本内容");
getTextItem.addSelectionListener(new SelectionAdapter(){
public void widgetSelected(SelectionEvent e) {
getTextHandler();
}
});
createOle();
shell.setSize(500, 400);
shell.open();
while (!shell.isDisposed()) {
if (!display.readAndDispatch()) {
display.sleep();
}
}
display.dispose();
}
private void openFileHandler(){
System.out.println("打开文件");
Variant[] va = new Variant[3];
va[0] = new Variant("D:\\test.doc");
va[1] = new Variant(false);
va[2] = new Variant("Word.Document");
execMethod(oa, "OpenLocalFile", va);
}
private void showToolbarHandler(ToolItem item){
boolean isShow = getProperty(oa, "ToolBars").getBoolean();
if(isShow){
item.setText("显示工具栏");
setProperty(oa,"ToolBars","false");
}else{
item.setText("隐藏工具栏");
setProperty(oa,"ToolBars","true");
}
}
private void getTextHandler(){
System.out.println("获得文本内容");
String text = null;
Variant obj = getProperty(oa, "ActiveDocument");
if(obj!=null){
if(obj.getType()==OLE.VT_DISPATCH){
OleAutomation auto = obj.getAutomation();
OleAutomation rang = execMethod(auto, "Range", null).getAutomation();
Variant textVar = (Variant)getProperty(rang, "Text");
text = textVar.getString();
}
}
MessageDialog.openInformation(shell, "word文档内容", text);
}
private void createOle(){
OleFrame frame = new OleFrame(shell, SWT.NONE);
frame.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
OleClientSite client = new OleClientSite(frame,SWT.NONE,"NTKO.OfficeControl");
oa = new OleAutomation(client);
setProperty(oa,"ToolBars","false");
client.doVerb(OLE.OLEIVERB_SHOW);
}
private void setProperty(OleAutomation oa, String key, String value){
int[] ia = oa.getIDsOfNames(new String[]{key});
if(ia!=null && ia.length>0){
oa.setProperty(ia[0], new Variant(value));
}
}
private Variant getProperty(OleAutomation oa, String key){
int[] ia = oa.getIDsOfNames(new String[]{key});
if(ia!=null && ia.length>0){
return oa.getProperty(ia[0]);
}else{
return null;
}
}
private Variant execMethod(OleAutomation oa, String method, Variant[] params){
int[] ia = oa.getIDsOfNames(new String[]{method});
if(ia!=null && ia.length>0){
return oa.invoke(ia[0], params);
}
return null;
}
public static void main(String[] args) {
new NTKOTest();
}
}
运行效果如图: