java操作word资料文档

整理文档很辛苦,赏杯茶钱您下走!

免费阅读已结束,点击下载阅读编辑剩下 ...

阅读已结束,您可以下载文档离线阅读编辑

资源描述

JAVA操作wordJava操作MicrosoftWord之jacob(1)现在我们一起来看看,用J1ava如何操作MicrosoftWord。jacob,官网是这是一个开源的工具。最新版本1.7官方的解释是:TheJACOBProject:AJAva-COMBridge这是官方对下载文件的说明:jacob.jar:aJARfileforthejavaclasseswhichyoumustaddtoyourCLASSPATH.Thepackagenamesreplacecom.mswithcom.jacob(forexamplecom.ms.com.Variantmapstocom.jacob.com.Variant.jacob.dll:asmallWin32DLLwhichyoumustaddtoyourPATH.samples:providedinJavasourceandcompiledformtodemonstratevariousfeaturesoftheproduct.Inparticular,asetofwrapperclassesforMicrosoft®ADOareprovidedassamples.开发环境:JDK1.6MyEclipseEnterpriseWorkbenchVersion:7.0Milestone-1Tomcat5.5.27现在MyEclipse中新建一个项目jacob,将jacob的jar包放到该项目的类库中。我的jacob版本是1.14.3。下面这一步非常重要,就是拷贝jacob目录中jacob-1.14.3-x86.dll文件到系统环境变量目录中一般情况就放在当前jdk中bin目录下。。这里有一个MSWordManager类,是jacob官方发布的工具类,里面有大多数Java操作MSOffice的工具。packagecom.test;importjava.sql.Connection;importjava.sql.DriverManager;importjava.sql.ResultSet;importjava.sql.Statement;importjava.util.ArrayList;importjava.util.List;importcom.jacob.activeX.ActiveXComponent;importcom.jacob.com.Dispatch;importcom.jacob.com.Variant;publicclassMSWordManager{//word文档privateDispatchdoc;//word运行程序对象privateActiveXComponentword;//所有word文档集合privateDispatchdocuments;//选定的范围或插入点privateDispatchselection;privatebooleansaveOnExit=true;/***//****@paramvisible为true表示word应用程序可见*/publicMSWordManager(booleanvisible){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=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));}publicvoidmoveEnd(){if(selection==null)selection=Dispatch.get(word,Selection).toDispatch();Dispatch.call(selection,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);//向前查找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*/publicvoidmergeCel

1 / 17
下载文档,编辑使用

©2015-2020 m.777doc.com 三七文档.

备案号:鲁ICP备2024069028号-1 客服联系 QQ:2149211541

×
保存成功