Java编程实现调用com操作Word方法实例代码
程序员文章站
2024-02-29 13:40:34
实例代码如下:
import com.jacob.activex.activexcomponent;
import com.jacob.com.dispatc...
实例代码如下:
import com.jacob.activex.activexcomponent; import com.jacob.com.dispatch; import com.jacob.com.variant; /** * jacob操作msword类 * @author */ public class wordbean { // word文档 private dispatch doc; // word运行程序对象 private activexcomponent word; // 所有word文档集合 private dispatch documents; // 选定的范围或插入点 private dispatch selection; private boolean saveonexit = true; public wordbean()throws exception{ if (word == null) { word = new activexcomponent("word.application"); word.setproperty("visible", new variant(false)); //不可见打开word word.setproperty("automationsecurity", new variant(3)); //禁用宏 } if (documents == null) documents = word.getproperty("documents").todispatch(); } /** * 设置退出时参数 * * @param saveonexit * boolean true-退出时保存文件,false-退出时不保存文件 */ public void setsaveonexit(boolean saveonexit) { this.saveonexit = saveonexit; } /** * 创建一个新的word文档 * */ public void createnewdocument() { doc = dispatch.call(documents, "add").todispatch(); selection = dispatch.get(word, "selection").todispatch(); } /** * 打开一个已存在的文档 * * @param docpath */ public void opendocument(string docpath) { closedocument(); doc = dispatch.call(documents, "open", docpath).todispatch(); selection = dispatch.get(word, "selection").todispatch(); } /** * 打开一个保护文档, * @param docpath-文件全名 * @param pwd-密码 */ public void opendocumentonlyread(string docpath, string pwd)throws exception { closedocument(); // doc = dispatch.invoke(documents, "open", dispatch.method, // new object[]{docpath, new variant(false), new variant(true), new variant(true), pwd}, // new int[1]).todispatch();//打开word文件 doc = dispatch.calln(documents, "open", new object[]{docpath, new variant(false), new variant(true), new variant(true), pwd, "", new variant(false)}).todispatch(); selection = dispatch.get(word, "selection").todispatch(); } public void opendocument(string docpath, string pwd)throws exception { closedocument(); doc = dispatch.calln(documents, "open", new object[]{docpath, new variant(false), new variant(false), new variant(true), pwd}).todispatch(); selection = dispatch.get(word, "selection").todispatch(); } /** * 把选定的内容或插入点向上移动 * * @param pos * 移动的距离 */ public void moveup(int pos) { if (selection == null) selection = dispatch.get(word, "selection").todispatch(); for (int i = 0; i < pos; i++) dispatch.call(selection, "moveup"); } /** * 把选定的内容或者插入点向下移动 * * @param pos * 移动的距离 */ public void movedown(int pos) { if (selection == null) selection = dispatch.get(word, "selection").todispatch(); for (int i = 0; i < pos; i++) dispatch.call(selection, "movedown"); } /** * 把选定的内容或者插入点向左移动 * * @param pos * 移动的距离 */ public void moveleft(int pos) { if (selection == null) selection = dispatch.get(word, "selection").todispatch(); for (int i = 0; i < pos; i++) { dispatch.call(selection, "moveleft"); } } /** * 把选定的内容或者插入点向右移动 * * @param pos * 移动的距离 */ public void moveright(int pos) { if (selection == null) selection = dispatch.get(word, "selection").todispatch(); for (int i = 0; i < pos; i++) dispatch.call(selection, "moveright"); } /** * 把插入点移动到文件首位置 * */ public void movestart() { if (selection == null) selection = dispatch.get(word, "selection").todispatch(); dispatch.call(selection, "homekey", new variant(6)); } /** * 从选定内容或插入点开始查找文本 * * @param tofindtext * 要查找的文本 * @return boolean true-查找到并选中该文本,false-未查找到文本 */ @suppresswarnings("static-access") public boolean find(string tofindtext) { if (tofindtext == null || tofindtext.equals("")) return false; // 从selection所在位置开始查询 dispatch find = word.call(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 tofindtext * 查找字符串 * @param newtext * 要替换的内容 * @return */ public boolean replacetext(string tofindtext, string newtext) { if (!find(tofindtext)) return false; dispatch.put(selection, "text", newtext); return true; } /** * 全局替换文本 * * @param tofindtext * 查找字符串 * @param newtext * 要替换的内容 */ public void replacealltext(string tofindtext, string newtext) { while (find(tofindtext)) { dispatch.put(selection, "text", newtext); dispatch.call(selection, "moveright"); } } /** * 在当前插入点插入字符串 * * @param newtext * 要插入的新字符串 */ public void inserttext(string newtext) { dispatch.put(selection, "text", newtext); } /** * * @param tofindtext * 要查找的字符串 * @param imagepath * 图片路径 * @return */ public boolean replaceimage(string tofindtext, string imagepath) { if (!find(tofindtext)) return false; dispatch.call(dispatch.get(selection, "inlineshapes").todispatch(), "addpicture", imagepath); return true; } /** * 全局替换图片 * * @param tofindtext * 查找字符串 * @param imagepath * 图片路径 */ public void replaceallimage(string tofindtext, string imagepath) { while (find(tofindtext)) { dispatch.call(dispatch.get(selection, "inlineshapes").todispatch(), "addpicture", imagepath); dispatch.call(selection, "moveright"); } } /** * 在当前插入点插入图片 * * @param imagepath * 图片路径 */ public void insertimage(string imagepath) { dispatch.call(dispatch.get(selection, "inlineshapes").todispatch(), "addpicture", imagepath); } /** * 合并单元格 * * @param tableindex * @param fstcellrowidx * @param fstcellcolidx * @param seccellrowidx * @param seccellcolidx */ public void mergecell(int tableindex, int fstcellrowidx, int fstcellcolidx, int seccellrowidx, int seccellcolidx) { // 所有表格 dispatch tables = dispatch.get(doc, "tables").todispatch(); // 要填充的表格 dispatch table = dispatch.call(tables, "item", new variant(tableindex)) .todispatch(); 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); } /** * 在指定的单元格里填写数据 * * @param tableindex * @param cellrowidx * @param cellcolidx * @param txt */ public void puttxttocell(int tableindex, int cellrowidx, int cellcolidx, string txt) { // 所有表格 dispatch tables = dispatch.get(doc, "tables").todispatch(); // 要填充的表格 dispatch table = dispatch.call(tables, "item", new variant(tableindex)) .todispatch(); dispatch cell = dispatch.call(table, "cell", new variant(cellrowidx), new variant(cellcolidx)).todispatch(); dispatch.call(cell, "select"); dispatch.put(selection, "text", txt); } /** * 获得指定的单元格里数据 * * @param tableindex * @param cellrowidx * @param cellcolidx * @return */ public string gettxtfromcell(int tableindex, int cellrowidx, int cellcolidx) { // 所有表格 dispatch tables = dispatch.get(doc, "tables").todispatch(); // 要填充的表格 dispatch table = dispatch.call(tables, "item", new variant(tableindex)) .todispatch(); dispatch cell = dispatch.call(table, "cell", new variant(cellrowidx), new variant(cellcolidx)).todispatch(); dispatch.call(cell, "select"); string ret = ""; ret = dispatch.get(selection, "text").tostring(); ret = ret.substring(0, ret.length()-1); //去掉最后的回车符; return ret; } /** * 在当前文档拷贝剪贴板数据 * @param pos */ public void pasteexcelsheet(string pos) { movestart(); if (this.find(pos)) { dispatch textrange = dispatch.get(selection, "range").todispatch(); dispatch.call(textrange, "paste"); } } /** * 在当前文档指定的位置拷贝表格 * * @param pos * 当前文档指定的位置 * @param tableindex * 被拷贝的表格在word文档中所处的位置 */ public void copytable(string pos, int tableindex) { // 所有表格 dispatch tables = dispatch.get(doc, "tables").todispatch(); // 要填充的表格 dispatch table = dispatch.call(tables, "item", new variant(tableindex)) .todispatch(); dispatch range = dispatch.get(table, "range").todispatch(); dispatch.call(range, "copy"); if (this.find(pos)) { dispatch textrange = dispatch.get(selection, "range").todispatch(); dispatch.call(textrange, "paste"); } } /** * 在当前文档指定的位置拷贝来自另一个文档中的表格 * * @param anotherdocpath * 另一个文档的磁盘路径 * @param tableindex * 被拷贝的表格在另一格文档中的位置 * @param pos * 当前文档指定的位置 */ public void copytablefromanotherdoc(string anotherdocpath, int tableindex, string pos) { dispatch doc2 = null; try { doc2 = dispatch.call(documents, "open", anotherdocpath) .todispatch(); // 所有表格 dispatch tables = dispatch.get(doc2, "tables").todispatch(); // 要填充的表格 dispatch table = dispatch.call(tables, "item", new variant(tableindex)).todispatch(); dispatch range = dispatch.get(table, "range").todispatch(); dispatch.call(range, "copy"); if (this.find(pos)) { dispatch textrange = dispatch.get(selection, "range") .todispatch(); dispatch.call(textrange, "paste"); } } catch (exception e) { e.printstacktrace(); } finally { if (doc2 != null) { dispatch.call(doc2, "close", new variant(saveonexit)); doc2 = null; } } } /** * 在当前文档指定的位置拷贝来自另一个文档中的图片 * * @param anotherdocpath 另一个文档的磁盘路径 * @param shapeindex 被拷贝的图片在另一格文档中的位置 * @param pos 当前文档指定的位置 */ public void copyimagefromanotherdoc(string anotherdocpath, int shapeindex, string pos) { dispatch doc2 = null; try { doc2 = dispatch.call(documents, "open", anotherdocpath) .todispatch(); dispatch shapes = dispatch.get(doc2, "inlineshapes").todispatch(); dispatch shape = dispatch.call(shapes, "item", new variant(shapeindex)).todispatch(); dispatch imagerange = dispatch.get(shape, "range").todispatch(); dispatch.call(imagerange, "copy"); if (this.find(pos)) { dispatch textrange = dispatch.get(selection, "range") .todispatch(); dispatch.call(textrange, "paste"); } } catch (exception e) { e.printstacktrace(); } finally { if (doc2 != null) { dispatch.call(doc2, "close", new variant(saveonexit)); doc2 = null; } } } /** * 创建表格 * * @param pos * 位置 * @param cols * 列数 * @param rows * 行数 */ public void createtable(string pos, int numcols, int numrows) { if (find(pos)) { dispatch tables = dispatch.get(doc, "tables").todispatch(); dispatch range = dispatch.get(selection, "range").todispatch(); @suppresswarnings("unused") dispatch newtable = dispatch.call(tables, "add", range, new variant(numrows), new variant(numcols)).todispatch(); dispatch.call(selection, "moveright"); } else { dispatch tables = dispatch.get(doc, "tables").todispatch(); dispatch range = dispatch.get(selection, "range").todispatch(); @suppresswarnings("unused") dispatch newtable = dispatch.call(tables, "add", range, new variant(numrows), new variant(numcols)).todispatch(); dispatch.call(selection, "moveright"); } } /** * 在指定行前面增加行 * * @param tableindex * word文件中的第n张表(从1开始) * @param rowindex * 指定行的序号(从1开始) */ public void addtablerow(int tableindex, int rowindex) { // 所有表格 dispatch tables = dispatch.get(doc, "tables").todispatch(); // 要填充的表格 dispatch table = dispatch.call(tables, "item", new variant(tableindex)) .todispatch(); // 表格的所有行 dispatch rows = dispatch.get(table, "rows").todispatch(); dispatch row = dispatch.call(rows, "item", new variant(rowindex)) .todispatch(); dispatch.call(rows, "add", new variant(row)); } /** * 在第1行前增加一行 * * @param tableindex * word文档中的第n张表(从1开始) */ public void addfirsttablerow(int tableindex) { // 所有表格 dispatch tables = dispatch.get(doc, "tables").todispatch(); // 要填充的表格 dispatch table = dispatch.call(tables, "item", new variant(tableindex)) .todispatch(); // 表格的所有行 dispatch rows = dispatch.get(table, "rows").todispatch(); dispatch row = dispatch.get(rows, "first").todispatch(); dispatch.call(rows, "add", new variant(row)); } /** * 在最后1行前增加一行 * * @param tableindex * word文档中的第n张表(从1开始) */ public void addlasttablerow(int tableindex) { // 所有表格 dispatch tables = dispatch.get(doc, "tables").todispatch(); // 要填充的表格 dispatch table = dispatch.call(tables, "item", new variant(tableindex)) .todispatch(); // 表格的所有行 dispatch rows = dispatch.get(table, "rows").todispatch(); dispatch row = dispatch.get(rows, "last").todispatch(); dispatch.call(rows, "add", new variant(row)); } /** * 增加一行 * * @param tableindex * word文档中的第n张表(从1开始) */ public void addrow(int tableindex) { dispatch tables = dispatch.get(doc, "tables").todispatch(); // 要填充的表格 dispatch table = dispatch.call(tables, "item", new variant(tableindex)) .todispatch(); // 表格的所有行 dispatch rows = dispatch.get(table, "rows").todispatch(); dispatch.call(rows, "add"); } /** * 增加一列 * * @param tableindex * word文档中的第n张表(从1开始) */ public void addcol(int tableindex) { // 所有表格 dispatch tables = dispatch.get(doc, "tables").todispatch(); // 要填充的表格 dispatch table = dispatch.call(tables, "item", new variant(tableindex)) .todispatch(); // 表格的所有行 dispatch cols = dispatch.get(table, "columns").todispatch(); dispatch.call(cols, "add").todispatch(); dispatch.call(cols, "autofit"); } /** * 在指定列前面增加表格的列 * * @param tableindex * word文档中的第n张表(从1开始) * @param colindex * 制定列的序号 (从1开始) */ public void addtablecol(int tableindex, int colindex) { // 所有表格 dispatch tables = dispatch.get(doc, "tables").todispatch(); // 要填充的表格 dispatch table = dispatch.call(tables, "item", new variant(tableindex)) .todispatch(); // 表格的所有行 dispatch cols = dispatch.get(table, "columns").todispatch(); system.out.println(dispatch.get(cols, "count")); dispatch col = dispatch.call(cols, "item", new variant(colindex)) .todispatch(); // dispatch col = dispatch.get(cols, "first").todispatch(); dispatch.call(cols, "add", col).todispatch(); dispatch.call(cols, "autofit"); } /** * 在第1列前增加一列 * * @param tableindex * word文档中的第n张表(从1开始) */ public void addfirsttablecol(int tableindex) { dispatch tables = dispatch.get(doc, "tables").todispatch(); // 要填充的表格 dispatch table = dispatch.call(tables, "item", new variant(tableindex)) .todispatch(); // 表格的所有行 dispatch cols = dispatch.get(table, "columns").todispatch(); dispatch col = dispatch.get(cols, "first").todispatch(); dispatch.call(cols, "add", col).todispatch(); dispatch.call(cols, "autofit"); } /** * 在最后一列前增加一列 * * @param tableindex * word文档中的第n张表(从1开始) */ public void addlasttablecol(int tableindex) { dispatch tables = dispatch.get(doc, "tables").todispatch(); // 要填充的表格 dispatch table = dispatch.call(tables, "item", new variant(tableindex)) .todispatch(); // 表格的所有行 dispatch cols = dispatch.get(table, "columns").todispatch(); dispatch col = dispatch.get(cols, "last").todispatch(); dispatch.call(cols, "add", col).todispatch(); dispatch.call(cols, "autofit"); } /** * 自动调整表格 * */ @suppresswarnings("deprecation") public void autofittable() { dispatch tables = dispatch.get(doc, "tables").todispatch(); int count = dispatch.get(tables, "count").toint(); 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"); } } /** * 调用word里的宏以调整表格的宽度,其中宏保存在document下 * */ @suppresswarnings("deprecation") public void callwordmacro() { dispatch tables = dispatch.get(doc, "tables").todispatch(); int count = dispatch.get(tables, "count").toint(); variant vmacroname = new variant("normal.newmacros.tablefit"); @suppresswarnings("unused") variant vparam = new variant("param1"); @suppresswarnings("unused") variant para[] = new variant[] { vmacroname }; for (int i = 0; i < count; i++) { dispatch table = dispatch.call(tables, "item", new variant(i + 1)) .todispatch(); dispatch.call(table, "select"); dispatch.call(word, "run", "tablefitcontent"); } } /** * 设置当前选定内容的字体 * * @param boldsize * @param italicsize * @param underlinesize * 下划线 * @param colorsize * 字体颜色 * @param size * 字体大小 * @param name * 字体名称 */ public void setfont(boolean bold, boolean italic, boolean underline, string colorsize, string size, string name) { dispatch font = dispatch.get(selection, "font").todispatch(); dispatch.put(font, "name", new variant(name)); dispatch.put(font, "bold", new variant(bold)); dispatch.put(font, "italic", new variant(italic)); dispatch.put(font, "underline", new variant(underline)); dispatch.put(font, "color", colorsize); dispatch.put(font, "size", size); } /** * 设置单元格被选中 * * @param tableindex * @param cellrowidx * @param cellcolidx */ public void settablecellselected(int tableindex, int cellrowidx, int cellcolidx){ dispatch tables = dispatch.get(doc, "tables").todispatch(); dispatch table = dispatch.call(tables, "item", new variant(tableindex)) .todispatch(); dispatch cell = dispatch.call(table, "cell", new variant(cellrowidx), new variant(cellcolidx)).todispatch(); dispatch.call(cell, "select"); } /** * 设置选定单元格的垂直对起方式, 请使用settablecellselected选中一个单元格 * @param align 0-顶端, 1-居中, 3-底端 */ public void setcellverticalalign(int verticalalign){ dispatch cells = dispatch.get(selection, "cells").todispatch(); dispatch.put(cells, "verticalalignment", new variant(verticalalign)); } /** * 设置当前文档中所有表格水平居中方式及其它一些格式,用在将word文件转化为html中,针对申报表 */ @suppresswarnings("deprecation") public void setapplytableformat(){ dispatch tables = dispatch.get(doc, "tables").todispatch(); int tabcount = integer.valueof(dispatch.get(tables, "count").tostring()); //system.out.println(tabcount); system.out.println("*******************************************************"); for(int i=1; i<=tabcount; i++){ dispatch table = dispatch.call(tables, "item", new variant(i)).todispatch(); dispatch rows = dispatch.get(table, "rows").todispatch(); if(i==1){ dispatch.put(rows, "alignment", new variant(2)); //1-居中,2-right continue ; } dispatch.put(rows, "alignment", new variant(1)); //1-居中 dispatch.call(table, "autofitbehavior", new variant(1));//设置自动调整表格方式,1-根据窗口自动调整 dispatch.put(table, "preferredwidthtype", new variant(1)); dispatch.put(table, "preferredwidth", new variant(700)); system.out.println(dispatch.get(rows, "heightrule").tostring()); dispatch.put(rows, "heightrule", new variant(1)); //0-自动wdrowheightauto,1-最小值wdrowheightatleast, 2-固定wdrowheightexactly dispatch.put(rows, "height", new variant(0.04*28.35)); //int oldalign = integer.valueof(dispatch.get(rows, "alignment").tostring()); //system.out.println("algin:" + oldalign); } } /** * 设置段落格式 * * @param alignment * 0-左对齐, 1-右对齐, 2-右对齐, 3-两端对齐, 4-分散对齐 * @param linespaceingrule * @param lineunitbefore * @param lineunitafter * @param characterunitfirstlineindent */ public void setparagraphsproperties(int alignment, int linespaceingrule, int lineunitbefore, int lineunitafter, int characterunitfirstlineindent){ dispatch paragraphs = dispatch.get(selection, "paragraphs").todispatch(); dispatch.put(paragraphs, "alignment", new variant(alignment)); //对齐方式 dispatch.put(paragraphs, "linespacingrule", new variant(linespaceingrule)); //行距 dispatch.put(paragraphs, "lineunitbefore", new variant(lineunitbefore)); //段前 dispatch.put(paragraphs, "lineunitafter", new variant(lineunitafter)); //段后 dispatch.put(paragraphs, "characterunitfirstlineindent", new variant(characterunitfirstlineindent)); //首行缩进字符数 } /** * 设置当前段落格式, 使用前,请先选中段落 */ public void getparagraphsproperties(){ dispatch paragraphs = dispatch.get(selection, "paragraphs").todispatch(); string val = dispatch.get(paragraphs, "linespacingrule").tostring(); //行距 val = dispatch.get(paragraphs, "alignment").tostring(); //对齐方式 val = dispatch.get(paragraphs, "lineunitbefore").tostring(); //段前行数 val = dispatch.get(paragraphs, "lineunitafter").tostring(); //段后行数 val = dispatch.get(paragraphs, "firstlineindent").tostring(); //首行缩进 val = dispatch.get(paragraphs, "characterunitfirstlineindent").tostring(); //首行缩进字符数 } /** * 文件保存或另存为 * * @param savepath * 保存或另存为路径 */ public void save(string savepath) { dispatch.call(dispatch.call(word, "wordbasic").getdispatch(), "filesaveas", savepath); } /** * 文件保存为html格式 * * @param savepath * @param htmlpath */ public void saveashtml(string htmlpath){ dispatch.invoke(doc,"saveas", dispatch.method, new object[]{htmlpath, new variant(8)}, new int[1]); } /** * 关闭文档 *@param val 0不保存修改 -1 保存修改 -2 提示是否保存修改 */ public void closedocument(int val) { dispatch.call(doc, "close", new variant(val)); doc = null; } /** * 关闭当前word文档 * */ public void closedocument() { if (doc != null) { dispatch.call(doc, "save"); dispatch.call(doc, "close", new variant(saveonexit)); doc = null; } } public void closedocumentwithoutsave(){ if (doc != null) { dispatch.call(doc, "close", new variant(false)); doc = null; } } /** * 关闭全部应用 * */ public void close() { //closedocument(); if (word != null) { dispatch.call(word, "quit"); word = null; } selection = null; documents = null; } /** * 打印当前word文档 * */ public void printfile() { if (doc != null) { dispatch.call(doc, "printout"); } } /** * 保护当前档,如果不存在, 使用expression.protect(type, noreset, password) * * @param pwd * wdprotectiontype 可以是下列 wdprotectiontype 常量之一: * 1-wdallowonlycomments, 2-wdallowonlyformfields, 0-wdallowonlyrevisions, * -1-wdnoprotection, 3-wdallowonlyreading * * 使用参照 main1() */ public void protectedword(string pwd){ string protectiontype = dispatch.get(doc, "protectiontype").tostring(); if(protectiontype.equals("-1")){ dispatch.call(doc, "protect", new variant(3), new variant(true), pwd); } } /** * 解除文档保护,如果存在 * @param pwd * wdprotectiontype 常量之一(long 类型,只读): * 1-wdallowonlycomments,2-wdallowonlyformfields、 * 0-wdallowonlyrevisions,-1-wdnoprotection, 3-wdallowonlyreading * * 使用参照 main1() */ public void unprotectedword(string pwd){ string protectiontype = dispatch.get(doc, "protectiontype").tostring(); if(protectiontype.equals("3")){ dispatch.call(doc, "unprotect", pwd); } } /** * 设置word文档安全级别 * @param value * 1-msoautomationsecuritybyui 使用“安全”对话框指定的安全设置。 * 2-msoautomationsecurityforcedisable 在程序打开的所有文件中禁用所有宏,而不显示任何安全提醒。 * 3-msoautomationsecuritylow 启用所有宏,这是启动应用程序时的默认值。 */ public void setautomationsecurity(int value){ word.setproperty("automationsecurity", new variant(value)); } /** * 读取文档中第paragraphsindex段文字的内容; * @param paragraphsindex * @return */ public string getparagraphs(int paragraphsindex){ string ret = ""; dispatch paragraphs = dispatch.get(doc, "paragraphs").todispatch(); // 所有段落 int paragraphcount = dispatch.get(paragraphs, "count").getint(); // 一共的段落数 dispatch paragraph = null; dispatch range = null; if(paragraphcount > paragraphsindex && 0 < paragraphsindex){ paragraph = dispatch.call(paragraphs, "item", new variant(paragraphsindex)).todispatch(); range = dispatch.get(paragraph, "range").todispatch(); ret = dispatch.get(range, "text").tostring(); } return ret; } /** * 设置页眉文字 * @param cont * @return * * sub addheadertext() * '设置页眉或页脚中的文字 * '由 headers、footers 和 headerfooter 属性返回 headerfooter 对象。下列示例更改当前页眉中的文字。 * with activedocument.activewindow.view * .seekview = wdseekcurrentpageheader * selection.headerfooter.range.text = "header text" * .seekview = wdseekmaindocument * end with * end sub */ public void setheadercontent(string cont){ dispatch activewindow = dispatch.get(doc, "activewindow").todispatch(); dispatch view = dispatch.get(activewindow, "view").todispatch(); //dispatch seekview = dispatch.get(view, "seekview").todispatch(); dispatch.put(view, "seekview", new variant(9)); //wdseekcurrentpageheader-9 dispatch headerfooter = dispatch.get(selection, "headerfooter").todispatch(); dispatch range = dispatch.get(headerfooter, "range").todispatch(); dispatch.put(range, "text", new variant(cont)); //string content = dispatch.get(range, "text").tostring(); dispatch font = dispatch.get(range, "font").todispatch(); dispatch.put(font, "name", new variant("楷体_gb2312")); dispatch.put(font, "bold", new variant(true)); //dispatch.put(font, "italic", new variant(true)); //dispatch.put(font, "underline", new variant(true)); dispatch.put(font, "size", 9); dispatch.put(view, "seekview", new variant(0)); //wdseekmaindocument-0恢复视图; } public static void main(string[] args)throws exception{ wordbean word = new wordbean(); word.opendocument("d:/竞价平台.doc"); word.setheadercontent("*****************88设置页眉内容11111111111111111!"); //word.unprotectedword("1qaz"); //word.protectedword("123"); system.out.print(word.getparagraphs(3)); word.closedocument(); word.close(); } }
//更新目录并自动保存办法
/**启动word进程*/ activexcomponent app = new activexcomponent("word.application"); app.setproperty("visible", new variant(false)); dispatch docs = app.getproperty("documents").todispatch(); /**打开word文档*/ dispatch doc = dispatch.invoke(docs, "open", dispatch.method, new object[] { "d:/aaa/a.doc", new variant(false), new variant(false) }, new int[1]).todispatch(); dispatch activedocument = app.getproperty("activedocument").todispatch(); /**获取目录*/ dispatch tablesofcontents = dispatch.get(activedocument,"tablesofcontents").todispatch(); /**获取第一个目录。若有多个目录,则传递对应的参数*/ variant tablesofcontent = dispatch.call(tablesofcontents, "item", new variant(1)); /**更新目录,有两个方法:update 更新域,updatepagenumbers 只更新页码*/ dispatch toc = tablesofcontent.todispatch(); toc.call(toc, "update"); /**另存为*/ /**关闭word文档*/ dispatch.call(doc, "save"); dispatch.call(doc, "close", new variant(-1)); /**退出word进程*/ app.invoke("quit", new variant[] {});
总结:
本文关于java编程实现调用com操作word方法的介绍就到这里,希望对大家有所帮助。如果有什么问题可以留言,小编会及时回复大家的。