请教高手,关于jacob操作word文档的问题,怎么把二份word合并成一个。

liuzhengkang 2010-07-23 06:26:47
请教高手,关于jacob操作word文档的问题,怎么把二份word合并成一个。

刚在网上找到这么一段代码,
jacob操作API
http://www.cnblogs.com/BruceLeey/archive/2009/09/28/1575577.html
但是没有复制、粘贴的方法,

我现在是想,打开二份word,把其中一个【2.doc】全选复制,然后粘贴到另一个【1.doc】文档的后面,再把【1.doc】另存为【3.doc】,这个【3.doc】就是我要的文档了。
请问下面这几个方法怎么用,我怎么试都不行。

Dispatch.call(range, "Copy");
Dispatch.call(textRange, "Paste");
...全文
793 7 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
chengLoginJava 2012-11-22
  • 打赏
  • 举报
回复
关注一下,楼主要是解决了共享一下吧。
xudan58 2012-10-29
  • 打赏
  • 举报
回复
5楼的用了,是可以的,但是如果我的2个文档,一个是横向排列,一个是纵向排列的,页眉也不一样,合并后,却以第一个文档的设置为准,第二个文档也横向排了,我要的是,保持原来格式不变,只是向下黏贴(每个文档后都换页了,但是要么全是横的,要么全是纵的,这样不好)
renxj1106 2011-09-08
  • 打赏
  • 举报
回复
package com.sinosoft.dao;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

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

public class Word2Pdf {

static final int wdDoNotSaveChanges = 0;// 不保存待定的更改。
static final int wdFormatPDF = 17;// PDF 格式

public static void main(String[] args) {
List list = new ArrayList();
File file1= new File("D:\\file1.doc");
File file2= new File("D:\\file2.doc");
File file3= new File("D:\\file3.doc");
list.add(file1);
list.add(file2);
list.add(file3);
uniteDoc(list,"d:\\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不可见
app.setProperty("Visible", new Variant(false));
//获得documents对象
Object docs = app.getProperty("Documents").toDispatch();
//打开第一个文件
Object doc = Dispatch
.invoke(
(Dispatch) docs,
"Open",
Dispatch.Method,
new Object[] { (String) fileList.get(0),
new Variant(false), new Variant(true) },
new int[3]).toDispatch();
//追加文件
for (int i = 1; i < fileList.size(); i++) {

Dispatch.invoke(app.getProperty("Selection").toDispatch(),
"insertFile", Dispatch.Method, new Object[] {
(String) fileList.get(i), "",
new Variant(false), new Variant(false),
new Variant(false) }, new int[3]);
}

//保存新的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);

} catch (Exception e) {
throw new RuntimeException("合并word文件出错.原因:" + e);
} finally {
app.invoke("Quit", new Variant[] {});
}

}
}
liuzhengkang 2010-07-24
  • 打赏
  • 举报
回复
帖子又沉了,顶上去,希望高手帮忙。。
liuzhengkang 2010-07-24
  • 打赏
  • 举报
回复
期待高手。。。
liuzhengkang 2010-07-23
  • 打赏
  • 举报
回复
不用POI。jacob怎么操作啊。
Silence_Smile 2010-07-23
  • 打赏
  • 举报
回复
用POI,直接取出内容,再合并

62,634

社区成员

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

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