`
lifei114
  • 浏览: 26741 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

SWT 中嵌入Word 控件应用

阅读更多

利用WebOffice进行Word控件的应用,源码如下:

效果图如下:

SWT 中Word 应用

package com.edocbase.desktop.word;

import java.io.File;
import java.util.HashMap;
import java.util.Iterator;

import org.eclipse.swt.SWT;
import org.eclipse.swt.SWTException;
import org.eclipse.swt.events.DisposeEvent;
import org.eclipse.swt.events.DisposeListener;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.ole.win32.OLE;
import org.eclipse.swt.ole.win32.OleAutomation;
import org.eclipse.swt.ole.win32.OleControlSite;
import org.eclipse.swt.ole.win32.OleFrame;
import org.eclipse.swt.ole.win32.Variant;
import org.eclipse.swt.widgets.Composite;

/**
 * @author lifei114@gmail.com,lifei114@126.com
 * 嵌入 Word 的容器,更多的API正在开发中。
 */
public class WordComposite extends Composite {
	private static String progID = "WEBOFFICE.WebOfficeCtrl.1";
	OleControlSite site = null;
	OleAutomation auto = null;
	private OleFrame frame;

	/**
	 * Create the composite.
	 * 
	 * @param parent
	 * @param style
	 */
	public WordComposite(Composite parent, int style) {
		super(parent, style | SWT.BORDER);
		parent.addDisposeListener(new DisposeListener() {

			@Override
			public void widgetDisposed(DisposeEvent e) {
				dispose();
			}
		});
		setLayout(new FillLayout());
		init();
	}

	private void init() {
		frame = new OleFrame(this, SWT.NONE);
		try {
			site = new OLESite(frame, SWT.NONE, progID);
			// site.doVerb(OLE.OLEIVERB_SHOW);
			site.doVerb(OLE.OLEIVERB_INPLACEACTIVATE);
			auto = new OleAutomation(site);
			showWebOfficeBtn();
		} catch (SWTException ex) {
			try {
				System.out.println("注册Office控件。");
				installWebOffice();
				site = new OLESite(frame, SWT.NONE, progID);
				site.doVerb(OLE.OLEIVERB_SHOW);
				site.doVerb(OLE.OLEIVERB_INPLACEACTIVATE);
				auto = new OleAutomation(site);
				showWebOfficeBtn();				
			} catch (Exception e) {
				System.out.println("Unable to open type library for " + progID);
				e.printStackTrace();
			}
			return;
		}
	}
	private void installWebOffice() throws Exception{
		StringBuffer cmd = new StringBuffer();
		File ocxfile = new File("activex\\WebOffice.ocx");
		cmd.append("regsvr32 /s ").append(ocxfile.getAbsolutePath());
		String cmdStr = cmd.toString();
		Process process = Runtime.getRuntime().exec(cmdStr);
		process.waitFor();
	}
	@Override
	protected void checkSubclass() {
	}

	public void open(File file) {
		String filePath = file.getAbsolutePath();
		int[] methodIDs = null;
		methodIDs = auto.getIDsOfNames(new String[] { "LoadOriginalFile" });

		auto.invoke(methodIDs[0], new Variant[] { new Variant(filePath),new Variant("doc") });
		//hideToolBarAll();
		
	}
	public void printPreview(){
		int[] methodIDs = null;
		methodIDs = auto.getIDsOfNames(new String[] { "PrintPreview" });

		auto.invoke(methodIDs[0]);
	}
	public void print(){
		
		int[] methodIDs = null;
		methodIDs = auto.getIDsOfNames(new String[] { "PrintOut" });

		auto.invoke(methodIDs[0]);
	}
	public void setFieldValue(String key,String value){
		int[] methodIDs = null;
		methodIDs = auto.getIDsOfNames(new String[] { "SetFieldValue" });
		Variant[] vars = new Variant[]{new Variant(key),new Variant(value),new Variant("")};
		auto.invoke(methodIDs[0],vars);
	}
	public void updateFields(HashMap<String, String> values){
		Iterator<String> keyIterator = values.keySet().iterator();
		while (keyIterator.hasNext()){
			String key = keyIterator.next();
			String value = values.get(key);
			setFieldValue(key, value);
		}
	}
	/**
	 * 隐藏 WebOffice 所有工具栏
	 */
	public void hideToolBarAll(){
		int ids[] = auto.getIDsOfNames(new String[] { "ShowToolBar" });
		auto.setProperty(ids[0], new Variant[] { new Variant(false) });
	}
	
	public void showWebOfficeBtn(){
		int ids[] = auto.getIDsOfNames(new String[] { "HideMenuItem" });
		//auto.invoke(ids[0],new Variant[]{new Variant(0x04 + 0x10 + 0x20)});
		auto.invoke(ids[0],new Variant[]{new Variant(0x01 + 0x1000 + 0x02)});
	}
	
	@Override
	public void dispose() {
		super.dispose();
		auto.dispose();
		site.dispose();
		frame.dispose();
	}

}
分享到:
评论

相关推荐

    使Excel嵌入到SWT窗口中

    如果要嵌入Word,则其Id为“Word.Document”。OLE对象的Id是唯一的,用于标识不同的OLE对象。 知识点六:OLE对象的动作 在本例中,我们使用OLEIVERB_SHOW来显示OLE对象。OLEIVERB_SHOW是一个预定义的动作,用于...

    在自己的应用程序中嵌入Office,操作word文档.zip

    这个压缩包"在自己的应用程序中嵌入Office,操作word文档.zip"提供了相关的代码示例和资源,帮助开发者实现这一功能。下面将详细解释其中涉及的关键知识点: 1. **嵌入Office组件**:这通常通过COM(Component ...

    swt调用ocx应用实例(wps和windows本机播放器)

    WPS是一款兼容Microsoft Office的办公软件,其OCX控件可以被嵌入到其他应用程序中,用于显示和编辑Word文档。这个示例中,开发者通过SWT与OCX的交互,实现了在Java程序中打开和操作DOC文件的能力,这对于需要在无须...

    swing 嵌入word

    综上所述,通过结合Swing和SWT,我们可以实现Swing应用中嵌入Word文档的功能,这在需要在Java GUI中展示、编辑或交互Word文档的场景下非常有用。在实际开发中,务必根据具体需求和环境调整上述代码,并处理好可能...

    使用EclipseRCP进行桌面程序开发(四):在Windows中使用ActiveX控件

    OLE的体验,就是平时我们可以把Excel表格嵌入Word文档,或者把PDF嵌入浏览器显示一样,而ActiveX控件更是无处不在,做VB开发和网页开发的人都应该很熟悉。使用Windows系统中丰富的ActiveX控件资源,我们可以实现功能...

    JAVA中即时通讯内容扩展研究.pdf

    针对这一问题,本研究提出了一种在即时通讯软件中嵌入Word文档的方法,使得用户能够在聊天窗口内即时编辑和修改矢量图形、图表和公式等信息。这一方法的优势在于它能够支持多种XML标准的扩展标记语言,如SVG和MathML...

Global site tag (gtag.js) - Google Analytics