使用java将word文档转化为html格式.docx

上传人:b****4 文档编号:4475327 上传时间:2022-12-01 格式:DOCX 页数:27 大小:21.18KB
下载 相关 举报
使用java将word文档转化为html格式.docx_第1页
第1页 / 共27页
使用java将word文档转化为html格式.docx_第2页
第2页 / 共27页
使用java将word文档转化为html格式.docx_第3页
第3页 / 共27页
使用java将word文档转化为html格式.docx_第4页
第4页 / 共27页
使用java将word文档转化为html格式.docx_第5页
第5页 / 共27页
点击查看更多>>
下载资源
资源描述

使用java将word文档转化为html格式.docx

《使用java将word文档转化为html格式.docx》由会员分享,可在线阅读,更多相关《使用java将word文档转化为html格式.docx(27页珍藏版)》请在冰豆网上搜索。

使用java将word文档转化为html格式.docx

使用java将word文档转化为html格式

MicrosoftWord文档到HTML文档的转化(环境配置)

在该部分的转化中,使用了JACOBProject(AJava-COMBridge)一种Java到com的连接桥来操作基于com模型的Microsoftword文档。

使用Jocob基本上可以完全操作word文档,在该例中主要使用了JACOB来操作word文档另存为HTML文档。

首先在doc描述文件、JACOB.dll、JACOB.jar。

填加JACOB.dll到系统//windows//system32//目录下或者安装Java的bin目录下;在eclipse环境下填加JACOB.jar到自己的Java库中,填加方法是在包资源管理器下点击鼠标右键选择构建路径//配置构建路径//Java构建路径//库//添加外部jar然后在弹出窗口中选择外部jar路径,也就是JACOB.jar的所在的文件夹。

做好以上的环境配置工作后,就可以正确使用JACOB来操作Microsoftword文档了。

在该例中,主要使用了JACOB来操作word另存为HTML文件。

核心操作代码见下文。

使用java将word文档转化为html格式(续)

2008-07-0623:

13

MicrosoftWord文档到HTML文档的转化(实现代码)

importcom.jacob.activeX.*;

import.*;

importJava.io.File;

publicclassWordToHtml{

publicstaticvoidconvertDocToHtml(StringinputFile,StringoutputFile){

booleanflag=false;

//打开Word应用程序

ActiveXComponentapp=newActiveXComponent("Word.Application");

try{

//设置word不可见

app.setProperty("Visible",newVariant(false));

//打开word文件

Dispatchdoc1=app.getProperty("Documents").toDispatch();

Dispatchdoc2=Dispatch.invoke(doc1,"Open",Dispatch.Method,newObject[]{inputFile,newVariant(false),newVariant(true)},newint[1]).toDispatch();

//newVariant(i)参量用来控制word文档另存为的目标文档格;i=7时word存为txt文件;i=8时word存为HTML文件

Dispatch.invoke(doc2,"SaveAs",Dispatch.Method,newObject[]{outputFile,newVariant(8)},newint[1]);

//关闭word

Variantf=newVariant(false);Dispatch.call(doc2,"close",f);

flag=true;

}catch(Exceptione){

e.printStackTrace();

}finally{

app.invoke("Quit",newVariant[]{});

}

if(flag==true){

System.out.println(inputFile+"-->TransformeSuccessfully");

}else{

System.out.println(inputFile+"-->TransformFailed");

}

}

}

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

                       Stringtxt){

               //所有表格

               Dispatchtables=Dispatch.get(doc,"Tables").toDispatch();

               //要填充的表格

               Dispatchtable=Dispatch.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);

       }

       /***//**

        *在当前文档拷贝数据

        *    

        *@parampos

        */

       publicvoidcopy(StringtoCopyText){

               moveStart();

               if(this.find(toCopyText)){

                       DispatchtextRange=Dispatch.get(selection

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

当前位置:首页 > PPT模板 > 其它模板

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

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