当前位置:编程学习 > JAVA >>

求助,jacob调用word后,一直遗留winword.exe

先上代码
import java.io.File;

import org.apache.log4j.Logger;

import com.jacob.activeX.ActiveXComponent;
import com.jacob.com.ComThread;
import com.jacob.com.Dispatch;
import com.jacob.com.Variant;

/**
 * 将jacob.dll放入JDK的bin目录下 把jacob.jar放入项目的WEB-INF\lib目录下
 */
public class JacobHelper {
private static Logger logger = Logger.getLogger(JacobHelper.class);
private ActiveXComponent word;
private Dispatch documents;
private Dispatch doc;
private Dispatch selection;

// private ActiveXComponent excel;
// private Dispatch workbooks;
// private Dispatch workbook;

private void wordInit() {
ComThread.InitMTA();
if (word == null) {

// 创建一个word对象
word = new ActiveXComponent("Word.Application");
word.setProperty("Visible", new Variant(false)); // word不可见
word.setProperty("AutomationSecurity", new Variant(3)); // 禁用宏
}

if (documents == null) {
// 获取文挡属性
documents = word.getProperty("Documents").toDispatch();
}
}

private void wordDestory() {
try {
if (doc != null) {
// Dispatch.call(doc, "Close", new Variant(true));
Dispatch.invoke(doc, "Close", Dispatch.Method,
new Object[] { new Variant(true) }, new int[1]);
doc.safeRelease();
doc = null;
}

if (word != null) {

Dispatch.call(word, "Close", new Variant(false));
word.invoke("Quit", new Variant[] {});
word.safeRelease();
word = null;
}
} catch (Exception ex) {
logger.error(ex);
ex.printStackTrace();
} finally {
ComThread.Release();
ComThread.quitMainSTA();
}
}

/**
 * 合并Word文档
 */
public boolean wordUnite(String[] filePath, String outputPath) {
if (filePath == null || filePath.length == 0)
return false;

boolean successful = false;
try {
wordInit();
// 添加一个新文挡
doc = Dispatch.call(documents, "Add").toDispatch();
selection = word.getProperty("Selection").toDispatch();
for (int i = 0; i < filePath.length; i++) {
Dispatch.call(selection, "insertFile", filePath[i]);
if (i < filePath.length - 1) {
Dispatch.call(selection, "TypeParagraph"); // 换行
}
}
// 设置页码
setPageNumber();
// 文件另存为
Dispatch.call(doc, "SaveAs", outputPath);
successful = true;
} catch (Exception ex) {
successful = false;
logger.error(ex);
ex.printStackTrace();
} finally {
wordDestory();
}

return successful;
}

/**
 * 设置页码
 */
private void setPageNumber() {
// 光标定位到首页
Dispatch.call(selection, "HomeKey", new Variant(6));
// 取得活动窗体对象
Dispatch ActiveWindow = word.getProperty("ActiveWindow").toDispatch();
// 取得活动窗格对象
Dispatch ActivePane = Dispatch.get(ActiveWindow, "ActivePane")
.toDispatch();
// 取得视图对象
Dispatch View = Dispatch.get(ActivePane, "View").toDispatch();
// 10是设置尾页(游标所在处)
Dispatch.put(View, "SeekView", new Variant(10)); // 设置页脚

selection = word.getProperty("Selection").toDispatch();// 当前页脚位置
// 页脚
Dispatch headerFooter = Dispatch.get(selection, "HeaderFooter")
.toDispatch();
// 页码
Dispatch pageNumbers = Dispatch.get(headerFooter, "PageNumbers")
.toDispatch();
// 居中显示页码,首页不显示
Dispatch.call(pageNumbers, "Add", new Variant(1), new Variant(false));
// 是在当前节开始
Dispatch.put(pageNumbers, "RestartNumberingAtSection",
new Variant(true));
// 开始页0
Dispatch.put(pageNumbers, "StartingNumber", 0);
// 退出页码设置视图
Dispatch.put(View, "SeekView", new Variant(0));

}

public static void main(String[] args) {
new JacobHelper().wordUnite(new String[] { "e:\\1.doc", "e:\\2.doc" },
"e:\\1.doc");
}


求助各位java高手,我的目的是把两个word文件合并成一个文件,但是为什么我这样子的写法,总是关不了进程,在服务器上遗留了很多的winword.exe进程,跪求帮助呀! --------------------编程问答-------------------- 你关闭WORD的处理,只写在正常处理的TRY里面了。如果前面的处理出现错误,就跳到CATCH,然后到FINALLY中,这两个地方却都没有关闭。

正确处理方法应该是把关闭WORD释放内存的语句写在FINALLY里,无论出不出错最后都执行它。 --------------------编程问答-------------------- 多谢theforever,我按照您说的方式,把释放资源代码放到finally里面去了,但是还是不行。 --------------------编程问答-------------------- 和我的一样
引用 1 楼  的回复:
你关闭WORD的处理,只写在正常处理的TRY里面了。如果前面的处理出现错误,就跳到CATCH,然后到FINALLY中,这两个地方却都没有关闭。

正确处理方法应该是把关闭WORD释放内存的语句写在FINALLY里,无论出不出错最后都执行它。
--------------------编程问答-------------------- 楼主,解决了吗?我现在和你一样问题 --------------------编程问答-------------------- 网上说这个可以:http://hi.baidu.com/vhqvtaeghkknoyr/item/bef83c2b232f86122a0f1c46

不过我这儿失败了,我这并发量太大了,进程不好搞的很。

C# 中有解决办法:Dispatch.NormalTemplate.Saved = true;

这么强大的jacob怎么就没有呢?
--------------------编程问答-------------------- 对了,这个和jacob版本有关系的。我目前测试出来的:jacob1.17的和jdk.16在一起搭档,就不会出现word进程的问题了。你们可以试试看。 --------------------编程问答-------------------- 再来看看,以为你们有新的方法了?上面写错了,是jdk1.6。如果有好的解决办法,大家共享哈么。
补充:Java ,  Web 开发
CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,