关于jacob的使用疑惑

问题描述

各位大牛··小弟最近想操作java解析jacob,在网上找到很多解析代码,可是看不懂呀,哪个大牛可以和我说说啊·欢迎加我QQ447638426.现在特将一个代码写上··请帮忙说说那些方法的作用,急···

解决方案

解决方案二:
代码就是这样importjava.util.HashMap;importcom.jacob.activeX.ActiveXComponent;importcom.jacob.com.Dispatch;importcom.jacob.com.Variant;importjava.awt.Graphics;publicclassJacob3{//word文档privateDispatchdoc;//word运行程序对象privateActiveXComponentword;//所有word文档集合privateDispatchdocuments;//选定的范围或插入点privateDispatchselection;privatebooleansaveOnExit=true;publicJacob3(){if(word==null){word=newActiveXComponent("Word.Application");word.setProperty("Visible",newVariant(false));}if(documents==null)documents=word.getProperty("Documents").toDispatch();}/***设置退出时参数**@paramsaveOnExit*booleantrue-退出时保存文件,false-退出时不保存文件*/publicvoidsetSaveOnExit(booleansaveOnExit){this.saveOnExit=saveOnExit;}/***创建一个新的word文档**/publicvoidcreateNewDocument(){doc=Dispatch.call(documents,"Add").toDispatch();selection=Dispatch.get(word,"Selection").toDispatch();}/***打开一个已存在的文档**@paramdocPath*/publicvoidopenDocument(StringdocPath){closeDocument();doc=Dispatch.call(documents,"Open",docPath).toDispatch();selection=Dispatch.get(word,"Selection").toDispatch();}/***把选定的内容或插入点向上移动**@parampos*移动的距离*/publicvoidmoveUp(intpos){if(selection==null)selection=Dispatch.get(word,"Selection").toDispatch();for(inti=0;i<pos;i++)Dispatch.call(selection,"MoveUp");}/***把选定的内容或者插入点向下移动**@parampos*移动的距离*/publicvoidmoveDown(intpos){if(selection==null)selection=Dispatch.get(word,"Selection").toDispatch();for(inti=0;i<pos;i++)Dispatch.call(selection,"MoveDown");}/***把选定的内容或者插入点向左移动**@parampos*移动的距离*/publicvoidmoveLeft(intpos){if(selection==null)selection=Dispatch.get(word,"Selection").toDispatch();for(inti=0;i<pos;i++){Dispatch.call(selection,"MoveLeft");}}/***把选定的内容或者插入点向右移动**@parampos*移动的距离*/publicvoidmoveRight(intpos){if(selection==null)selection=Dispatch.get(word,"Selection").toDispatch();for(inti=0;i<pos;i++)Dispatch.call(selection,"MoveRight");}/***把插入点移动到文件首位置**/publicvoidmoveStart(){if(selection==null)selection=Dispatch.get(word,"Selection").toDispatch();Dispatch.call(selection,"HomeKey",newVariant(6));}/***从选定内容或插入点开始查找文本**@paramtoFindText*要查找的文本*@returnbooleantrue-查找到并选中该文本,false-未查找到文本*/publicbooleanfind(StringtoFindText){if(toFindText==null||toFindText.equals(""))returnfalse;//从selection所在位置开始查询Dispatchfind=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");//查找并选中returnDispatch.call(find,"Execute").getBoolean();}/***把选定选定内容设定为替换文本**@paramtoFindText*查找字符串*@paramnewText*要替换的内容*@return*/publicbooleanreplaceText(StringtoFindText,StringnewText){if(!find(toFindText))returnfalse;Dispatch.put(selection,"Text",newText);returntrue;}/***全局替换文本**@paramtoFindText*查找字符串*@paramnewText*要替换的内容*/publicvoidreplaceAllText(StringtoFindText,StringnewText){while(find(toFindText)){Dispatch.put(selection,"Text",newText);Dispatch.call(selection,"MoveRight");}}/***在当前插入点插入字符串**@paramnewText*要插入的新字符串*/publicvoidinsertText(StringnewText){Dispatch.put(selection,"Text",newText);}/****@paramtoFindText*要查找的字符串*@paramimagePath*图片路径*@return*/publicbooleanreplaceImage(StringtoFindText,StringimagePath){if(!find(toFindText))returnfalse;Dispatch.call(Dispatch.get(selection,"InLineShapes").toDispatch(),"AddPicture",imagePath);returntrue;}/***全局替换图片**@paramtoFindText*查找字符串*@paramimagePath*图片路径*/publicvoidreplaceAllImage(StringtoFindText,StringimagePath){while(find(toFindText)){Dispatch.call(Dispatch.get(selection,"InLineShapes").toDispatch(),"AddPicture",imagePath);Dispatch.call(selection,"MoveRight");}}/***在当前插入点插入图片**@paramimagePath*图片路径*/publicvoidinsertImage(StringimagePath){Dispatch.call(Dispatch.get(selection,"InLineShapes").toDispatch(),"AddPicture",imagePath);}/***合并单元格**@paramtableIndex*@paramfstCellRowIdx*@paramfstCellColIdx*@paramsecCellRowIdx*@paramsecCellColIdx*/publicvoidmergeCell(inttableIndex,intfstCellRowIdx,intfstCellColIdx,intsecCellRowIdx,intsecCellColIdx){//所有表格Dispatchtables=Dispatch.get(doc,"Tables").toDispatch();//要填充的表格Dispatchtable=Dispatch.call(tables,"Item",newVariant(tableIndex)).toDispatch();DispatchfstCell=Dispatch.call(table,"Cell",newVariant(fstCellRowIdx),newVariant(fstCellColIdx)).toDispatch();DispatchsecCell=Dispatch.call(table,"Cell",newVariant(secCellRowIdx),newVariant(secCellColIdx)).toDispatch();Dispatch.call(fstCell,"Merge",secCell);}
解决方案三:
/***在指定的单元格里填写数据**@paramtableIndex*@paramcellRowIdx*@paramcellColIdx*@paramtxt*/publicvoidputTxtToCell(inttableIndex,intcellRowIdx,intcellColIdx,Stringtxt){//所有表格Dispatchtables=Dispatch.get(doc,"Tables").toDispatch();//要填充的表格Dispatchtable=Dispatch.call(tables,"Item",newVariant(tableIndex)).toDispatch();Dispatchcell=Dispatch.call(table,"Cell",newVariant(cellRowIdx),newVariant(cellColIdx)).toDispatch();Dispatch.call(cell,"Select");Dispatch.put(selection,"Text",txt);}/***在当前文档拷贝剪贴板数据*@parampos*/publicvoidpasteExcelSheet(Stringpos){moveStart();if(this.find(pos)){DispatchtextRange=Dispatch.get(selection,"Range").toDispatch();Dispatch.call(textRange,"Paste");}}/***在当前文档指定的位置拷贝表格**@parampos*当前文档指定的位置*@paramtableIndex*被拷贝的表格在word文档中所处的位置*/publicvoidcopyTable(Stringpos,inttableIndex){//所有表格Dispatchtables=Dispatch.get(doc,"Tables").toDispatch();//要填充的表格Dispatchtable=Dispatch.call(tables,"Item",newVariant(tableIndex)).toDispatch();Dispatchrange=Dispatch.get(table,"Range").toDispatch();Dispatch.call(range,"Copy");if(this.find(pos)){DispatchtextRange=Dispatch.get(selection,"Range").toDispatch();Dispatch.call(textRange,"Paste");}}/***在当前文档指定的位置拷贝来自另一个文档中的表格**@paramanotherDocPath*另一个文档的磁盘路径*@paramtableIndex*被拷贝的表格在另一格文档中的位置*@parampos*当前文档指定的位置*/publicvoidcopyTableFromAnotherDoc(StringanotherDocPath,inttableIndex,Stringpos){Dispatchdoc2=null;try{doc2=Dispatch.call(documents,"Open",anotherDocPath).toDispatch();//所有表格Dispatchtables=Dispatch.get(doc2,"Tables").toDispatch();//要填充的表格Dispatchtable=Dispatch.call(tables,"Item",newVariant(tableIndex)).toDispatch();Dispatchrange=Dispatch.get(table,"Range").toDispatch();Dispatch.call(range,"Copy");if(this.find(pos)){DispatchtextRange=Dispatch.get(selection,"Range").toDispatch();Dispatch.call(textRange,"Paste");}}catch(Exceptione){e.printStackTrace();}finally{if(doc2!=null){Dispatch.call(doc2,"Close",newVariant(saveOnExit));doc2=null;}}}/***在当前文档指定的位置拷贝来自另一个文档中的图片*@paramanotherDocPath另一个文档的磁盘路径*@paramshapeIndex被拷贝的图片在另一格文档中的位置*@parampos当前文档指定的位置*/publicvoidcopyImageFromAnotherDoc(StringanotherDocPath,intshapeIndex,Stringpos){Dispatchdoc2=null;try{doc2=Dispatch.call(documents,"Open",anotherDocPath).toDispatch();Dispatchshapes=Dispatch.get(doc2,"InLineShapes").toDispatch();Dispatchshape=Dispatch.call(shapes,"Item",newVariant(shapeIndex)).toDispatch();DispatchimageRange=Dispatch.get(shape,"Range").toDispatch();Dispatch.call(imageRange,"Copy");if(this.find(pos)){DispatchtextRange=Dispatch.get(selection,"Range").toDispatch();Dispatch.call(textRange,"Paste");}}catch(Exceptione){e.printStackTrace();}finally{if(doc2!=null){Dispatch.call(doc2,"Close",newVariant(saveOnExit));doc2=null;}}}/***创建表格**@parampos*位置*@paramcols*列数*@paramrows*行数*/publicvoidcreateTable(Stringpos,intnumCols,intnumRows){if(find(pos)){Dispatchtables=Dispatch.get(doc,"Tables").toDispatch();Dispatchrange=Dispatch.get(selection,"Range").toDispatch();DispatchnewTable=Dispatch.call(tables,"Add",range,newVariant(numRows),newVariant(numCols)).toDispatch();Dispatch.call(selection,"MoveRight");}}/***在指定行前面增加行**@paramtableIndex*word文件中的第N张表(从1开始)*@paramrowIndex*指定行的序号(从1开始)*/publicvoidaddTableRow(inttableIndex,introwIndex){//所有表格Dispatchtables=Dispatch.get(doc,"Tables").toDispatch();//要填充的表格Dispatchtable=Dispatch.call(tables,"Item",newVariant(tableIndex)).toDispatch();//表格的所有行Dispatchrows=Dispatch.get(table,"Rows").toDispatch();Dispatchrow=Dispatch.call(rows,"Item",newVariant(rowIndex)).toDispatch();Dispatch.call(rows,"Add",newVariant(row));}/***在第1行前增加一行**@paramtableIndex*word文档中的第N张表(从1开始)*/publicvoidaddFirstTableRow(inttableIndex){//所有表格Dispatchtables=Dispatch.get(doc,"Tables").toDispatch();//要填充的表格Dispatchtable=Dispatch.call(tables,"Item",newVariant(tableIndex)).toDispatch();//表格的所有行Dispatchrows=Dispatch.get(table,"Rows").toDispatch();Dispatchrow=Dispatch.get(rows,"First").toDispatch();Dispatch.call(rows,"Add",newVariant(row));}/***在最后1行前增加一行**@paramtableIndex*word文档中的第N张表(从1开始)*/publicvoidaddLastTableRow(inttableIndex){//所有表格Dispatchtables=Dispatch.get(doc,"Tables").toDispatch();//要填充的表格Dispatchtable=Dispatch.call(tables,"Item",newVariant(tableIndex)).toDispatch();//表格的所有行Dispatchrows=Dispatch.get(table,"Rows").toDispatch();Dispatchrow=Dispatch.get(rows,"Last").toDispatch();Dispatch.call(rows,"Add",newVariant(row));}/***增加一行**@paramtableIndex*word文档中的第N张表(从1开始)*/publicvoidaddRow(inttableIndex){Dispatchtables=Dispatch.get(doc,"Tables").toDispatch();//要填充的表格Dispatchtable=Dispatch.call(tables,"Item",newVariant(tableIndex)).toDispatch();//表格的所有行Dispatchrows=Dispatch.get(table,"Rows").toDispatch();Dispatch.call(rows,"Add");}/***增加一列**@paramtableIndex*word文档中的第N张表(从1开始)*/publicvoidaddCol(inttableIndex){//所有表格Dispatchtables=Dispatch.get(doc,"Tables").toDispatch();//要填充的表格Dispatchtable=Dispatch.call(tables,"Item",newVariant(tableIndex)).toDispatch();//表格的所有行Dispatchcols=Dispatch.get(table,"Columns").toDispatch();Dispatch.call(cols,"Add").toDispatch();Dispatch.call(cols,"AutoFit");}/***在指定列前面增加表格的列**@paramtableIndex*word文档中的第N张表(从1开始)*@paramcolIndex*制定列的序号(从1开始)*/publicvoidaddTableCol(inttableIndex,intcolIndex){//所有表格Dispatchtables=Dispatch.get(doc,"Tables").toDispatch();//要填充的表格Dispatchtable=Dispatch.call(tables,"Item",newVariant(tableIndex)).toDispatch();//表格的所有行Dispatchcols=Dispatch.get(table,"Columns").toDispatch();System.out.println(Dispatch.get(cols,"Count"));Dispatchcol=Dispatch.call(cols,"Item",newVariant(colIndex)).toDispatch();//Dispatchcol=Dispatch.get(cols,"First").toDispatch();Dispatch.call(cols,"Add",col).toDispatch();Dispatch.call(cols,"AutoFit");}/***在第1列前增加一列**@paramtableIndex*word文档中的第N张表(从1开始)*/publicvoidaddFirstTableCol(inttableIndex){Dispatchtables=Dispatch.get(doc,"Tables").toDispatch();//要填充的表格Dispatchtable=Dispatch.call(tables,"Item",newVariant(tableIndex)).toDispatch();//表格的所有行Dispatchcols=Dispatch.get(table,"Columns").toDispatch();Dispatchcol=Dispatch.get(cols,"First").toDispatch();Dispatch.call(cols,"Add",col).toDispatch();Dispatch.call(cols,"AutoFit");}/***在最后一列前增加一列**@paramtableIndex*word文档中的第N张表(从1开始)*/publicvoidaddLastTableCol(inttableIndex){Dispatchtables=Dispatch.get(doc,"Tables").toDispatch();//要填充的表格Dispatchtable=Dispatch.call(tables,"Item",newVariant(tableIndex)).toDispatch();//表格的所有行Dispatchcols=Dispatch.get(table,"Columns").toDispatch();Dispatchcol=Dispatch.get(cols,"Last").toDispatch();Dispatch.call(cols,"Add",col).toDispatch();Dispatch.call(cols,"AutoFit");}/***自动调整表格**/publicvoidautoFitTable(){Dispatchtables=Dispatch.get(doc,"Tables").toDispatch();intcount=Dispatch.get(tables,"Count").toInt();for(inti=0;i<count;i++){Dispatchtable=Dispatch.call(tables,"Item",newVariant(i+1)).toDispatch();Dispatchcols=Dispatch.get(table,"Columns").toDispatch();Dispatch.call(cols,"AutoFit");}}
解决方案四:
/***调用word里的宏以调整表格的宽度,其中宏保存在document下**/publicvoidcallWordMacro(){Dispatchtables=Dispatch.get(doc,"Tables").toDispatch();intcount=Dispatch.get(tables,"Count").toInt();VariantvMacroName=newVariant("Normal.NewMacros.tableFit");VariantvParam=newVariant("param1");Variantpara[]=newVariant[]{vMacroName};for(inti=0;i<count;i++){Dispatchtable=Dispatch.call(tables,"Item",newVariant(i+1)).toDispatch();Dispatch.call(table,"Select");Dispatch.call(word,"Run","tableFitContent");}}/***设置当前选定内容的字体**@paramboldSize*@paramitalicSize*@paramunderLineSize*下划线*@paramcolorSize*字体颜色*@paramsize*字体大小*@paramname*字体名称*/publicvoidsetFont(booleanbold,booleanitalic,booleanunderLine,StringcolorSize,Stringsize,Stringname){Dispatchfont=Dispatch.get(selection,"Font").toDispatch();Dispatch.put(font,"Name",newVariant(name));Dispatch.put(font,"Bold",newVariant(bold));Dispatch.put(font,"Italic",newVariant(italic));Dispatch.put(font,"Underline",newVariant(underLine));Dispatch.put(font,"Color",colorSize);Dispatch.put(font,"Size",size);}/***文件保存或另存为**@paramsavePath*保存或另存为路径*/publicvoidsave(StringsavePath){Dispatch.call(Dispatch.call(word,"WordBasic").getDispatch(),"FileSaveAs",savePath);}/***关闭当前word文档**/publicvoidcloseDocument(){if(doc!=null){Dispatch.call(doc,"Save");Dispatch.call(doc,"Close",newVariant(saveOnExit));doc=null;}}/***关闭全部应用**/publicvoidclose(){closeDocument();if(word!=null){Dispatch.call(word,"Quit");word=null;}selection=null;documents=null;}/***打印当前word文档**/publicvoidprintFile(){if(doc!=null){Dispatch.call(doc,"PrintOut");}}publicstaticvoidmain(Stringargs[]){Jacob3msWordManager=newJacob3();try{msWordManager.openDocument("D:\123.doc");//msWordManager.callWordMacro();msWordManager.setFont(false,true,true,"1,0,0,0","15","仿宋_GB2312");msWordManager.replaceAllText("内1容hahahahahha","mark");msWordManager.replaceAllText("mark","aaaaa");msWordManager.closeDocument();}catch(Exceptione){e.printStackTrace();}msWordManager.close();}}

时间: 2025-01-01 22:53:11

关于jacob的使用疑惑的相关文章

jacob操纵word文件中控制行间距的问题

问题描述 jacob操纵word文件中控制行间距的问题 代码如下:如果参数lineSpace=4;及行间距设为固定值,但在word中设为固定值后, 还要设置一个磅值,请问在下面代码中如何设置磅值?或者有大侠知道磅值的参数名称是什么? /** * 对当前段落进行格式化 * * @param align * 设置排列方式 默认:居左 0:居左 1:居中 2:居右 3:两端对齐 4:分散对齐 * @param lineSpace * 设置行间距 默认:1.0 0:1.0 1:1.5 2:2.0 3:最

新手学习c++,使用vs的疑惑

问题描述 新手学习c++,使用vs的疑惑 大家好,我是一名刚刚学习c++的新手,语法已经看得还可以了,使用vs2010写程序,但是我发现写出来的程序都是命令提示框,跟我之前使用的vb根本不同,不能直接做出界面,折腾了好久,后来发现vs中有一个工具箱,貌似可以使用控件设计界面,但是又发现这好像是c#的代码,而且看了一下,好像都是用类处理的,作为c++新手的我就蒙掉了. 我就想问是不是学习c++,使用vs就只能是利用mfc做界面,或者是直接调用api制作界面呢?而呢个工具箱好像就是为c#设计而已啊?

double类型结构体对齐的疑惑

问题描述 double类型结构体对齐的疑惑 32bit的cpu,在msvc中如果结构体有double类型,则以8字节对齐,例如 struct test { char ch; double j; }; ch也会占用8个字节,而32bit的cpu会一次性取到8个字节么?难道不是32bit,4个字节? 为什么要以8个字节来对齐呢?谢谢 解决方案 如果编译器为sse优化,那么是按照128bit,也就是8字节对齐的,如果编译器为sse2优化,那么是按照16字节对齐的.http://www.xuebuyua

个人疑惑之——机器学习

问题描述 个人疑惑之--机器学习 本科生适合搞机器学习么?能做到什么样的程度呢?个人觉得数学太不够了,或者有大神带带小弟么?

ie兼容-关于IE条件语句的一点疑惑

问题描述 关于IE条件语句的一点疑惑 经常在网站头部看到类似于这样的IE条件注释: <!--[if IE 7 ]><html lang=""zh"" id=""ne_wrap"" class=""no-js ie7""><![endif]--> 虽然能够理解该注释语法:在浏览器版本为ie7时,应用该代码,非ie浏览器则只把其当做一条注释而忽略掉.但不太

JS异步传递数组Action接受的实现与疑惑

最近开发中遇到了页面传递数组到Action后台List类型,接受到的list对象并不是想象的按照数组元素位置对应的接受,例如数组的0位置插入到list对象的0位置, 而是数组的全部内容全部插入到了list集合的第一位置.经过反复的试验,没有找到很好的解决办法,只能把这种粗糙的实现方式记录下来,以求抛砖引玉望大家能给出更好的实现方式. 这是jsp页面代码:异步提交数组到Action中: <html> <head> <meta http-equiv="Content-T

网页前端分析:关于淘宝页面编码的疑惑

因为即将开始淘宝的项目,在前端方面必然要深入了解taobao ued规范,规范还是比较全的,只是对taobao.com的编码和字符集的选择有很多困惑,由于历史原因,taobao的页面编码是ASCII编码,字符集采用gb2312,这并无不妥,麻烦的仅仅是做代码开发的时候要抽出精力去对付复杂多元的字符集,比如在taobao首页是ASCII,页面的meta中指定的字符集是GB18030,但在window下,用firefox另存页面到本地后,发现meta字段的字符集变为gb2312,而且在ie中打开淘宝

对云计算服务安全问题的疑惑

进入2011年,不谈论"云"的IT会议几乎是没有了,厂家争先恐后地推出"云"概念与方案,用户摩拳擦掌地立项建设:大家讨论"云服务"模式的产业链,畅想"云经济"的宏伟蓝图:从弹性计算服务到公众出租服务,从"私有云"到"公共云",从"云安全"到"安全的云"- 我这里不准备讨论各种云的运营模式与技术,只是想弄清楚一点:"云服务"是一种

解答有关REST的十点疑惑

在了解过REST之后,你肯定很想知道这个概念在你的实际应用当中究竟能派上多大用场.而且,假如你已经熟悉另一套完全不同的架构手法的话,那么你担心"REST或REST式HTTP(RESTful HTTP),是否真的能在实践中派上用场,还是在介绍性的.'Hello, World'级场景以外就不灵光了"是很正常的.我将在本文解答人们--尤其是那些深谙基于SOAP/WSDL的Web服务架构手法的人--开始研究REST时容易产生的关于REST的十点疑惑. 1.REST也许适用于CRUD,但并不适用