Java 将Word文档转换Html

 import java.io.File;

import com.jacob.activeX.ActiveXComponent;
import com.jacob.com.Dispatch;
import com.jacob.com.Variant;

/**
*
* @FileName FileExtracter.java
* @author dinguangx blog: http://hi.baidu.com/dinguangx
* @date <Apr 15, 2009> <2:12:26 PM> @
*       <p>
*       company: www.zzuli.edu.cn
*       </p>
* @version GDMS v1.0
*/
public class JavaConWord {

/**
* 将源word文件转换为html文件.进行转换之后,就可以在网页中直接引用这个html文件
*
* @param srcFileName
* @param destFileName
* @return
*/
public static boolean doc2Html(String srcFileName, String destFileName) {
   if (srcFileName == null || "".equals(srcFileName.trim()))
    throw new IllegalStateException("文件名不能为空");
   if (destFileName == null || "".equals(destFileName.trim()))
    throw new IllegalStateException("文件名不能为空");
   boolean flag = false;
   // 取得文件类型
   srcFileName = srcFileName.trim();
   destFileName = destFileName.trim();
   String srcFiletype = new String();
   srcFiletype = srcFileName.substring((srcFileName.length() - 3),
     srcFileName.length());
   String destFiletype = new String();
   destFiletype = destFileName.substring((destFileName.length() - 4),
     destFileName.length());
   // 判断源文件是否为doc文件
   if (!"doc".equals(srcFiletype))
    throw new IllegalStateException("错误的文件格式!");
   // 判断目录文件是否为html文件
   if (!"html".equals(destFiletype))
    throw new IllegalStateException("错误的文件格式!");
   // 打印doc文件名
   System.out
     .println(srcFileName.substring(0, (srcFileName.length() - 4)));
   // 启动word
   ActiveXComponent app = new ActiveXComponent("Word.Application");
   try {
    app.setProperty("Visible", new Variant(false));
    // 设置word不可见
    Object docs = app.getProperty("Documents").toDispatch();
    Object doc = Dispatch.invoke((Dispatch) docs, "Open",
      Dispatch.Method, new Object[] { srcFileName }, new int[1])
      .toDispatch();
    // 打开word文件
    Dispatch.invoke((Dispatch) doc, "SaveAs", Dispatch.Method,
      new Object[] { destFileName, new Variant(8) }, new int[1]);
    // 作为html格式保存到临时文件
    Variant f = new Variant(false);
    Dispatch.invoke((Dispatch) doc, "Close", Dispatch.Method,
      new Object[] { f }, new int[1]);
    flag = true;
   } catch (Exception e) {
    e.printStackTrace();
   } finally {
    app.invoke("Quit", new Variant[] {});
   }
    System.out.println("转化完毕!");

   return flag;
}


public static void main(String[] args) {

   System.out.println(doc2Html("G://test.doc", "g:/test.html"));

}

}

 

还要下载jacob 建议用1.9版本以上,把jacob.jar放到工程lib包下,把jacob.dll放入操作系统C:/WINDOWS/system32下

 

关于jacob在jdk5.0出错:"no jacob in java.library.path"

这里的jacob是指jacob.dll文件,其解决办法是把jacob.dll文件拷贝到

jre/bin 目录和jre/lib/ext目录下

 

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值