风之语

posts(135) comments(158) trackbacks(1)
  • BlogJava
  • 联系
  • RSS 2.0 Feed 聚合
  • 管理

News

常用链接

  • 我的随笔
  • 我的评论
  • 我的参与
  • 最新评论

留言簿

  • 给我留言
  • 查看公开留言
  • 查看私人留言

随笔分类

  • ajax(1)
  • AppFuse(1)
  • iText(1)
  • JSF(8)
  • linux(3)
  • maven(1)
  • MSTR
  • Open XML(1)
  • Oracle(8)
  • RCP
  • Struts(2)
  • SybaseIQ(6)
  • tapestry
  • tomcat(2)
  • webservice(1)
  • 收藏(25)
  • 数据仓库(9)
  • 架构设计(3)
  • 生活(1)
  • 项目管理(2)

随笔档案

  • 2008年9月 (1)
  • 2008年7月 (2)
  • 2008年6月 (4)
  • 2008年5月 (6)
  • 2008年4月 (1)
  • 2008年3月 (1)
  • 2007年12月 (2)
  • 2007年11月 (5)
  • 2007年10月 (2)
  • 2007年9月 (3)
  • 2007年8月 (3)
  • 2007年4月 (1)
  • 2007年3月 (2)
  • 2007年2月 (2)
  • 2007年1月 (2)
  • 2006年12月 (3)
  • 2006年8月 (2)
  • 2006年7月 (2)
  • 2006年6月 (2)
  • 2006年4月 (2)
  • 2006年3月 (1)
  • 2006年2月 (3)
  • 2006年1月 (6)
  • 2005年12月 (6)
  • 2005年11月 (4)
  • 2005年10月 (17)
  • 2005年9月 (26)
  • 2005年8月 (16)
  • 2005年7月 (8)

相册

  • 技术图片

收藏夹

  • java

link

My wife

  • My wife's blog

最新随笔

  • 1. 设置正确的Content-Type以解决Ext的中文乱码问题
  • 2. 关于JFreechart柱状图 柱上不能显示数值的问题
  • 3. 快速创建个性化iGoogle主题的3个方法
  • 4. Apache Maven 2 简介(目前最全的技术资料了)
  • 5. 使用 AppFuse 的七个理由
  • 6. MyEclipse 6.5注册
  • 7. tomcat中的Server.xml元素详解
  • 8. Java 使用SWT 创建COM对象
  • 9.  如何在Java中嵌入IE
  • 10. 如何在Java内读取COM接口中所有的方法和属性

搜索

  •  

积分与排名

  • 积分 - 143413
  • 排名 - 52

最新评论

  • 1. re: 一个政府项目总结
  • 政府项目只是看上去很好 实际做起来很慢 能拖死你.....
  • --testtt
  • 2. re: hibernate3 大批量更新/删除数据 (update/delete)
  • hibernate批量更新或删除效果不理想,还是绕过hibernate 用JDBC吧
  • --ssss
  • 3. re: 基于Weblogic Server 8.1 ant工具开发Web Service
  • 如果知道 问题所以请联系rxiaoliang@sina.com
  • --liuxl
  • 4. re: 基于Weblogic Server 8.1 ant工具开发Web Service
  • 评论内容较长,点击标题查看
  • --liuxl
  • 5. re: MyEclipse4.0破解[未登录]
  • 这个破解文件怎么下载不下来啊
  • --long

阅读排行榜

评论排行榜

View Post

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

产品中要用到在Rcp程序中操作Excel。在网上goole了一下,发现一篇文章还不错,测试了一下通过。

Integrate Eclipse RCP with Microsoft Applications - Tutorial
Lars Vogel
<webmaster@vogella.de>


Version 0.5


Copyright © 2007 Lars Vogel

01.11.2007

Abstract

The Eclipse Rich Client Platform (RCP) is using the SWT GUI framework. SWT does allow to integrated Microsoft application via OLE (Object Linking and Embedding). This article will demonstrate how SWT can be used within an Eclipse RCP application to integrate / use Microsoft applications. Microsoft Outlook and Microsoft Excel are used as examples

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


--------------------------------------------------------------------------------

Table of Contents

1. Microsoft Object Linking and Embedding
1.1. Overview
2. Microsoft Integration - Sending an email via outlook
2.1. Create Project
2.2. Add action
2.3. Program the action
3. Microsoft Integration - Use Excel in your application.
3.1. Create Project
3.2. Change View code
4. Links and Literature
1. Microsoft Object Linking and Embedding
1.1. Overview
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.

2. Microsoft Integration - Sending an email via outlook
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 "OutlookTest" (see here how to create a new RCP project). Use the "Hello RCP " as a template.

2.2. Add action
Using extensions add action "sendEmail" (see here how to add an action). The class of this action should be "outlooktest.SendEmail". Run the application. The result should look like the following.

 


2.3. Program the action
Lets program the action. 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 outlooktest;

import java.io.File;

import org.eclipse.jface.action.IAction;
import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.jface.viewers.ISelection;
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;
import org.eclipse.ui.IWorkbenchWindow;
import org.eclipse.ui.IWorkbenchWindowActionDelegate;

public class SendEmail implements IWorkbenchWindowActionDelegate {

 private Shell shell;

 @Override
 public void dispose() {

  // TODO Auto-generated method stub

 }

 @Override
 public void init(IWorkbenchWindow window) {

  shell = window.getShell();

 }

 @Override
 public void run(IAction action) {

  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" */);

 }

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

 }

 @Override
 public void selectionChanged(IAction action, ISelection selection) {

  // TODO Auto-generated method stub

 }

 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 Integration - Use Excel in your application.
This example assume that you running on Microsoft and that you have a version of Microsoft Excel installed.

3.1. Create Project
Create a new project "ExcelTest" (see here how to create a new RCP project). 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. Links and Literature
http://www.eclipse.org/ Eclipse Website

http://www.vogella.de/articles/Eclipse/article.html Using Eclipse as IDE - Tutorial

http://www.vogella.de/articles/RichClientPlatform/article.html Eclipse Rich Client Platform - Tutorial

http://www.vogella.de/articles/EclipseCodeAccess/article.html A guide to access the Eclipse Sources

 

posted on 2008-05-17 09:21 风 阅读(343) 评论(0)  编辑  收藏

IT新闻  新用户注册  刷新评论列表  

标题  
姓名  
主页
验证码 *  
内容(请不要发表任何与政治相关的内容)  
  登录  使用高级评论  新用户注册  返回页首  恢复上次提交      
 
 
相关链接:
网站导航:
博客园      BlogJava   博客生活   IT博客网   C++博客   PHP博客   博客园社区
管理博客   教师博客     天文博客   汽车博客   足球博客   股票博客   电子博客  管理

 
Powered by:
BlogJava
Copyright © 风