使用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");
}
相关文章推荐
- java对世界各个时区(TimeZone)的通用转换处理方法(转载)
- java-注解annotation
- java-模拟tomcat服务器
- java-用HttpURLConnection发送Http请求.
- java-WEB中的监听器Lisener
- Android IPC进程间通讯机制
- Android Native 绘图方法
- Android java 与 javascript互访(相互调用)的方法例子
- 介绍一款信息管理系统的开源框架---jeecg
- 聚类算法之kmeans算法java版本
- java实现 PageRank算法
- PropertyChangeListener简单理解
- c++11 + SDL2 + ffmpeg +OpenAL + java = Android播放器
- 插入排序
- 冒泡排序
- 堆排序
- 快速排序
- 二叉查找树