java 读写word

lwl_java 2011-03-22 04:59:54
Exception in thread "main" com.jacob.com.ComFailException: A COM exception has been encountered:
At Invoke of: Documents
Description: An unknown COM error has occured.
at com.jacob.com.Dispatch.invokev(Native Method)
at com.jacob.activeX.ActiveXComponent.getProperty(ActiveXComponent.java)
at com.test.MSWordManager.<init>(MSWordManager.java:32)
at com.test.MSWordManager.main(MSWordManager.java:647)








public MSWordManager(boolean visible) {
if (word == null) {
word = new ActiveXComponent("Word.Application");
word.setProperty("Visible", new Variant(visible));
}
if (documents == null)
documents = word.getProperty("Documents").toDispatch();
}
...全文
721 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
孤鸿掠影 2011-03-24
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 guyong1018 的回复:]
有没有将jacob-1.15-M3-x86.dll文件拷贝至jdk的bin目录下?给你一个类做参考,这个类是将word转成html:
import java.io.File;
import java.io.IOException;
import java.util.Scanner;

import com.jacob.com.*;
import com.jacob.activeX.*;……
[/Quote]

ls很给力,学习下。
星辰 2011-03-23
  • 打赏
  • 举报
回复
有没有将jacob-1.15-M3-x86.dll文件拷贝至jdk的bin目录下?给你一个类做参考,这个类是将word转成html:
import java.io.File;
import java.io.IOException;
import java.util.Scanner;

import com.jacob.com.*;
import com.jacob.activeX.*;

/**
*
* @author 将word转换成html文件,单个文件转换 word转换后进程会自动释放, 但是一次只能转一个文件,
* 如果同时转多个就会报错,进程不释放,占资源,所以 写了杀掉进程的代码
*
*/
public class WordtoHtml {
/**
* 文档转换函数
*
* @param docfile
* word文档的绝对路径加文件名(包含扩展名)
* @param htmlfile
* 转换后的html文件绝对路径和文件名(包含扩展名)
*/
public static void change(String docfile, String htmlfile, boolean bestrow) {
if (!bestrow) {
File file = new File(htmlfile);
if (file.exists()) {
System.out.println(DateTime.getSystemDateTime() + " 文件已存在,不转换:"
+ htmlfile);
return;
}
}
System.out.println(DateTime.getSystemDateTime() + " 开始转换:" + docfile);
long start = System.currentTimeMillis();
ActiveXComponent app = new ActiveXComponent("Word.Application"); // 启动word
try {
app.setProperty("Visible", new Variant(false));
Dispatch docs = app.getProperty("Documents").toDispatch();
Dispatch doc = Dispatch.invoke(
docs,
"Open",
Dispatch.Method,
new Object[] { docfile, new Variant(false),
new Variant(true) }, new int[1]).toDispatch();
Dispatch.invoke(doc, "SaveAs", Dispatch.Method, new Object[] {
htmlfile, new Variant(8) }, new int[1]);
Variant f = new Variant(false);
Dispatch.call(doc, "Close", f);
long end = System.currentTimeMillis();
System.out.println(DateTime.getSystemDateTime() + " 转换时间:"
+ (end - start) + " 毫秒!");
} catch (com.jacob.com.ComFailException e) {
e.printStackTrace();
} finally {
app.invoke("Quit", new Variant[] {});
app = null;
Process process;
int pid = 0;
try {
// 获取系统进程
process = Runtime.getRuntime().exec("tasklist");
Scanner in = new Scanner(process.getInputStream());
while (in.hasNextLine()) {
String p = in.nextLine();
if (p.contains("WINWORD.EXE")) {
System.out.println(DateTime.getSystemDateTime()
+ " 获取WINWORD.EXE进程,进程释放");
StringBuffer buf = new StringBuffer();
for (int i = 0; i < p.length(); i++) {
char ch = p.charAt(i);
if (ch != ' ') {
buf.append(ch);
}
}
try {
pid = Integer
.parseInt(buf.toString().split("Console")[0]

.substring("WINWORD.EXE".length()));
} catch (NumberFormatException e) {
e.printStackTrace();
}
Runtime.getRuntime().exec("tskill" + " " + pid);
System.out.println(DateTime.getSystemDateTime()
+ " 释放WINWORD.EXE进程成功");
}
}
} catch (IOException e) {
e.printStackTrace();
}
}
}

public static void main(String[] strs) {
long a = System.currentTimeMillis();
WordtoHtml.change("c:\\信息化建设项目解决方案V1.6.doc",
"c:\\test0a.html",false);
long b = System.currentTimeMillis();
System.out.println(b - a);
}
}
zl_love_IT 2011-03-22
  • 打赏
  • 举报
回复
错误信息贴出来

23,404

社区成员

发帖
与我相关
我的任务
社区描述
Java 非技术区
社区管理员
  • 非技术区社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