java操作内部资料_第1页
java操作内部资料_第2页
java操作内部资料_第3页
java操作内部资料_第4页
java操作内部资料_第5页
免费预览已结束,剩余15页可下载查看

下载本文档

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

文档简介

1、JAVA 操作 wordJava操作MicrosoftWord之jacob(1)现在我们一起来看看,用 J1ava 如何操作 MicrosoftWord。jacob,官网是 http:/ 这是一个开源的工具。最新版本 1.7官方的解释是:TheJACOBProject:AJAva-COMBridge这是官方对下载文件的说明:jacob.jar:aJARfileforthejavaclasseswhichyoumustaddtoyourCLASSPATH.Thepackagenamesreplacecom.mswithcom.jacob(.V.Variant.jacob.dll:asmallWi

2、n32DLLwhichyoumustaddtoyourPATH.samples:providedinJavasourceandcompiledformtodemonstratevariousfeaturesoftheproduct.Inparticular,asetofwrapperclassesforMicrosoft?ADOareprovidedassamples.开发环境:JDK1.6MyEclipseEnterpriseWorkbenchVersion:7.0Milestone-1Tomcat5.5.27现在 MyEclipse 中新建一个项目 jacob,将 jacob 的 jar

3、包放到该项目的类库中。我的 jacob 版本是 1.14.3。Pack/RHierarQ1G弦bfot1,】tHVt-1t1NewGoIntoOpeninNewWindowOpenTypeHierarchyF4ShowInAlt+Shift+WBlCopyCtrl+C聆CopyQualifiedName信PasteCtrl+VXDeleteDeleteBuildPath1福LinkSource.期NewSourceFolder.SourceRefactorAlt+Shift+SAlt+Shift+T泮UseasSourceFolder5AddExternalArchives.国AddLibra

4、ries.jGaiilaImport.BJIExport.RefreshF5CloseProjectCloseUnrelatedProjectsAssignWorkingSets.第ConfigureBuildPath.kRunAsDebugAsTeamCompareWithRestorefromLocalHistory.MyEdipsePDEToolsasksWebBrowser曰ConsolJavaApplicationC:ProgramFiles5凹PropertiesAlt+Enter技术博客8Mg叶叶Propertiesforjacobtypefiltertext|0Source|日

5、Proje匚ts|BiLibraries%Crete1rdJARsandclassfoldersonthebuildpath:吧jacobjar-F:TacobBin_i7-JavaEE5Libraries&JRESystemLibraryMyEcipse7-OML下面这一步非常重要,就是拷贝jacob目录中jacob-1.14.3-x86.dll文件到系统环境变量目录中一般情况就放在当前jdk中bin目录下。JavaBuildPathResourceBuildersJavaBuildPathJavaCodeStyle:JavaCompilerJavaEditorJavadocL口匚a

6、tin。MyEdipseProjectReferencesRun/DebuqSettingsbin文件(日编辑(日查看垣)收藏工艮(工)帮助出)。翩管口文联厘?_jCProTamFiesJavajdkl.6.0bri创建日期:20093-017:IS 大小:92.0KB这里有一个MSWordManager类,是jacob官方发布的工具类,里面有大多数Java操彳MSOffice的工具packagecom.test;importjava.sql.Connection;importjava.sql.DriverManager;importjava.sql.ResultSet;importjava.

7、sql.Statement;importjava.util.ArrayList;importjava.util.List;importcom.jacob.activeX.ActiveXComponent;.Dispatch;.Variant;地址)文件和文件夹任务,重命名这个文件移劭这个文件复制这个文件将这个文件发布到 Web一1以电子邮件祁式发送jhtST件X 册除这个文件其它位置,jdcl.6.0I 口我的文档 L 我的电脑 v 网上郃居详细信息详细信息, ,iacab.dll应用程序扩屣修改日期:20n年 g 月 22 日,17:36大小:92.0KBJpa2QQIJaw(TM)Plat

8、formIISunMicrosystemsjpofcytodIJava(TM)PlatformISunMlcrosyJmidSEbinaryInc.SEbinaryInc.SEbinaryIncSEbinaryInc.servertoolIJava(TM)PlatformI1SunMicrosystemSjunpackSOOIJavafTM)PlatformISunMicrosystemswsirpartIJava(TM)SunMicrosySemSjSEbinaryInc.SEbinaryIncSEbinaryInc.packagerJavaBeans(TM)PackagerSunMicro

