java jacob 操作word 文档,进行写操作.docx_第1页
java jacob 操作word 文档,进行写操作.docx_第2页
java jacob 操作word 文档,进行写操作.docx_第3页
java jacob 操作word 文档,进行写操作.docx_第4页
java jacob 操作word 文档,进行写操作.docx_第5页
已阅读5页,还剩28页未读 继续免费阅读

下载本文档

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

文档简介

目录第一部分 Word文档自动追加表格内容2第二部分 java jacob 操作word 文档,进行写操作,如生成表格,添加 图片3第三部分28 第一部分 Word文档自动追加表格内容今天在做一个自动化生成SDD文档的小工具,通过Word的模板,前台通过Flex填入数据,最后将Word文档填写好。以下是一部分代码,关于表格自动追加表格内容Java代码1. importjava.util.ArrayList;2. 3. importcom.heavenlake.wordapi.Document;4. 5. publicclasstest6. 7. publictest()8. 9. Documentdoc=null;10. try11. doc=newDocument();12. doc.newDoc(e:/test4.doc);13. doc.insertln(测试文档);14. doc.insertln(天池软件产品列表);15. /doc.saveAs(e:/test2.doc);16. ArrayListtableData=newArrayList(3);17. Stringtitle=18. 序号,代码,名称,网址;19. tableData.add(title);20. Stringfield1=21. 1,751002,天池软件自动化生产平台,;22. tableData.add(field1);23. Stringfield2=24. 2,751004,产品数据管理系统,;25. tableData.add(field2);26. 27. doc.insert(tableData,流行型);28. 29. doc.insertln();30. 31. 32. ArrayListtableData1=newArrayList(3);33. 34. Stringfield3=35. 1,751002,天池软件自动化生产平台,;36. tableData1.add(field3);37. Stringfield4=38. 2,751004,产品数据管理系统,;39. tableData1.add(field4);40. doc.replaceTable(1,4,tableData1);41. 42. catch(Exceptione)43. 44. e.printStackTrace();45. finally46. 47. try48. 49. if(doc!=null)50. doc.close(true);51. catch(Exceptione)52. 53. e.printStackTrace();54. 55. 56. 57. 58. 59. 60. publicstaticvoidmain(Stringargs)61. 62. testtest1=newtest();63. 64. 65. 第二部分 java jacob 操作word 文档,进行写操作,如生成表格,添加 图片jacob-1.15-M3.zipjacob-1.15-M3-x86.dllcopy 到c:/windows/system32引入jacob.jar示例代码view plain1. 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(!find(toFindText)243. returnfalse;244. Dispatchselection=Dispatch.get(MsWordApp,Selection).toDispatch();/输入内容需要的对象245. Dispatch.put(selection,Text,newText);246. returntrue;247. 248. publicvoidprintFile()249. /Justprintthecurrentdocumenttothedefaultprinter250. Dispatch.call(document,PrintOut);251. 252. /保存文档的更改253. publicvoidsave()254. Dispatch.call(document,Save);255. 256. publicvoidsaveFileAs(Stringfilename)257. Dispatch.call(document,SaveAs,filename);258. 259. publicvoidcloseDocument()260. /Closethedocumentwithoutsavingchanges261. /0=wdDoNotSaveChanges262. /-1=wdSaveChanges263. /-2=wdPromptToSaveChanges264. Dispatch.call(document,Close,newVariant(0);265. document=null;266. 267. publicvoidcloseWord()268. Dispatch.call(MsWordApp,Quit);269. MsWordApp=null;270. document=null;271. 272. /设置wordApp打开后窗口的位置273. publicvoidsetLocation()274. DispatchactiveWindow=Dispatch.get(MsWordApp,Application)275. .toDispatch();276. Dispatch.put(activeWindow,WindowState,newVariant(1);/0=default277. /1=maximize278. /2=minimize279. Dispatch.put(activeWindow,Top,newVariant(0);280. Dispatch.put(activeWindow,Left,newVariant(0);281. Dispatch.put(activeWindow,Height,newVariant(600);282. Dispatch.put(activeWindow,width,newVariant(800);283. 284. 285. publicclassJacobTest2286. publicstaticvoidcreateANewFileTest()287. WordBeanwordBean=newWordBean();288. /word.openWord(true);/打开word程序289. wordBean.setVisible(true);290. wordBean.createNewDocument();/创建一个新文档291. wordBean.setLocation();/设置打开后窗口的位置292. wordBean.insertText(你好);/向文档中插入字符293. wordBean.insertJpeg(D:+File.separator+a.jpg);/插入图片294. /如果,想保存文件,下面三句295. /word.saveFileAs(d:/a.doc);296. /word.closeDocument();297. /word.closeWord();298. 299. publicstaticvoidopenAnExistsFileTest()300. WordBeanwordBean=newWordBean();301. wordBean.setVisible(true);/是否前台打开word程序,或者后台运行302. wordBean.openFile(d:/a.doc);303. wordBean.insertJpeg(D:+File.separator+a.jpg);/插入图片(注意刚打开的word304. /,光标处于开头,故,图片在最前方插入)305. wordBean.save();306. wordBean.closeDocument();307. wordBean.closeWord();308. 309. publicstaticvoidinsertFormatStr(Stringstr)310. WordBeanwordBean=newWordBean();311. wordBean.setVisible(true);/是否前台打开word程序,或者后台运行312. wordBean.createNewDocument();/创建一个新文档313. wordBean.insertFo

温馨提示

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

评论

0/150

提交评论