使用JACOB进行Word编程示例2015年7月2日13:44Jacob项目的官方地址:/projects/jacob-project/官方介绍:JACOB is a JAVA-COM Bridge that allows you to call COM Automation components from Java. It uses JNI to make native calls to the COM libraries. JACOB runs on x86 and x64 environments supporting 32 bit and 64 bit JVMs它是一个使用jni来调用com组件及其库文件的工具。
这里仅介绍处理word文档。
首先要部署其dll文件。
jacob-1.14.3-x86.dll 或 jacob-1.14.3-x64.dll,下载的文件解压后即可看到。
我是直接将dll文件放在windows\system32目录下的,方便,当然也有其他做法。
其次,就是把其jar文件路径加入Windows路径中。
接着就可以在你的Java程序中调用了。
我总结下大概有下面几个阶段:1. 初始化相关参数,代码如下:// 打开Word应用程序ActiveXComponent app = new ActiveXComponent("Word.Application");// 设置word不可见app.setProperty("Visible", new Variant(false));// 打开word文件Dispatch word = app.getProperty("Documents").toDispatch();Dispatch doc = Dispatch.invoke(word, "Open", Dispatch.Method, new Object[] {" File Name", new Variant(false), new Variant(false) }, new int[1]).toDispatch();//获取指针Dispatch cursor = app.getProperty("Selection").toDispatch();如上,我们打开了一个名为File Name的word文件,你可能会觉得最后那句看起来很繁琐,是的,有更好的代替方式,如下:Dispatch doc = Dispatch.call(word, "Open", new Variant(false), new Variant(false) ).toDisptach();这正是Jacob给我们提供的两种调用方式。
2. 处理word文档,如果你先前有VBA相关开发经验,那就简单了。
没有的也没事,打开office程序目录下的2052\VBAWD10.chm,这里有Word的对象模型的所有记录。
接着讲述下Jacob中两种最常用的类型:Variant:这是一种可变的类型,用于Jacob中几乎所有函数的返回值,并可转换为其他任何类型,包括Java基本类型。
当然,应该按需要与规则来。
Dispatch:Object represents MS level dispatch object. Each instance of this points at some data structure on the MS windows side.就是说可以代表所有对象模型。
import java.util.ArrayList;import java.util.List;import com.jacob.activeX.ActiveXComponent;import Thread;import .Dispatch;import .Variant;import org.apache.log4j.Logger;/****作用:利用jacob插件生成word 文件!***/public class DOCWriter {/** 日志记录器 */static private Logger logger = Logger.getLogger(DOCWriter.class);/** word文档** 在本类中有两种方式可以进行文档的创建,<br>* 第一种调用 createNewDocument* 第二种调用 openDocument*/private Dispatch document = null;/** word运行程序对象 */private ActiveXComponent word = null;/** 所有word文档 */private Dispatch documents = null;/*** Selection 对象代表窗口或窗格中的当前所选内容。
所选内容代表文档中选定(或突出显示)的区域,如果文档中没有选定任何内容,则代表插入点。
* 每个文档窗格只能有一个Selection 对象,并且在整个应用程序中只能有一个活动的Selection 对象。
*/private Dispatch selection = null;/**** Range 对象代表文档中的一个连续区域。
每个 Range 对象由一个起始字符位置和一个终止字符位置定义。
* 说明:与书签在文档中的使用方法类似,Range 对象在 Visual Basic 过程中用来标识文档的特定部分。
* 但与书签不同的是,Range对象只在定义该对象的过程运行时才存在。
* Range对象独立于所选内容。
也就是说,您可以定义和处理一个范围而无需更改所选内容。
还可以在文档中定义多个范围,但每个窗格中只能有一个所选内容。
*/private Dispatch range = null;/*** PageSetup 对象该对象包含文档的所有页面设置属性(如左边距、下边距和纸张大小)。
*/private Dispatch pageSetup = null;/** 文档中的所有表格对象 */private Dispatch tables = null;/** 一个表格对象 */private Dispatch table = null;/** 表格所有行对象 */private Dispatch rows = null;/** 表格所有列对象 */private Dispatch cols = null;/** 表格指定行对象 */private Dispatch row = null;/** 表格指定列对象 */private Dispatch col = null;/** 表格中指定的单元格 */private Dispatch cell = null;/** 字体 */private Dispatch font = null;/** 对齐方式 */private Dispatch alignment = null;/*** 构造方法*/public DOCWriter() {if(this.word == null){/* 初始化应用所要用到的对象实例 */this.word = new ActiveXComponent("Word.Application");/* 设置Word文档是否可见,true-可见false-不可见 */this.word.setProperty("Visible",new Variant(true));/* 禁用宏 */this.word.setProperty("AutomationSecurity", new Variant(3)); }if(this.documents == null){this.documents = word.getProperty("Documents").toDispatch(); }}/*** 设置页面方向和页边距** @param orientation* 可取值0或1,分别代表横向和纵向* @param leftMargin* 左边距的值* @param rightMargin* 右边距的值* @param topMargin* 上边距的值* @param buttomMargin* 下边距的值*/public void setPageSetup(int orientation, int leftMargin,int rightMargin, int topMargin, int buttomMargin) {logger.debug("设置页面方向和页边距...");if(this.pageSetup == null){this.getPageSetup();}Dispatch.put(pageSetup, "Orientation", orientation);Dispatch.put(pageSetup, "LeftMargin", leftMargin);Dispatch.put(pageSetup, "RightMargin", rightMargin);Dispatch.put(pageSetup, "TopMargin", topMargin);Dispatch.put(pageSetup, "BottomMargin", buttomMargin);}/*** 打开文件** @param inputDoc* 要打开的文件,全路径* @return Dispatch* 打开的文件*/public Dispatch openDocument(String inputDoc) {logger.debug("打开Word文档...");this.document = Dispatch.call(documents,"Open",inputDoc).toDispatch();this.getSelection();this.getRange();this.getAlignment();this.getFont();this.getPageSetup();return this.document;}/*** 创建新的文件** @return Dispache 返回新建文件*/public Dispatch createNewDocument(){logger.debug("创建新的文件...");this.document = Dispatch.call(documents,"Add").toDispatch(); this.getSelection();this.getRange();this.getPageSetup();this.getAlignment();this.getFont();return this.document;}/*** 选定内容* @return Dispatch 选定的范围或插入点*/public Dispatch getSelection() {logger.debug("获取选定范围的插入点...");this.selection = word.getProperty("Selection").toDispatch();return this.selection;}/*** 获取当前Document内可以修改的部分<p><br>* 前提条件:选定内容必须存在** @param selectedContent 选定区域* @return 可修改的对象*/public Dispatch getRange() {logger.debug("获取当前Document内可以修改的部分...");this.range = Dispatch.get(this.selection, "Range").toDispatch();return this.range;}/*** 获得当前文档的文档页面属性*/public Dispatch getPageSetup() {logger.debug("获得当前文档的文档页面属性...");if(this.document == null){logger.warn("document对象为空...");return this.pageSetup;}this.pageSetup = Dispatch.get(this.document, "PageSetup").toDispatch(); return this.pageSetup;}/*** 把选定内容或插入点向上移动* @param count 移动的距离*/public void moveUp(int count) {logger.debug("把选定内容或插入点向上移动...");for(int i = 0;i < count;i++) {Dispatch.call(this.selection,"MoveUp");}/*** 把选定内容或插入点向下移动* @param count 移动的距离*/public void moveDown(int count) {logger.debug("把选定内容或插入点向下移动..."); for(int i = 0;i < count;i++) {Dispatch.call(this.selection,"MoveDown");}}/*** 把选定内容或插入点向左移动* @param count 移动的距离*/public void moveLeft(int count) {logger.debug("把选定内容或插入点向左移动..."); for(int i = 0;i < count;i++) {Dispatch.call(this.selection,"MoveLeft");}}/*** 把选定内容或插入点向右移动* @param count 移动的距离*/public void moveRight(int count) {logger.debug("把选定内容或插入点向右移动..."); for(int i = 0;i < count;i++) {Dispatch.call(this.selection,"MoveRight");}/*** 回车键*/public void enterDown(int count){logger.debug("按回车键...");for(int i = 0;i < count;i++) {Dispatch.call(this.selection, "TypeParagraph");}}/*** 把插入点移动到文件首位置*/public void moveStart() {logger.debug("把插入点移动到文件首位置...");Dispatch.call(this.selection,"HomeKey",new Variant(6));}/*** 从选定内容或插入点开始查找文本* @param selection 选定内容* @param toFindText 要查找的文本* @return boolean true-查找到并选中该文本,false-未查找到文本*/public boolean find(String toFindText) {logger.debug("从选定内容或插入点开始查找文本"+" 要查找内容: "+toFindText); /* 从selection所在位置开始查询 */Dispatch find = Dispatch.call(this.selection,"Find").toDispatch();/* 设置要查找的内容 */Dispatch.put(find,"Text",toFindText);/* 向前查找 */Dispatch.put(find,"Forward","True");/* 设置格式 */Dispatch.put(find,"Format","True");/* 大小写匹配 */Dispatch.put(find,"MatchCase","True");/* 全字匹配 */Dispatch.put(find,"MatchWholeWord","True");/* 查找并选中 */return Dispatch.call(find,"Execute").getBoolean();}/*** 把选定内容替换为设定文本* @param selection 选定内容* @param newText 替换为文本*/public void replace(String newText) {logger.debug("把选定内容替换为设定文本...");/* 设置替换文本 */Dispatch.put(this.selection,"Text",newText);}/*** 全局替换* @param selection 选定内容或起始插入点* @param oldText 要替换的文本* @param replaceObj 替换为文本*/public void replaceAll(String oldText,Object replaceObj) {logger.debug("全局替换...");/* 移动到文件开头 */moveStart();/* 表格替换方式 */String newText = (String) replaceObj;/* 图片替换方式 */if(oldText.indexOf("image") != -1 || stIndexOf(".bmp") != -1 ||stIndexOf(".jpg") != -1 || stIndexOf(".gif") != -1){while (find(oldText)) {insertImage(newText);Dispatch.call(this.selection,"MoveRight");}/* 正常替换方式 */} else {while (find(oldText)) {replace(newText);Dispatch.call(this.selection,"MoveRight");}}}/*** 插入图片* @param selection 图片的插入点* @param imagePath 图片文件(全路径)*/public void insertImage(String imagePath) {logger.debug("插入图片...");Dispatch.call(this.selection, "TypeParagraph");Dispatch.call(Dispatch.get(this.selection,"InLineShapes").toDispatch(),"AddPicture",i magePath);}/*** 合并表格** @param selection 操作对象* @param tableIndex 表格起始点* @param fstCellRowIdx 开始行* @param fstCellColIdx 开始列* @param secCellRowIdx 结束行* @param secCellColIdx 结束列*/public void mergeCell(int tableIndex, int fstCellRowIdx, int fstCellColIdx, int secCellRowIdx, int secCellColIdx){logger.debug("合并单元格...");if(this.table == null){logger.warn("table对象为空...");return;}Dispatch fstCell = Dispatch.call(table, "Cell",new Variant(fstCellRowIdx), new Variant(fstCellColIdx)).toDispatch();Dispatch secCell = Dispatch.call(table, "Cell",new Variant(secCellRowIdx), new Variant(secCellColIdx)).toDispatch();Dispatch.call(fstCell, "Merge", secCell);}/*** 想Table对象中插入数值<p>* 参数形式:ArrayList<String[]>List.size()为表格的总行数<br>* String[]的length属性值应该与所创建的表格列数相同** @param selection 插入点* @param tableIndex 表格起始点* @param list 数据内容*/public void insertToTable(List<String[]> list){System.out.println("向Table对象中插入数据...");logger.debug("向Table对象中插入数据...");if(list == null || list.size() <= 0){logger.warn("写出数据集为空...");return;}if(this.table == null){logger.warn("table对象为空...");return;}for(int i = 0; i < list.size(); i++){String[] strs = list.get(i);for(int j = 0; j<strs.length; j++){/* 遍历表格中每一个单元格,遍历次数与所要填入的内容数量相同 */ Dispatch cell = this.getCell(i+1, j+1);/* 选中此单元格 */Dispatch.call(cell, "Select");/* 写出内容到此单元格中 */Dispatch.put(this.selection, "Text", strs[j]);/* 移动游标到下一个位置 */}this.moveDown(1);}this.enterDown(1);}/*** 在文档中正常插入文字内容** @param selection 插入点* @param list 数据内容*/public void insertToDocument(List<String> list){logger.debug("向Document对象中插入数据...");if(list == null || list.size() <= 0){logger.warn("写出数据集为空...");return;}if(this.document == null){logger.warn("document对象为空...");return;}for(String str : list){/* 写出至word中 */this.applyListTemplate(3, 2);Dispatch.put(this.selection, "Text", str);this.moveDown(1);this.enterDown(1);}}/*** 创建新的表格** @param selection 插入点* @param document 文档对象* @param rowCount 行数* @param colCount 列数* @param width 边框数值 0浅色1深色* @return 新创建的表格对象*/public Dispatch createNewTable(int rowCount, int colCount, int width){logger.debug("创建新的表格...");if(this.tables == null){this.getTables();}this.getRange();if(rowCount > 0 && colCount > 0){this.table = Dispatch.call(this.tables,"Add",this.range,new Variant(rowCount),new Variant(colCount),new Variant(width)).toDispatch();}/* 返回新创建表格 */return this.table;}/*** 获取Document对象中的所有Table对象** @return 所有Table对象*/public Dispatch getTables(){logger.debug("获取所有表格对象...");if(this.document == null){logger.warn("document对象为空...");return this.tables;}this.tables = Dispatch.get(this.document, "Tables").toDispatch(); return this.tables;}/*** 获取Document中Table的数量** @return 表格数量*/public int getTablesCount(){logger.debug("获取文档中表格数量...");if(this.tables == null){this.getTables();}return Dispatch.get(tables, "Count").getInt();}/*** 获取指定序号的Table对象** @param tableIndex Table序列* @return*/public Dispatch getTable(int tableIndex){logger.debug("获取指定表格对象...");if(this.tables == null){this.getTables();}if(tableIndex >= 0){this.table = Dispatch.call(this.tables, "Item", new Variant(tableIndex)).toDispatch();}return this.table;}/*** 获取表格的总列数** @return 总列数*/public int getTableColumnsCount() {logger.debug("获取表格总行数...");if(this.table == null){logger.warn("table对象为空...");return 0;}return Dispatch.get(this.cols,"Count").getInt(); }/*** 获取表格的总行数** @return 总行数*/public int getTableRowsCount(){logger.debug("获取表格总行数...");if(this.table == null){logger.warn("table对象为空...");return 0;}return Dispatch.get(this.rows,"Count").getInt();}/*** 获取表格列对象** @return 列对象*/public Dispatch getTableColumns() {logger.debug("获取表格行对象...");if(this.table == null){logger.warn("table对象为空...");return this.cols;}this.cols = Dispatch.get(this.table,"Columns").toDispatch(); return this.cols;}/*** 获取表格的行对象** @return 总行数*/public Dispatch getTableRows(){logger.debug("获取表格总行数...");if(this.table == null){logger.warn("table对象为空...");return this.rows;}this.rows = Dispatch.get(this.table,"Rows").toDispatch();}/*** 获取指定表格列对象** @return 列对象*/public Dispatch getTableColumn(int columnIndex) {logger.debug("获取指定表格行对象...");if(this.cols == null){this.getTableColumns();}if(columnIndex >= 0){this.col = Dispatch.call(this.cols, "Item", newVariant(columnIndex)).toDispatch();}return this.col;}/*** 获取表格中指定的行对象** @param rowIndex 行序号* @return 行对象*/public Dispatch getTableRow(int rowIndex){logger.debug("获取指定表格总行数...");if(this.rows == null){this.getTableRows();}if(rowIndex >= 0){this.row = Dispatch.call(this.rows, "Item", new Variant(rowIndex)).toDispatch(); }}/*** 自动调整表格*/public void autoFitTable() {logger.debug("自动调整表格...");int count = this.getTablesCount();for (int i = 0; i < count; i++) {Dispatch table = Dispatch.call(tables, "Item", new Variant(i + 1)).toDispatch(); Dispatch cols = Dispatch.get(table, "Columns").toDispatch();Dispatch.call(cols, "AutoFit");}}/*** 获取当前文档中,表格中的指定单元格** @param CellRowIdx 单元格所在行* @param CellColIdx 单元格所在列* @return 指定单元格对象*/public Dispatch getCell(int cellRowIdx, int cellColIdx) {logger.debug("获取当前文档中,表格中的指定单元格...");if(this.table == null){logger.warn("table对象为空...");return this.cell;}if(cellRowIdx >= 0 && cellColIdx >=0){this.cell = Dispatch.call(this.table, "Cell", new Variant(cellRowIdx),newVariant(cellColIdx)).toDispatch();}}/*** 设置文档标题** @param title 标题内容*/public void setTitle(String title){logger.debug("设置文档标题...");if(title == null || "".equals(title)){logger.warn("文档标题为空...");return;}Dispatch.call(this.selection, "TypeText", title);}/*** 设置当前表格线的粗细** @param width* width范围:1<w<13,如果是0,就代表没有框*/public void setTableBorderWidth(int width) {logger.debug("设置当前表格线的粗细...");if(this.table == null){logger.warn("table对象为空...");return;}/** 设置表格线的粗细 1:代表最上边一条线 2:代表最左边一条线 3:最下边一条线4:最右边一条线 5:除最上边最下边之外的所有横线* 6:除最左边最右边之外的所有竖线 7:从左上角到右下角的斜线 8:从左下角到右上角的斜线*/Dispatch borders = Dispatch.get(table, "Borders").toDispatch();Dispatch border = null;for (int i = 1; i < 7; i++) {border = Dispatch.call(borders, "Item", new Variant(i)).toDispatch();if (width != 0) {Dispatch.put(border, "LineWidth", new Variant(width));Dispatch.put(border, "Visible", new Variant(true));} else if (width == 0) {Dispatch.put(border, "Visible", new Variant(false));}}}/*** 对当前selection设置项目符号和编号* @param tabIndex* 1: 项目编号* 2: 编号* 3: 多级编号* 4: 列表样式* @param index* 0:表示没有 ,其它数字代表的是该Tab页中的第几项内容*/public void applyListTemplate(int tabIndex,int index){logger.debug("对当前selection设置项目符号和编号...");/* 取得ListGalleries对象列表 */Dispatch listGalleries = Dispatch.get(this.word, "ListGalleries").toDispatch();/* 取得列表中一个对象 */Dispatch listGallery = Dispatch.call(listGalleries, "Item", newVariant(tabIndex)).toDispatch();Dispatch listTemplates = Dispatch.get(listGallery, "ListTemplates").toDispatch(); if(this.range == null){this.getRange();}Dispatch listFormat = Dispatch.get(this.range, "ListFormat").toDispatch();Dispatch.call(listFormat,"ApplyListTemplate",Dispatch.call(listTemplates, "Item", new Variant(index)), new Variant(true),new Variant(1),new Variant(0));}/*** 增加文档目录** 目前采用固定参数方式,以后可以动态进行调整*/public void addTablesOfContents(){/* 取得ActiveDocument、TablesOfContents、range对象 */Dispatch ActiveDocument = word.getProperty("ActiveDocument").toDispatch(); Dispatch TablesOfContents =Dispatch.get(ActiveDocument,"TablesOfContents").toDispatch();Dispatch range = Dispatch.get(this.selection, "Range").toDispatch();/* 增加目录 */Dispatch.call(TablesOfContents,"Add",range,new Variant(true),new Variant(1),new Variant(3),new Variant(true),new Variant(""),new Variant(true),new Variant(true));}/*** 设置当前Selection 位置方式* @param selectedContent 0-居左;1-居中;2-居右。