9、systemsIncrmlcJavafTM)PiatfiormSEbrioSunMcrosystenrfi,IncrrmiregistryJava(TM)PlatformSEbriaSunMcrosystenre,Inc,serialverJava(TM)PUtformSEbnaSunMicrosystems,InctnameservJavaCnl)PtatfiormSEbinaSunMfcrosystemSjIncrwsgenJava(TM)PlatformSEbinaSunMicrosystemsInc,可匚Java(TM)PlatformSEbnaSunMicrosystems,Incu

10、JliilyuujschemagenJava(TM)PlatformSunMicrosystems,publicclassMSWordManager/word文档privateDispatchdoc;/word运行程序对象privateActiveXComponentword;/所有word文档集合privateDispatchdocuments;/选定的范围或插入点privateDispatchselection;privatebooleansaveOnExit=true;*paramvisible为true表示word应用程序可见*/publicMSWordManager(booleanv

11、isible)if(word=null)word=newActiveXComponent(Word.Application);word.setProperty(Visible,newVariant(visible);)if(documents=null)documents=word.getProperty(Documents).toDispatch();)*设置退出时参数*paramsaveOnExitbooleantrue-退出时保存文件,false-退出时不保存文件*/publicvoidsetSaveOnExit(booleansaveOnExit)this.saveOnExit=sav

12、eOnExit;)*创建一个新的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=Disp

13、atch.get(word,Selection).toDispatch();)*把选定的内容或插入点向上移动*/*/*/*/*/*/*/*/*/*/*/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

14、=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,M

15、oveLeft);)*把插入点移动到文件首位置*/publicvoidmoveStart()if(selection=null)selection=Dispatch.get(word,Selection).toDispatch();Dispatch.call(selection,HomeKey,)publicvoidmoveEnd()if(selection=null)selection=Dispatch.get(word,Selection).toDispatch();/*/*MoveRight);/*/*/*/*/*/*newVariant(6);Dispatch.call(selecti

16、on,EndKey,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);

17、/向前查找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*/publicbooleanreplaceTex

18、t(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(select

19、ion,未查找到文本/*/*/*/*MoveRight);/*/*paramtoFindTextparamimagePathreturn要查找的字符串图片路径*/publicbooleanreplaceImage(StringtoFindText,StringimagePath)InLineShapes).toDispatch(),imagePath);/*/*全局替换图片*if(!find(toFindText)returnfalse;Dispatch.call(Dispatch.get(selection,AddPicture,returntrue;paramtoFindTextparam

20、imagePath查找字符串图片路径*/publicvoidreplaceAllImage(Stringwhile(find(toFindText)toFindText,StringimagePath)InLineShapes).toDispatch(),Dispatch.call(Dispatch.get(selection,AddPicture,imagePath);Dispatch.call(selection,MoveRight);/*/*在当前插入点插入图片*paramimagePath图片路径*/publicvoidinsertImage(StringimagePath)Dispa

21、tch.call(Dispatch.get(selection,InLineShapes).toDispatch(),AddPicture,imagePath);/*/*合并单元格*paramtableIndexparamfstCellRowIdxparamfstCellColIdxparamsecCellRowIdxparamsecCellColIdx*/publicvoidmergeCell(inttableIndex,intfstCellRowIdx,intfstCellColIdx,intsecCellRowIdx,intsecCellColIdx)Tables).toDispatch

