Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How Java connects COM objects

2025-02-27 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

这篇文章将为大家详细讲解有关Java如何连接COM对象,小编觉得挺实用的,因此分享给大家做个参考,希望大家阅读完这篇文章后可以有所收获。

问题是在CSDN上一网友提出的将 m$ word 转换为 XML 的Java程序问题(.NET/expert/topic/1091/1091620.xml)or(http://search.csdn.net/expert/topic/54/5406/2002/10/12/1091620.htm">http://www.csdn.net/expert/topic/1091/1091620.xml)or(http://search.csdn.net/expert/topic/54/5406/2002/10/12/1091620.htm). 现将一种简便的方法详细阐述于下:

1。下在Java-COM连接桥JACOB(http://danadler.com/jacob/),需要jacob.jar和jacob.dll两个文件就可以了。并把jacob.dll所在目录设到PATH。

2。调用程序(Exam_2.java)如下:

import java.io.File;

import com.jacob.com.*;

import com.jacob.ActiveX.*;

public class Exam_2 {

public static void main(String[] args) {

ActiveXComponent app = new ActiveXComponent("Word.Application");//启动word

String inFile = "H:Majix-1_2_2_fullsamplemy.doc";//要转换的word文件

String tpFile = "H:Majix-1_2_2_fullsamplemy.htm";//临时文件

String otFile = "H:Majix-1_2_2_fullsamplemy.xml";//目标文件

boolean flag = false;

try {

app.setProperty("Visible", new Variant(false));//设置word不可见

object docs = app.getProperty("Documents").toDispatch();

Object doc = Dispatch.invoke(docs,"Open", Dispatch.Method, new Object[]{inFile,new Variant(false), new Variant(true)}, new int[1]).toDispatch();//打开word文件

Dispatch.invoke(doc,"SaveAs", Dispatch.Method, new Object[]{tpFile,new Variant(8)}, new int[1]);//作为html格式保存到临时文件

Variant f = new Variant(false);

Dispatch.call(doc, "Close", f);

flag = true;

} catch (Exception e) {

e.printStackTrace();

} finally {

app.invoke("Quit", new Variant[] {});

}

if ( flag ) {

app = new ActiveXComponent("excel.Application");//启动excel

try {

app.setProperty("Visible", new Variant(false));//设置excel不可见

Object workbooks = app.getProperty("Workbooks").toDispatch();

Object workbook = Dispatch.invoke(workbooks,"Open",Dispatch.Method,new Object[]{tpFile,new Variant(false), new Variant(true)}, new int[1]).toDispatch();//打开临时文件

Dispatch.invoke(workbook,"SaveAs", Dispatch.Method, new Object[]{otFile,new Variant(46)}, new int[1]);//以xml格式保存到目标文件

Variant f = new Variant(false);

Dispatch.call(workbook, "Close", f);

} catch (Exception e) {

e.printStackTrace();

} finally {

app.invoke("Quit", new Variant[] {});

try {

File file = new File(tpFile);

file.delete();

} catch (Exception e) {

}

}

}

}

}

3。编译执行(确保jacob.dll在PATH中)

H:mytest>javac -classpath jacob.jar Exam_2.java

H:mytest>java -cp jacob.jar;. Exam_2

关于"Java如何连接COM对象"这篇文章就分享到这里了,希望以上内容可以对大家有一定的帮助,使各位可以学到更多知识,如果觉得文章不错,请把它分享出去让更多的人看到。

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report