java操作word内部资料.docx

上传人:b****6 文档编号:5760905 上传时间:2023-01-01 格式:DOCX 页数:30 大小:118.98KB
下载 相关 举报
java操作word内部资料.docx_第1页
第1页 / 共30页
java操作word内部资料.docx_第2页
第2页 / 共30页
java操作word内部资料.docx_第3页
第3页 / 共30页
java操作word内部资料.docx_第4页
第4页 / 共30页
java操作word内部资料.docx_第5页
第5页 / 共30页
点击查看更多>>
下载资源
资源描述

java操作word内部资料.docx

《java操作word内部资料.docx》由会员分享,可在线阅读,更多相关《java操作word内部资料.docx(30页珍藏版)》请在冰豆网上搜索。

java操作word内部资料.docx

java操作word内部资料

JAVA操作word

Java操作MicrosoftWord之jacob

(1)

现在我们一起来看看,用Java如何操作MicrosoftWord。

  jacob,官网是这是一个开源的工具。

最新版本1.7

  官方的解释是:

TheJACOBProject:

AJAva-COMBridge

  这是官方对下载文件的说明:

  jacob.jar:

aJARfileforthejavaclasseswhichyoumustaddtoyourCLASSPATH.Thepackagenamesreplacecom.mswithcom.jacob(forexample.Variantmapsto.Variant.

  jacob.dll:

asmallWin32DLLwhichyoumustaddtoyourPATH.

  samples:

providedinJavasourceandcompiledformtodemonstratevariousfeaturesoftheproduct.Inparticular,asetofwrapperclassesforMicrosoft®ADOareprovidedassamples.

  开发环境:

  JDK1.6

  MyEclipseEnterpriseWorkbenchVersion:

7.0Milestone-1

  Tomcat5.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;

import.Dispatch;

import.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

        */

       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,intc

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > 工程科技 > 电子电路

copyright@ 2008-2022 冰豆网网站版权所有

经营许可证编号:鄂ICP备2022015515号-1