RCP中整合Office (excel 、word、outlook)

本文介绍如何使用Eclipse RCP集成Microsoft Office产品,包括Outlook、Excel和文件资源管理器。通过具体步骤和代码示例展示如何实现这些功能。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

本文主要介绍了如何在RCP中集成Office的产品。

转载于http://www.vogella.de/articles/EclipseMicrosoftIntegration/article.html

Microsoft and Java Integration with Eclipse - Tutorial

Lars Vogel

 

Version 0.5

04.05.2009

Revision History
Revision 0.1-0.501.11.2007Lars Vogel
Created
Revision 0.604.05.2009Lars Vogel
Embed explorer into RCP, Outlook example upgraded from action to command

Eclipse Microsoft Integration

This article will demonstrate how Eclipse SWT can be used to integrate / use Microsoft applications. Eclipse is using the SWT GUI framework. SWT does allow to integrated Microsoft application via OLE (Object Linking and Embedding).Microsoft Outlook, Microsoft Excel and the File explorer are used as examples. The examples used are based on Eclipse RCP as this makes it easy to demonstrate the usage but work also with standalone Java application (which are using SWT).

This article assumes that you are already familiar with using the Eclipse IDE and with developing simple Eclipse RCP applications.


1. Microsoft Object Linking and Embedding

Windows applications use OLE (Object Linking and Embedding) to allow applications to control other application objects. The following will show how to do this using a few examples.

Obviously the following examples assume that your are running on Microsoft Windows and have the application which is discussed installed.

2. Microsoft Outlook with Eclipse

This example assumes you running an MS operating system and that you have a version of Outlook installed.

2.1. Create Project

Create a new project "de.vogella.microsoft.outlook". See Eclipse RCP for details. Use the "Hello RCP " as a template.

2.2. Create a command

Using extensions create the command "de.vogella.microsoft.outlook.sendEmail" and add it to the menu. See Eclipse command for details. Program the default handler "de.vogella.microsoft.outlook.handler.SendEmail"

The following coding will create and open the email for the user. It also assumes that you have a file c:/temp/test.txt which will be attached to the email.

 

				
package de.vogella.microsoft.outlook.handler;

import java.io.File;

import org.eclipse.core.commands.AbstractHandler;
import org.eclipse.core.commands.ExecutionEvent;
import org.eclipse.core.commands.ExecutionException;
import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.swt.SWT;
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;

public class SendEmail extends AbstractHandler {

	@Override
	public Object execute(ExecutionEvent event) throws ExecutionException {
		Display display = Display.getCurrent();
		Shell shell = new Shell(display);
		OleFrame frame = new OleFrame(shell, SWT.NONE);
		// This should start outlook if it is not running yet
		OleClientSite site = new OleClientSite(frame, SWT.NONE, "OVCtl.OVCtl");
		site.doVerb(OLE.OLEIVERB_INPLACEACTIVATE);
		// Now get the outlook application
		OleClientSite site2 = new OleClientSite(frame, SWT.NONE,
				"Outlook.Application");
		OleAutomation outlook = new OleAutomation(site2);
		// 
		OleAutomation mail = invoke(outlook, "CreateItem", 0 /* Mail item */)
				.getAutomation();
		setProperty(mail, "To", "test@gmail.com"); /*
													 * Empty but could also be
													 * predefined
													 */
		setProperty(mail, "Bcc", "test@gmail.com"); /*
													 * Empty but could also be
													 * predefined
													 */
		setProperty(mail, "BodyFormat", 2 /* HTML */);
		setProperty(mail, "Subject", "Top News for you");
		setProperty(mail, "HtmlBody",
				"<html>Hello<p>, please find some infos here.</html>");
		File file = new File("c:/temp/test.txt");
		if (file.exists()) {
			OleAutomation attachments = getProperty(mail, "Attachments");
			invoke(attachments, "Add", "c:/temp/test.txt");
		} else {
			MessageDialog
					.openInformation(shell, "Info",
							"Attachment File c:/temp/test.txt not found; will send email with attachment");
		}
		invoke(mail, "Display" /* or "Send" */);
		return null;
	}

