在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();
	}
}

 运行效果如图:


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Ntko接口开发是指基于Ntko软件的接口开发,它提供了一种可自定义的方式来扩展和集成Ntko软件的功能。Ntko是一家专业从事办公自动化软件开发的公司,其旗下的产品包括办公文档处理、PDF编辑、图像处理等多个方面。 Ntko接口开发的定义提示可以从以下几个方面来回答: 1. 功能扩展:Ntko接口开发允许开发人员通过API来访问和操作Ntko软件的功能。通过这种方式,用户可以根据自己的需求来扩展Ntko软件的功能,实现更加个性化的操作。 2. 数据集成Ntko接口开发提供了各种API,可以与其他系统和软件进行数据交互。开发人员可以利用这些API将Ntko软件与现有的系统集成起来,使得数据在不同系统之间的传输更加高效和方便。 3. 自定义界面:Ntko接口开发允许开发人员通过API来自定义界面和布局。用户可以根据自己的习惯和需求,自定义软件的界面和布局,使得使用起来更加舒适和便捷。 4. 批量处理:Ntko接口开发还可以实现批量处理功能,通过编写代码来实现对大量文档或图片的批处理操作。这样可以极大地提升工作效率,节省时间和人力成本。 通过Ntko接口开发,用户可以根据自己的需求自定义软件的功能和界面,实现更好的办公效率和用户体验。同时,它也提供了与其他系统的集成能力,使得数据的交互更加方便。总之,Ntko接口开发为用户提供了更多个性化和定制化的选择,使得软件的使用更加灵活和高效。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值