您的位置:首页 > 编程语言 > Java开发

使用Jacob合并多个word文档生成一个word文档

2016-06-24 11:41 645 查看
public void uniteDoc(String[] tempIds, String savepaths) {
String rootPath = ****;
if (ObjectUtil.isNull(tempIds) || tempIds.length <= 0 || ObjectUtil.isNull(tempIds[0])) {
return;
}
//将jacob-1.17-M2-x64.dll文件拷到下面打印出来的目录
System.out.println("---------->"+System.getProperty("java.library.path"));
//打开word
ActiveXComponent app = new ActiveXComponent("Word.Application");//启动word
try {
//设置word不可见
app.setProperty("Visible", new Variant(false));
//获得documents对象
Object docs = app.getProperty("Documents").toDispatch();
//打开第一个文件
Temp firstTemp = this.findById(tempIds[0]);
Object doc = Dispatch.invoke(
(Dispatch) docs,
"Open",
Dispatch.Method,
new Object[] { (String) rootPath+firstTemp.getFjurl(),
new Variant(false), new Variant(true) },
new int[3]).toDispatch();
//获得当前文档选定部分或光标所在位置
Dispatch selection = app.getProperty("Selection").toDispatch();
//光标定位到文档首位置
Dispatch.call(selection, "HomeKey", new Variant(6));
//在光标所在位置插入文本
insertFormatParagraph(selection, "类型:"+firstTemp.getType().getTypeName(),
true, false, false, null, "10.5", "宋体", "0");
insertFormatParagraph(selection, "来源:"+firstTemp.getUnit().getName(),
true, false, false, null, "10.5", "宋体", "0");
insertFormatParagraph(selection, "标题:"+firstTemp.getTitle(),
true, false, false, null, "10.5", "宋体", "0");
//光标定位到文档末尾位置
Dispatch.call(selection, "EndKey", new Variant(6));
Dispatch.call(selection, "TypeParagraph");
//插入一个分页符
Dispatch.call(selection, "InsertBreak");

//追加文件
for (int i = 1; i < tempIds.length; i++) {
Temp temp = this.findById(tempIds[i]);
if(!ObjectUtil.isNull(temp.getFjurl())){
File file = new File(rootPath+temp.getFjurl());
if(file.exists() && temp.getFjurl().endsWith("doc")){
//每一篇文档前添加报送类型、单位和标题
insertFormatParagraph(selection, "类型:"+temp.getType().getTypeName(),
true, false, false, null, "10.5", "宋体", "0");
insertFormatParagraph(selection, "来源:"+temp.getUnit().getName(),
true, false, false, null, "10.5", "宋体", "0");
insertFormatParagraph(selection, "标题:"+temp.getTitle(),
true, false, false, null, "10.5", "宋体", "0");
//文档内容
Dispatch.invoke(selection,
"insertFile", Dispatch.Method, new Object[] {
(String) rootPath+temp.getFjurl(), "",
new Variant(false), new Variant(false),
new Variant(false) }, new int[3]);
//每一篇文档后添加分页符
Dispatch.call(selection, "EndKey", new Variant(6));
Dispatch.call(selection, "TypeParagraph");
if(i != tempIds.length-1){
Dispatch.call(selection, "InsertBreak");
}
}
}
}
//保存新的word文件
File folder = new File(savepaths.substring(0, savepaths.lastIndexOf("/")+1));
if(!folder.exists() && !folder.isDirectory()){
folder.mkdirs();
}
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[] {});
}
}


另写了一个方法,在光标位置插入一个带格式的文本段落。

/**
* 在当前光标位置插入(或将选定内容替换成) 一段带格式的文本段落
* @param selection 光标位置或选定内容
* @param text 插入的内容
* @param isBold 是否加粗
* @param isItalic 是否斜体
* @param isUnderLine 是否加下划线
* @param color 字体颜色
* @param size 字号(只能是数字)
* @param name 字体名称
* @param align 对齐方式:0左对齐,1居中,2右对齐,3两端对齐,4分散对齐
*/
private void insertFormatParagraph(Dispatch selection, String text, boolean isBold, boolean isItalic,
boolean isUnderLine, String color, String size, String name, String align){
//在光标所在位置插入文本(插入完后默认选中插入内容)
Dispatch.put(selection, "Text", text);
//设置选中部分字体格式
Dispatch font = Dispatch.get(selection, "Font").toDispatch();
if(!ObjectUtil.isNull(name)){
Dispatch.put(font, "Name", new Variant(name));
}
Dispatch.put(font, "Bold", new Variant(isBold));
Dispatch.put(font, "Italic", new Variant(isItalic));
Dispatch.put(font, "UnderLine", new Variant(isUnderLine));
if(!ObjectUtil.isNull(color)){
Dispatch.put(font, "Color", color);
}
if(!ObjectUtil.isNull(size)){
Dispatch.put(font, "Size", size);
}
if(!ObjectUtil.isNull(align)){
Dispatch paragraphs = Dispatch.get(selection, "Paragraphs").toDispatch();
Dispatch.put(paragraphs, "Alignment", new Variant(align));
}
//光标移动(取消选定内容)
Dispatch.call(selection, "Move");
//设置为一个段落(换行)
Dispatch.call(selection, "TypeParagraph");
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  Jacob word java