	private static OleAutomation getProperty(OleAutomation auto, String name) {
		Variant varResult = auto.getProperty(property(auto, name));
		if (varResult != null && varResult.getType() != OLE.VT_EMPTY) {
			OleAutomation result = varResult.getAutomation();
			varResult.dispose();
			return result;
		}
		return null;
	}

	private static Variant invoke(OleAutomation auto, String command,
			String value) {
		return auto.invoke(property(auto, command),
				new Variant[] { new Variant(value) });
	}

	private static Variant invoke(OleAutomation auto, String command) {
		return auto.invoke(property(auto, command));
	}

	private static Variant invoke(OleAutomation auto, String command, int value) {
		return auto.invoke(property(auto, command),
				new Variant[] { new Variant(value) });
	}

	private static boolean setProperty(OleAutomation auto, String name,
			String value) {
		return auto.setProperty(property(auto, name), new Variant(value));
	}

	private static boolean setProperty(OleAutomation auto, String name,
			int value) {
		return auto.setProperty(property(auto, name), new Variant(value));
	}

	private static int property(OleAutomation auto, String name) {
		return auto.getIDsOfNames(new String[] { name })[0];
	}

}

			

If you now start the application and press the button an email should be prepared and shown to the user.

 

3.  Microsoft Excel with Eclipse

3.1. Create Project

Create a new project "ExcelTest". See Eclipse RCP for details. Use the "RCP with a view" as a template. Run it and see that is working.

3.2. Change View code

Select View.java and replace the coding with the following.

 

				
package exceltest;

import org.eclipse.swt.SWT;
import org.eclipse.swt.SWTError;
import org.eclipse.swt.ole.win32.OleClientSite;
import org.eclipse.swt.ole.win32.OleFrame;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.ui.part.ViewPart;

public class View extends ViewPart {

	public static final String ID = "ExcelTest.view";
	private OleClientSite site;

	public View() {
	}

	@Override
	public void createPartControl(Composite parent) {
		try {
			OleFrame frame = new OleFrame(parent, SWT.NONE);
			site = new OleClientSite(frame, SWT.NONE, "Excel.Sheet");
		} catch (SWTError e) {
			System.out.println("Unable to open activeX control");
			return;
		}
	}

	@Override
	public void setFocus() {
		// Have to set the focus see https://bugs.eclipse.org/bugs/show_bug.cgi?id=207688
		 site.setFocus();
	}

}

			

Start your application and excel should be displayed.

 

4.  Microsoft File Explorer with Eclipse

Create a new project "de.vogella.microsoft.fileexplorer". See Eclipse RCP for details.

Add a view to the application and the perspective. To display the file explorer you can use the following code for your view.

 

			
package de.vogella.microsoft.fileexplorer;

import org.eclipse.swt.SWT;
import org.eclipse.swt.SWTError;
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.Composite;
import org.eclipse.ui.part.ViewPart;

public class FileExplorerView extends ViewPart {
	private OleClientSite site;
	static final int Navigate = 0x68;

	@Override
	public void createPartControl(Composite parent) {
		try {
			OleFrame frame = new OleFrame(parent, SWT.NONE);
			site = new OleClientSite(frame, SWT.NONE, "Shell.Explorer.1");
			site.doVerb(OLE.OLEIVERB_INPLACEACTIVATE);
			OleAutomation auto = new OleAutomation(site);
			auto.invoke(Navigate, new Variant[] { new Variant("c://temp") });
		} catch (SWTError e) {
			System.out.println("Unable to open activeX control");
			return;
		}

	}

	@Override
	public void setFocus() {
		site.setFocus();
	}

}

		

5. Thank you

Thank you for practicing with this tutorial.

I maintain this tutorial in my private time. If you like the information please help me by using flattr or donating or by recommending this tutorial to other people.
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值