jacob常用的方法_第1页
jacob常用的方法_第2页
jacob常用的方法_第3页
jacob常用的方法_第4页
jacob常用的方法_第5页
已阅读5页,还剩27页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

通过Java调用OCX控件有几种方法,JNI、JACOB、Jawin等1.JNI最直接的方式,也是最麻烦的方式,需要自己完成所有的工作,不推荐。2.Jawin尝试了一下,效果不错,但相对来说,其编程风格更贴近Windows,离Java有点远3.Jacob使用Jacob非常方便,Java编程风格,需要了解的知识比较少。下载地址/projects/jacob-project/Jacob的使用方法1.初始化 ComThread.InitMTA(true);ActiveXComponent com = new ActiveXComponent(组件的ProgID) ;Dispatch disp = com.getObject();2.调用控件里面的方面2.1调用无参的方法,并返回一个short值Dispatch.call(disp, Init).getShort();2.2调用有一个参数的方法,并返回一个boolean值Dispatch.call(disp,Method,new Variant(args).getBoolean();调用多个参数依次类推,注意在传递参数前,将Java中的参数转换成Variant。问题解决在使用Jacob调用OCX控件时,总是出一个异常Exception in thread main .ComFailException: A COM exception has been encountered: At Invoke of: InitDescription: 灾难性故障 通过Jawin调用,会出现8000FFFF错误。这个错误是由ActiveX结构设计造成的。在Ole4.0版本之前,外部程序是可以直接调用OCX中方法的。Ole4.0之后,每次调用控件中的方法,系统会自动检查是否允许调用,即运行COleControl.IsInvokeAllowed (DISPID)该方法检查控件是否正确的初始化或者是否通过持久存储接口正确加载,如果两个条件有一个满足,即返回TRUE,否则返回FALSE。当控件在MFC中使用时,很多细节,如初始化,都被过滤了,这样,大多数用户都不会遇到这个问题。但是,当我们从C、C+的dll中调用控件时,不满足上述条件,该方法返回FALSE,这时候再调用任何控件方法,都会出现上述异常。解决方法很简单,在OCX控件中,重写COleControl.IsInvokeAllowed (DISPID)方法,即在控件的CMyNameCtrl中增加一个方法BOOL CMyNameCtrl:IsInvokeAllowed (DISPID) return TRUE; 问题解决 首先,大家先要了解一下jacob ,官方的解释是Java COM Bridge,即java和com组件间的桥梁(进一步了解com/dcom: /com/)com一般表现为dll或exe等二进制文件,像我们呆会会用到的jacob.dll文件这里说说为什么我们用java去操纵office(如:word)要使用com,而不直接使用java去做?首先,我们清楚office是建立在windows平台之上的,本身是一个软件,除了他自己提供的宏似乎没有什么能对他进行直接的操作;在windows平台上为了解决像这样的不同应用软件,通信缺乏通用api问题,推出了com的解决方案;我们使用dll中的一组或多组相关的函数存取组件数据,总的合称为接口具体到每个细节的实现称为方法;如果我们要调用接口里的方法,唯一的途径就是调用指向接口的指针; 所以总的来说使用就是dll完成api的转换;如果你听不懂,则称为废话(不懂没关系,会用就好,用久自然懂了); 好了com讲完,我们开始我们的主要内容吧!大家先下载这里jacob_1.9.zip里面的jacob.jar是我们要用的包 jacob.dll就是我前面说的com组件把包里的jacob.dll放到c:/windows/system32下讲解麻烦,画个图大家看 好值得注意的是,不同的版本的系统使用不同的dll文件所以如果你编译成功,但运行失败一般是dll文件问题遇到这种情况,可以到/jacob-project/jacob_1.9.zip?modtime=1109437002&big_mirror=0下载其他的版本的 dll 文件。先给大家个word的测试代码(经过更改该代码在我的机器上运行正常)以后有会找时间,推出其他的office代码 import com.jacob.activeX.ActiveXComponent; import .ComException; import .Dispatch; import .Variant; public class WordDocumentProperties / 声明一个word对象 private ActiveXComponentobjWord; / 声明四个word组件 private DispatchcustDocprops; private DispatchbuiltInDocProps; private Dispatchdocument; private DispatchwordObject; public WordDocumentProperties() /*/ /* *打开word文挡 */ public void open(Stringfilename) / 创建一个word对象 objWord = new ActiveXComponent( Word.Application ); / 为wordobject组件附值 wordObject = (Dispatch)(objWord.getObject(); / 改了这里 / 生成一个只读方式的word文挡组件 Dispatch.put(wordObject, Visible , new Variant( false ); / 获取文挡属性 Dispatchdocuments = objWord.getProperty( Documents ).toDispatch(); / 打开激活文挡 document = Dispatch.call(documents, Open ,filename).toDispatch(); public void selectCustomDocumentProperitiesMode() custDocprops = Dispatch.get(document, CustomDocumentProperties ).toDispatch(); public void selectBuiltinPropertiesMode() builtInDocProps = Dispatch.get(document, BuiltInDocumentProperties ).toDispatch(); /*/ /* *关闭文挡 */ public void close() Dispatch.call(document, Close ); public StringgetCustomProperty(StringcusPropName) try cusPropName = Dispatch.call(Dispatch)custDocprops, Item ,cusPropName).toString(); catch (ComExceptione) cusPropName = null ; return cusPropName; public StringgetBuiltInProperty(StringbuiltInPropName) try builtInPropName = Dispatch.call(Dispatch)builtInDocProps, Item ,builtInPropName).toString(); catch (ComExceptione) builtInPropName = null ; return builtInPropName; public static void main(Stringargs) try WordDocumentPropertiesjacTest = new WordDocumentProperties();jacTest.open( s.doc );jacTest.selectCustomDocumentProperitiesMode();jacTest.selectBuiltinPropertiesMode();StringcustValue = jacTest.getCustomProperty( InformationSource );StringbuiltInValue = jacTest.getBuiltInProperty( Author );jacTest.close();System.out.println( DocumentValOne: + custValue);System.out.println( DocumentAuthor: + builtInValue); catch (Exceptione) System.out.println(e); 4.java jacob 操作word 文档进行写操作,如生成表格,添加 图片jacob-1.15-M3-x86.dllcopy 到c:/windows/system32引入jacob.jar示例代码javaview plaincopy1. importjava.io.File;2. importcom.jacob.activeX.ActiveXComponent;3. .Dispatch;4. .Variant;5. classWordBean6. /代表一个word程序7. privateActiveXComponentMsWordApp=null;8. /代表进行处理的word文档9. privateDispatchdocument=null;10. publicWordBean()11. /OpenWordifwe/venotdoneitalready12. if(MsWordApp=null)13. MsWordApp=newActiveXComponent(Word.Application);14. 15. 16. /设置是否在前台打开word程序,17. publicvoidsetVisible(booleanvisible)18. MsWordApp.setProperty(Visible,newVariant(visible);19. /这一句作用相同20. /Dispatch.put(MsWordApp,Visible,newVariant(visible);21. 22. /创建一个新文档23. publicvoidcreateNewDocument()24. /FindtheDocumentscollectionobjectmaintainedbyWord25. /documents表示word的所有文档窗口,(word是多文档应用程序)26. Dispatchdocuments=Dispatch.get(MsWordApp,Documents).toDispatch();27. /CalltheAddmethodoftheDocumentscollectiontocreate28. /anewdocumenttoedit29. document=Dispatch.call(documents,Add).toDispatch();30. 31. /打开一个存在的word文档,并用document引用引用它32. publicvoidopenFile(StringwordFilePath)33. /FindtheDocumentscollectionobjectmaintainedbyWord34. /documents表示word的所有文档窗口,(word是多文档应用程序)35. Dispatchdocuments=Dispatch.get(MsWordApp,Documents).toDispatch();36. document=Dispatch.call(documents,Open,wordFilePath,37. newVariant(true)/*是否进行转换ConfirmConversions*/,38. newVariant(false)/*是否只读*/).toDispatch();39. /document=Dispatch.invoke(documents,Open,Dispatch.Method,40. /newObjectwordFilePath,newVariant(true),41. /newVariant(false)42. /,newint1).toDispatch();43. 44. /向document中插入文本内容45. publicvoidinsertText(StringtextToInsert)46. /GetthecurrentselectionwithinWordatthemoment.47. /anewdocumenthasjustbeencreatedthenthiswillbeat48. /thetopofthenewdoc获得选中的内容,如果是一个新创建的文件,因里面无内容,则光标应处于文件开头处49. Dispatchselection=Dispatch.get(MsWordApp,Selection).toDispatch();50. /取消选中,应该就是移动光标,否则新添加的内容会覆盖选中的内容51. Dispatch.call(selection,MoveRight,newVariant(1),newVariant(1);52. /Putthespecifiedtextattheinsertionpoint53. Dispatch.put(selection,Text,textToInsert);54. /取消选中,应该就是移动光标55. Dispatch.call(selection,MoveRight,newVariant(1),newVariant(1);56. 57. /向文档中添加一个图片,58. publicvoidinsertJpeg(StringjpegFilePath)59. Dispatchselection=Dispatch.get(MsWordApp,Selection).toDispatch();60. Dispatchimage=Dispatch.get(selection,InLineShapes).toDispatch();61. Dispatch.call(image,AddPicture,jpegFilePath);62. 63. /段落的处理,插入格式化的文本64. publicvoidinsertFormatStr(Stringtext)65. DispatchwordContent=Dispatch.get(document,Content).toDispatch();/取得word文件的内容66. Dispatch.call(wordContent,InsertAfter,text);/插入一个段落到最后67. Dispatchparagraphs=Dispatch.get(wordContent,Paragraphs)68. .toDispatch();/所有段落69. intparagraphCount=Dispatch.get(paragraphs,Count).changeType(70. Variant.VariantInt).getInt();/一共的段落数71. /找到刚输入的段落,设置格式72. DispatchlastParagraph=Dispatch.call(paragraphs,Item,73. newVariant(paragraphCount).toDispatch();/最后一段(也就是刚插入的)74. /Range对象表示文档中的一个连续范围,由一个起始字符位置和一个终止字符位置定义75. DispatchlastParagraphRange=Dispatch.get(lastParagraph,Range)76. .toDispatch();77. Dispatchfont=Dispatch.get(lastParagraphRange,Font).toDispatch();78. Dispatch.put(font,Bold,newVariant(true);/设置为黑体79. Dispatch.put(font,Italic,newVariant(true);/设置为斜体80. Dispatch.put(font,Name,newVariant(宋体);/81. Dispatch.put(font,Size,newVariant(12);/小四82. Dispatchselection=Dispatch.get(MsWordApp,Selection).toDispatch();83. Dispatch.call(selection,TypeParagraph);/插入一个空行84. Dispatchalignment=Dispatch.get(selection,ParagraphFormat)85. .toDispatch();/段落格式86. Dispatch.put(alignment,Alignment,2);/(1:置中2:靠右3:靠左)87. 88. /word中在对表格进行遍历的时候,是先列后行先column后cell89. /另外下标从1开始90. publicvoidinsertTable(StringtableTitle,introw,intcolumn)91. Dispatchselection=Dispatch.get(MsWordApp,Selection).toDispatch();/输入内容需要的对象92. Dispatch.call(selection,TypeText,tableTitle);/写入标题内容/标题格行93. Dispatch.call(selection,TypeParagraph);/空一行段落94. Dispatch.call(selection,TypeParagraph);/空一行段落95. Dispatch.call(selection,MoveDown);/游标往下一行96. /建立表格97. Dispatchtables=Dispatch.get(document,Tables).toDispatch();98. /intcount=Dispatch.get(tables,99. /Count).changeType(Variant.VariantInt).getInt();/document中的表格数量100. /Dispatchtable=Dispatch.call(tables,Item,newVariant(101. /1).toDispatch();/文档中第一个表格102. Dispatchrange=Dispatch.get(selection,Range).toDispatch();/当前光标位置或者选中的区域103. DispatchnewTable=Dispatch.call(tables,Add,range,104. newVariant(row),newVariant(column),newVariant(1)105. .toDispatch();/设置row,column,表格外框宽度106. Dispatchcols=Dispatch.get(newTable,Columns).toDispatch();/此表的所有列,107. intcolCount=Dispatch.get(cols,Count).changeType(108. Variant.VariantInt).getInt();/一共有多少列实际上这个数=column109. System.out.println(colCount+列);110. for(inti=1;i=colCount;i+)/循环取出每一列111. Dispatchcol=Dispatch.call(cols,Item,newVariant(i)112. .toDispatch();113. Dispatchcells=Dispatch.get(col,Cells).toDispatch();/当前列中单元格114. intcellCount=Dispatch.get(cells,Count).changeType(115. Variant.VariantInt).getInt();/当前列中单元格数实际上这个数等于row116. for(intj=1;j=cellCount;j+)/每一列中的单元格数117. /Dispatchcell=Dispatch.call(cells,Item,new118. /Variant(j).toDispatch();/当前单元格119. /Dispatchcell=Dispatch.call(newTable,Cell,new120. /Variant(j),newVariant(i).toDispatch();/取单元格的另一种方法121. /Dispatch.call(cell,Select);/选中当前单元格122. /Dispatch.put(selection,Text,123. /第+j+行,第+i+列);/往选中的区域中填值,也就是往当前单元格填值124. putTxtToCell(newTable,j,i,第+j+行,第+i+列);/与上面四句的作用相同125. 126. 127. 128. /*/129. /*130. *在指定的单元格里填写数据131. *132. *paramtableIndex133. *paramcellRowIdx134. *paramcellColIdx135. *paramtxt136. */137. publicvoidputTxtToCell(Dispatchtable,intcellRowIdx,intcellColIdx,138. Stringtxt)139. Dispatchcell=Dispatch.call(table,Cell,newVariant(cellRowIdx),140. newVariant(cellColIdx).toDispatch();141. Dispatch.call(cell,Select);142. Dispatchselection=Dispatch.get(MsWordApp,Selection).toDispatch();/输入内容需要的对象143. Dispatch.put(selection,Text,txt);144. 145. /*/146. /*147. *在指定的单元格里填写数据148. *149. *paramtableIndex150. *paramcellRowIdx151. *paramcellColIdx152. *paramtxt153. */154. publicvoidputTxtToCell(inttableIndex,intcellRowIdx,intcellColIdx,155. Stringtxt)156. /所有表格157. Dispatchtables=Dispatch.get(document,Tables).toDispatch();158. /要填充的表格159. Dispatchtable=Dispatch.call(tables,Item,newVariant(tableIndex)160. .toDispatch();161. Dispatchcell=Dispatch.call(table,Cell,newVariant(cellRowIdx),162. newVariant(cellColIdx).toDispatch();163. Dispatch.call(cell,Select);164. Dispatchselection=Dispatch.get(MsWordApp,Selection).toDispatch();/输入内容需要的对象165. Dispatch.put(selection,Text,txt);166. 167. /合并两个单元格168. publicvoidmergeCell(Dispatchcell1,Dispatchcell2)169. Dispatch.call(cell1,Merge,cell2);170. 171. publicvoidmergeCell(Dispatchtable,introw1,intcol1,introw2,intcol2)172. Dispatchcell1=Dispatch.call(table,Cell,newVariant(row1),173. newVariant(col1).toDispatch();174. Dispatchcell2=Dispatch.call(table,Cell,newVariant(row2),175. newVariant(col2).toDispatch();176. mergeCell(cell1,cell2);177. 178. publicvoidmergeCellTest()179. Dispatchtables=Dispatch.get(document,Tables).toDispatch();180. inttableCount=Dispatch.get(tables,Count).changeType(181. Variant.VariantInt).getInt();/document中的表格数量182. Dispatchtable=Dispatch.call(tables,Item,newVariant(tableCount)183. .toDispatch();/文档中最后一个table184. mergeCell(table,1,1,1,2);/将table中x=1,y=1与x=1,y=2的两个单元格合并185. 186. /=187. /*/188. /*189. *把选定的内容或光标插入点向上移动190. *191. *parampos192. *移动的距离193. */194. publicvoidmoveUp(intpos)195. Dispatchselection=Dispatch.get(MsWordApp,Selection).toDispatch();/输入内容需要的对象196. for(inti=0;ipos;i+)197. /MoveDownMoveLeftmoveRight198. /moveStart(Dispatch.call(selection,HomeKey,newVariant(6);199. /)200. /moveEndDispatch.call(selection,EndKey,newVariant(6);201. Dispatch.call(selection,MoveUp);202. 203. 204. /*/205. /*206. *从选定内容或插入点开始查找文本207. *208. *paramtoFindText209. *要查找的文本210. *returnbooleantrue-查找到并选中该文本,false-未查找到文本211. */212. publicbooleanfind(StringtoFindText)213. if(toFindText=null|toFindText.equals()214. returnfalse;215. Dispatchselection=Dispatch.get(MsWordApp,Selection).toDispatch();/输入内容需要的对象216. /从selection所在位置开始查询217. Dispatchfind=Dispatch.call(selection,Find).toDispatch();218. /设置要查找的内容219. Dispatch.put(find,Text,toFindText);220. /向前查找221. Dispatch.put(find,Forward,True);222. /设置格式223. Dispatch.put(find,Format,True);224. /大小写匹配225. Dispatch.put(find,MatchCase,True);226. /全字匹配227. Dispatch.put(find,MatchWholeWord,True);228. /查找并选中229. returnDispatch.call(find,Execute).getBoolean();230. 231. /*/232. /*233. *把选定选定内容设定为替换文本234. *235. *paramtoFindText236. *查找字符串237. *paramnewText238. *要替换的内容239. *return240. */241. publicbooleanreplaceText(StringtoFindText,StringnewText)242. if(!f

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论