jacob合并多个word文档出现问题。

Chloe丶 2018-08-22 07:36:43
当我尝试用jacob合并多个word文档的时候 总是会报错,其中报错如下所示:

代码:
public class testWord {

public static void main(String[] args) {
List list = new ArrayList();
String file1= "D:\\wordtest\\2.doc";
String file2= "D:\\wordtest\\1.doc";
//String file3= "D:\\2.docx";
list.add(file1);
list.add(file2);
//list.add(file3);
uniteDoc(list,"d:\\wordtest\\file.doc");
}
public static void uniteDoc(List fileList, String savepaths) {
if (fileList.size() == 0 || fileList == null) {
return;
}
//打开word
ActiveXComponent app = new ActiveXComponent("Word.Application");//启动word
try {

// 设置word不可见 ---也就是设置ActiveXComponent对象的一个属性
app.setProperty("Visible", new Variant(false));
//获得documents对象----Variant。toDispatch()
Object docs = app.getProperty("Documents").toDispatch();
//打开第一个文件
//功能调用,对ActiveX对象docs的"Open"属性执行Dispatch.Method(Dispatch.Put\Dispatch.Get等)操作,执行后"Open"的值为参数4,参数5为错误参数码常定义为new int[1],(注:call、get和put方法都是通过该方法实现的)
Object doc = Dispatch
.invoke(
(Dispatch) docs, //调用目标
"Open",
Dispatch.Method,
new Object[] { (String) fileList.get(0),new Variant(false), new Variant(true) }, //参数4
new int[3]).toDispatch();//参数5
//追加文件
//和上面一样,不过换种通俗的解释:参数1就是这个invoke方法的主题操作对象,参数2就是要对参数1执行的方法的名字,参数3就是参数2这个方法的具体操作;参数4就是一系列的参数;参数5为错误参数码常定义为new int[1]
for (int i = 1; i < fileList.size(); i++) {
Dispatch.invoke(
app.getProperty("Selection").toDispatch(),//参数1
"insertFile", //参数2
Dispatch.Method, //参数3
new Object[] {(String) fileList.get(i), "",new Variant(false), new Variant(false),new Variant(false) },//参数4
new int[3]);//参数5
}
//保存新的word文件
Dispatch.invoke(
(Dispatch) doc,
"SaveAs",
Dispatch.Method,
new Object[] { savepaths, new Variant(1) },
new int[3]);
Variant f = new Variant(false);
Dispatch.call((Dispatch) doc, "Close", f);//该方法和get方法非常类似,他是把"Close"属性赋值给f
} catch (Exception e) {
throw new RuntimeException("合并word文件出错.原因:" + e);
} finally {
app.invoke("Quit", new Variant[] {});
}
}
}
...全文
441 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
lkext 2019-12-23
  • 打赏
  • 举报
回复
会不会是winserver的问题啊 本地win10没问题 部署上去 有问题 有人说是权限的问题
dinglei6666 2019-04-13
  • 打赏
  • 举报
回复
部署到服务器上后会报错吗?
引用 1 楼 丶初心丶 的回复:
我的是能成功,但是先打开的那个文件顺序成最后一个了。中间有的页合并成一页了
丶初心丶 2019-04-12
  • 打赏
  • 举报
回复
我的是能成功,但是先打开的那个文件顺序成最后一个了。中间有的页合并成一页了
Chloe丶 2018-08-26
  • 打赏
  • 举报
回复
来个大佬好不好 一直困惑我
Chloe丶 2018-08-23
  • 打赏
  • 举报
回复
没人吗?
Chloe丶 2018-08-22
  • 打赏
  • 举报
回复
我之前通过jacob创建word是成功的

50,336

社区成员

发帖
与我相关
我的任务
社区描述
Java相关技术讨论
javaspring bootspring cloud 技术论坛(原bbs)
社区管理员
  • Java相关社区
  • 小虚竹
  • 谙忆
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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