22、();Variant(tableIndex)/所有表格Dispatchtables=Dispatch.get(doc,/要填充的表格Dispatchtable=Dispatch.call(tables,Item,new.toDispatch();DispatchfstCell=Dispatch.call(table,Cell,newVariant(fstCellRowIdx),newVariant(fstCellColIdx).toDispatch();DispatchsecCell=Dispatch.call(table,Cell,newVariant(secCellRowIdx),newV

23、ariant(secCellColldx).toDispatch();Dispatch.call(fstCell,Merge,secCell);*在指定的单元格里填写数据*paramtableindex*paramcellRowidx*paramcellColldx*paramtxt*/publicvoidputTxtToCell(inttableindex,intcellRowidx,intcellColIdx,*parampos*/publicvoidcopy(StringtoCopyText)moveStart();if(this.find(toCopyText)Dispatchtext

24、Range=Dispatch.get(selection,Range).toDispatch();Dispatch.call(textRange,Copy);*在当前文档粘帖剪贴板数据*parampos*/publicvoidpaste(Stringpos)moveStart();if(this.find(pos)DispatchtextRange=Dispatch.call(textRange,Paste);Stringtxt)/所有表格Dispatchtables=Dispatch.get(doc,Tables).toDispatch();/要填充的表格Dispatchtable=Disp

25、atch.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);/*/*在当前文档拷贝数据*/*/*/*/*Dispatch.get(selection,Range).toDispatch();/*/*Dispatchtables=D

26、ispatch.get(doc,Dispatchtable=Dispatch.call(tables,Item,new.toDispatch();Dispatchrange=Dispatch.get(table,Dispatch.call(range,Copy);if(this.find(pos)DispatchtextRange=Dispatch.get(selection,Range).toDispatch();Dispatch.call(textRange,Paste);*在当前文档末尾拷贝来自另一个文档中的段落* paramanotherDocPath另一个文档的磁盘路径* param

27、tableindex被拷贝的段落在另一格文档中的序号(从1开始)* /publicvoidcopyParagraphFromAnotherDoc(StringanotherDocPath,intparagraphIndex)DispatchwordContent=Dispatch.get(doc,Content).toDispatch();/取得当前文档的内容Dispatch.call(wordContent,InsertAfter,$selection$);/插入特殊符定位插入点copyParagraphFromAnotherDoc(anotherDocPath,paragraphIndex

28、,$selection$);*在当前文档指定的位置拷贝来自另一个文档中的段落* paramanotherDocPath另一个文档的磁盘路径* paramtableindex被拷贝的段落在另一格文档中的序号(从1开始)* parampos当前文档指定的位置*/publicvoidcopyParagraphFromAnotherDoc(StringanotherDocPath,intparagraphIndex,Stringpos)Dispatchdoc2=null;trydoc2=Dispatch.call(documents,Open,anotherDocPath).toDispatch();

29、Dispatchparagraphs=Dispatch.get(doc2,Paragraphs).toDispatch();Dispatchparagraph=Dispatch.call(paragraphs,Item,newVariant(paragraphindex).toDispatch();Dispatchrange=Dispatch.get(paragraph,Range).toDispatch();Dispatch.call(range,Copy);if(this.find(pos)DispatchtextRange=Dispatch.get(selection,Range).to

30、Dispatch();Dispatch.call(textRange,Paste);catch(Exceptione)/*/*Tables).toDispatch();Variant(tablelndex)Range).toDispatch();/*/*e.printStackTrace();finallyif(doc2!=null)Dispatch.call(doc2,Close,newVariant(saveOnExit);doc2=null;*在当前文档指定的位置拷贝来自另一个文档中的表格* paramanotherDocPath另一个文档的磁盘路径* paramtableindex被拷

31、贝的表格在另一格文档中的序号(从1开始)* parampos当前文档指定的位置*/publicvoidcopyTableFromAnotherDoc(StringanotherDocPath,inttableindex,Stringpos)Dispatchdoc2=null;trydoc2=Dispatch.call(documents,Open,anotherDocPath).toDispatch();Dispatchtables=Dispatch.get(doc2,Tables).toDispatch();Dispatchtable=Dispatch.call(tables,item,ne

32、wVariant(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();finallyif(doc2!=null)Dispatch.call(doc2,C

33、lose,newVariant(saveOnExit);doc2=null;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.

34、find(pos)DispatchtextRange=Dispatch.get(selection,Range).toDispatch();Dispatch.call(textRange,Paste);/*/*/*/*catch(Exceptione)e.printStackTrace();finallyif(doc2!=null)Dispatch.call(doc2,Close,newVariant(saveOnExit);doc2=null;Java操作MicrosoftWord之jacob(2)(接上)/二三/*创建表格*parampos位置*paramcols歹U数*paramrows

35、行数*/publicvoidcreateTable(intnumCols,intnumRows)/(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).toDisp

36、atch();Dispatch.call(selection,MoveRight);moveEnd();/*paramtableindexword文件中的第N张表(从1开始)三在指定行前面增加行/*/*paramrowlndex指定行的序号(从1开始)*/publicvoidaddTableRow(inttablelndex,introwlndex)/所有表格Dispatchtables=Dispatch.get(doc,Tables).toDispatch();/要填充的表格Dispatchtable=Dispatch.call(tables,ltem,new*/publicvoidaddR

37、ow(inttableindex)Dispatchtables=Dispatch.get(doc,Tables).toDispatch();/要填充的表格Variant(tablelndex)Variant(rowlndex).toDispatch();/表格的所有行Dispatchrows=Dispatch.get(table,Rows).toDispatch();Dispatchrow=Dispatch.call(rows,ltem,new.toDispatch();Dispatch.call(rows,Add,newVariant(row);/*/*在第1行前增加一行*paramtabl

38、elndexword文档中的第N张表(从1开始)*/publicvoidaddFirstTableRow(inttablelndex)/所有表格Dispatchtables=Dispatch.get(doc,Tables).toDispatch();/要填充的表格Dispatchtable=Dispatch.call(tables,ltem,newVariant(tablelndex).toDispatch();/表格的所有行Dispatchrows=Dispatch.get(table,Rows).toDispatch();Dispatchrow=Dispatch.get(rows,Firs

39、t).toDispatch();Dispatch.call(rows,Add,newVariant(row);/*/*在最后1行前增加一行*paramtablelndex*word文档中的第N张表(从1开始)Variant(tablelndex)*/publicvoidaddLastTableRow(inttablelndex)/所有表格Dispatchtables=Dispatch.get(doc,Tables).toDispatch();/要填充的表格Dispatchtable=Dispatch.call(tables,ltem,new.toDispatch();/表格的所有行Dispat

40、chrows=Dispatch.get(table,Rows).toDispatch();Dispatchrow=Dispatch.get(rows,Last).toDispatch();Dispatch.call(rows,Add,newVariant(row);/*/*Dispatchtable=Dispatch.call(tables,Item,newVariant(tableindex).toDispatch();/表格的所有行Dispatchrows=Dispatch.get(table,Rows).toDispatch();Dispatch.call(rows,Add);)*增加一

41、列*paramtableindexword文档中的第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).toDisp

42、atch();Dispatch.call(cols,AutoFit);)*在指定列前面增加表格的列* paramtableindexword文档中的第N张表(从1开始)* paramcolindex指定列的序号(从1开始)*/publicvoidaddTableCol(inttableindex,intcolindex)/所有表格Dispatchtables=Dispatch.get(doc,Tables).toDispatch();/要填充的表格Dispatchtable=Dispatch.call(tables,item,newVariant(tableindex).toDispatch(

43、);/表格的所有行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);)/*

44、/*/*/*/*paramtableindexword文档中的第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=Dispatc

45、h.get(cols,First).toDispatch();Dispatch.call(cols,Add,col).toDispatch();Dispatch.call(cols,AutoFit);)* 在最后一列前增加一列*paramtableindexword文档中的第N张表(从1开始)*/publicvoidaddLastTableCol(inttableindex)Dispatchtables=Dispatch.get(doc,Tables).toDispatch();/要填充的表格Dispatchtable=Dispatch.call(tables,item,newVariant(

46、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=

47、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);)intcount=Dispatch.get(tables,Count).toInt();VariantvMacroName=newVariant(Normal.NewM

48、acros.tableFit);VariantvParam=newVariant(param1);Variantpara=newVariantvMacroName;for(inti=0;i<para.length;i+)/*/*/*/*/*/*Item,newVariant(ich();tableFitContent);+1)/*/*设置当前选定内容的字体*Dispatchtable=Dispatch.call(tables,Dispatch.call(table,Select);Dispatch.call(word,Run,.toDispat* paramboldSize* p

49、aramitalicSize* paramunderLineSize下戈U线* 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(fon

温馨提示

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

评论

0/150

提交评论