在Java中嵌入Office
发布时间:2020-05-24 14:36:24 所属栏目:Java 来源:互联网
导读:我试图使用以下代码使用SWT将Office 2007/2010应用程序嵌入 Java应用程序中: import java.awt.Canvas;import javax.swing.JFrame;import org.eclipse.swt.SWT;import org.eclipse.swt.awt.SWT_AWT;import org.eclipse.swt.layout.FillL
|
我试图使用以下代码使用SWT将Office 2007/2010应用程序嵌入 Java应用程序中: import java.awt.Canvas;
import javax.swing.JFrame;
import org.eclipse.swt.SWT;
import org.eclipse.swt.awt.SWT_AWT;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.ole.win32.*;
import org.eclipse.swt.widgets.*;
public class EmbeddingTest extends Canvas {
private void initOleViewer(String target) {
Display display = new Display();
Shell shell = SWT_AWT.new_Shell(display,this);
shell.setLayout(new FillLayout());
OleFrame oleFrame = new OleFrame(shell,SWT.NONE);
OleControlSite oleControlSite = new OleControlSite(oleFrame,SWT.NONE,"Word.Document");
oleControlSite.doVerb(OLE.OLEIVERB_INPLACEACTIVATE);
OleAutomation word = new OleAutomation(oleControlSite);
int[] applicationId = word.getIDsOfNames(new String[]{"Application"});
Variant property = word.getProperty(applicationId[0]);
OleAutomation application = property.getAutomation();
int[] documentId = application.getIDsOfNames(new String[]{"Documents"});
property = application.getProperty(documentId[0]);
OleAutomation documents = property.getAutomation();
shell.open();
Variant[] arguments = new Variant[] { new Variant(target) };
int[] automationIDs = documents.getIDsOfNames(new String[]{"Open","FileName"});
documents.invokeNoReply(automationIDs[0],arguments,new int[]{automationIDs[1]});
while (!shell.isDisposed()) {
if (!display.readAndDispatch()) {
display.sleep();
}
}
}
public static void main(String[] args) {
JFrame jFrame = new JFrame("Embedding Test");
jFrame.setVisible(true);
EmbeddingTest viewer = new EmbeddingTest();
jFrame.add(viewer);
jFrame.setSize(600,600);
viewer.initOleViewer(args[0]);
}
}
当我不尝试在文档对象上调用“打开”Word在应用程序内成功嵌入,但是整个文件菜单被禁用.当我打“打开”应用程序崩溃与以下错误(DISP_E_EXCEPTION):
有没有人知道如何解决这个问题或一个替代解决方案来嵌入Office应用程序在Java?谢谢! 更新: 单独查询“打开”和“文件名”的ID为’FileName’返回null,因此它的值不正确.我也尝试没有命名参数没有任何成功: documents.invokeNoReply(automationIDs[0],arguments); 解决方法你为什么不做任何错误处理,结果检查或断言?请记住,getIDsOfNames(..)将默认失败并返回空值,以供无法识别的名称.在捕获有问题的异常之后,尝试打印documents.getLastError()的值. (编辑:安卓应用网) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
相关内容
- JDK1.8、JDK1.7、JDK1.6区别看这里
- iOS10添加本地推送(Local Notification)实例
- java8 stream的分组功能实例介绍
- Spring配置中transactionAttributes的使用方法介绍
- Apache的开源项目Commons Email发送邮件
- performance – 在四元组的向量中取消装箱的盒装值
- transactionAttributes各属性意义及配置
- java – 在同一对象中测试方法时如何模拟对象中的方法
- java – 什么时候应该抛出一个方法抛出InterruptedExceptio
- java – 在eclipse中使用星形图标的方法是什么?
