RCP中整合Office (excel 、word、outlook)
来源:互联网 发布:手机淘宝做代理的步骤 编辑:程序博客网 时间:2024/04/27 23:04
本文主要介绍了如何在RCP中集成Office的产品。
转载于http://www.vogella.de/articles/EclipseMicrosoftIntegration/article.html
Microsoft and Java Integration with Eclipse - Tutorial
Version 0.5
Copyright © 2007 - 2009 Lars Vogel
04.05.2009
Eclipse Microsoft Integration
This article will demonstrate how Eclipse SWT can be used tointegrate / use Microsoft applications. Eclipse is using the SWT GUIframework. SWT does allow to integrated Microsoft application viaOLE (Object Linking and Embedding).Microsoft Outlook, MicrosoftExcel and the File explorer are used as examples. The examples usedare based on Eclipse RCP as this makes it easy to demonstrate theusage but work also with standalone Java application (which areusing SWT).
This article assumes that you are already familiar with usingthe Eclipse IDE and with developing simple Eclipse RCP applications.
Table of Contents
- 1. Microsoft Object Linking and Embedding
- 2. Microsoft Outlook with Eclipse
- 2.1. Create Project
- 2.2. Create a command
- 3. Microsoft Excel with Eclipse
- 3.1. Create Project
- 3.2. Change View code
- 4. Microsoft File Explorer with Eclipse
- 5. Thank you
- 6. Questions and Discussion
- 7. Links and Literature
- 7.1. Source Code
- 7.2. vogella Resources
1. Microsoft Object Linking and Embedding
Windows applications use OLE (Object Linking and Embedding) toallow applications to control other application objects. The followingwill show how to do this using a few examples.
Obviously the following examples assume that your are running onMicrosoft Windows and have the application which is discussedinstalled.
2. Microsoft Outlook with Eclipse
This example assumes you running an MS operating system and thatyou 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 commandfor details. Program the default handler"de.vogella.microsoft.outlook.handler.SendEmail"
The following coding will create and open the email for theuser. It also assumes that you have a file c:/temp/test.txt whichwill 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 {@Overridepublic 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 yetOleClientSite site = new OleClientSite(frame, SWT.NONE, "OVCtl.OVCtl");site.doVerb(OLE.OLEIVERB_INPLACEACTIVATE);// Now get the outlook applicationOleClientSite 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 emailshould be prepared and shown to the user.
3. Microsoft Excel with Eclipse
3.1. Create Project
Create a new project "ExcelTest". See Eclipse RCPfor details. Use the "RCP with a view" as atemplate. 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() {}@Overridepublic 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;}}@Overridepublic 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 RCPfor details.
Add a view to the application and the perspective. To displaythe 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;@Overridepublic 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;}}@Overridepublic void setFocus() {site.setFocus();}}
5. Thank you
Thank you for practicing with this tutorial.
I maintain this tutorial in my private time. If you likethe informationplease help me by using flattr or donating or by recommending this tutorial to other people.- RCP中整合Office (excel 、word、outlook)
- Visual Studio Tools for Office : Using Visual Basic 2005 with Excel, Word, Outlook, and InfoPath
- Visual Studio Tools for Office: Using C# with Excel, Word, Outlook, and InfoPath
- Office and SharePoint 2007 User's Guide: Integrating SharePoint with Excel, Outlook, Access and Word
- VSTO 得到Office文档的选中内容(Word、Excel、PPT、Outlook)
- Java 操作Word Excel Outlook.
- Winform中显示Office文档(word,EXCEL,PPT)
- win8 DCOM 中不存在WORD、EXCEL等OFFICE组件解决方案
- 在DCOM中不存在WORD、EXCEL等OFFICE组件
- Winform中显示Office文档(word,EXCEL,PPT)
- 在DCOM 中不存在WORD、EXCEL等OFFICE组件
- 在DCOM 中不存在WORD、EXCEL等OFFICE组件
- VC Office 自动化(Word、Excel)
- C# OFFICE WORD EXCEL操作
- Java操作Word,Excel,Access,Outlook
- Java com bridge 操作Word Excel Outlook.
- Office技巧--怎样清除Outlook邮件地址 在word里换页
- C#的office操作[word&Excel]
- 花了两天时间,写了个lex
- -=[ WinDocker - AutoHotKey Script Hack ]=-
- AS3:时间戳的转换
- 项目管理中,最难管的是什么?
- ffmpeg快速应用开发
- RCP中整合Office (excel 、word、outlook)
- How can I create a object for a note?
- 使用JAR API打包
- [Android] ViewStack演示
- 补习C语言之在.h文件中定义全局变量
- android界面 slidingdrawer抽屉 从左侧拉出效果
- 最常见的20种VC++ 6.0编译错误信息集合
- 心痛我以前的论坛,感谢CSDN。
- javascript 兼容多种浏览器