如何用JAVA将Word文档转换为HTML

作者:Kenny Lee | 来源:http://www.topxp.org | 2008-05-11
<script type="text/javascript"> </script> <script type="text/javascript" src="http://pagead2.googlesyndication.com/pagead/show_ads.js"> </script><script> window.google_render_ad(); </script> name="google_ads_frame" src="http://pagead2.googlesyndication.com/pagead/ads?client=ca-pub-1721385695763485&dt=1222062348617&lmt=1210496551&prev_fmts=728x90_as&output=html&slotname=1458385250&correlator=1222062348537&url=http%3A%2F%2Fwww.topxp.org%2Fac_402881e419d71b8e0119d7327a270001.html&ref=http%3A%2F%2Fwww.topxp.org%2Fotheropensource.html&frm=0&cc=100&ga_vid=144753967.1222062349&ga_sid=1222062349&ga_hid=2132901391&flash=9.0.124&u_h=800&u_w=1280&u_ah=738&u_aw=1280&u_cd=32&u_tz=480&u_his=1&u_java=true&u_nplug=11&u_nmime=38" marginwidth="0" marginheight="0" vspace="0" hspace="0" allowtransparency="true" scrolling="no" width="250" frameborder="0" height="250">

前言

鉴于JACOB在各种系统环境下容易出现各种各样的问题,因此,本文介绍的是使用JCom组件来将Word文档自动转换为HTML。它非常容易使用。

JCom官方网址在:http://sourceforge.net/projects/jcom/

运行JCom需要一个jar包以及一个dll文件,从官方网站可以下载这两个文件,下文所描述的例子,是基于jcom- 2.2.4 ,在WindowsXP SP2office2003JDK1.5的环境下测试通过。

 

JCom基本结构

 

利用JCom转换Word文件

1、  首先要加入jcom.jar到类路径

2、  其次要将jcom.dll加入到path环境变量中

3、  需配置JAVA_HOME环境变量指向你的JDK

运行如下代码即可:

       //首先转换为HTML文件,存放到临时目录

       ReleaseManager rm = new ReleaseManager();

       String userdir = System.getProperty("user.dir");

       String tempHtmlFile = userdir + "/" + System.currentTimeMillis() + ".html";

       try {

           IDispatch wdApp = new IDispatch(rm, "Word.Application");

           wdApp.put("Visible", new Boolean(false));

           IDispatch wdDocuments = (IDispatch)wdApp.get("Documents");

           IDispatch wdDocument = (IDispatch)wdDocuments.method("Open",

                  new Object[]{wordDocumentFilePath});

           //转换为HTML文件

           wdDocument.method("SaveAs", new Object[]{tempHtmlFile,8});

           wdApp.method("Quit", null);

           logger.info("文件已临时存放到:"+tempHtmlFile);

       } catch (Exception e) {

           e.printStackTrace();

       }finally{

           rm.release();

       }

这将会自动将Word转换为HTML文件,其中的图片也被自动保存到了相应的目录(xxx.files目录)下。掌握了这个基本的核心原理,那么其它的操作也就不再困难了。本站的文章自动发布系统就是采用了JCom来做的,本站所有的文章都是在Word中编辑,随意插入各种格式和图片,最后将Word一发布(转换为HTML),即可将文本、格式、图片等一次性发布到网站上。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 3
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值