jacob完成word转html功能及注意事项

版权声明:本文为博主原创文章,遵循 CC 4.0 by-sa 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://blog.csdn.net/iteye_20372/article/details/82403375

 

1、在网上下载jacob-1.17-M2,里面包含jacob.jar,jacob-1.17-M2-x86.dll,jacob-1.17-M2-x64.dll以及docs文件夹,

 

2、把jar包放到项目的lib下,把相应操作系统的dll文件放到C:\WINDOWS\system32下,同时放到JDK 和JRE 目录下的lib里

 

3、配置好后编写转换类:

package com.lenova.tools;

import java.io.File;
import com.jacob.com.*;
import com.jacob.activeX.*;
import com.lenova.database.Config;

public class WordToHtml {
   
 public static boolean wordToHtml (String inPath,String toPath) {
  
  //启动word
  ActiveXComponent axc = new ActiveXComponent("Word.Application");
  
  boolean flag = false;
  
  try {
   //设置word不可见
   axc.setProperty("Visible",new Variant(false));
   
   Dispatch docs = axc.getProperty("Documents").toDispatch();
   
   //打开word文档
   Dispatch doc = Dispatch.invoke(
     docs, 
     "Open", 
     Dispatch.Method, 
     new Object[]{inPath,new Variant(false), new Variant(true)},
     new int[1]).toDispatch();
   
   //作为html格式保存到临时文件
   Dispatch.invoke(doc, "SaveAs", Dispatch.Method, new Object[] {
     toPath, new Variant(8) }, new int[1]);
   
   Variant f = new Variant(false);
   Dispatch.call(doc, "Close", f);
   flag = true;
   return flag;
   
  } catch (Exception e) {
   e.printStackTrace();
   return flag;
  } finally{
   axc.invoke("Quit", new Variant[] {});
  }
 }
 
 public static void main(String[] args) {
  
  WordToHtml.wordToHtml("F://WordToHtml//hello.doc", "F://WordToHtml//html//hello.htm");
  
 }
}

 

4、遇到的问题:

在使用中会遇到当服务重启时出现UnsatisfiedLinkError错误,详细如下提示:

jacob.dll already loaded in another classloader

Java API 表明:JVM只允许一个默认的ClassLoader来load native library,同时并不提供专门的API来unload一个 loaded native library,因此无法在我们的重启Web应用的代码中来手工清除已经load的jacob,最好的办法是重启webserver,而不是只重启该项目的服务

 

5、暂时研究到这以后有进展再更新...

展开阅读全文

jacobword转换为html报错

03-18

package Test;rnrnimport com.jacob.com.*;rnimport com.jacob.activeX.*;rnrnpublic class WordToHtml rn /**rn *文档转换函数rn * rn * @param docfilern * word文档的绝对路径加文件名(包含扩展名)rn *@param htmlfilern * 转换后的html文件绝对路径和文件名(不含扩展名)rn */rn public static final int WORD_HTML = 8;rnrn public static final int WORD_TXT = 7;rnrn public static final int EXCEL_HTML = 44;rnrn /**rn * WORD转HTMLrn * rn * @param docfilern * WORD文件全路径rn * @param htmlfilern * 转换后HTML存放路径rn */rn public static void wordToHtml(String docfile, String htmlfile) rn ActiveXComponent app = new ActiveXComponent("Word.Application"); // 启动wordrn try rn app.setProperty("Visible", new Variant(false));rn Dispatch docs = app.getProperty("Documents").toDispatch();rn Dispatch doc = Dispatch.invoke(rn docs,rn "Open",rn Dispatch.Method,rn new Object[] docfile, new Variant(false),rn new Variant(true) , new int[1]).toDispatch();rn Dispatch.invoke(doc, "SaveAs", Dispatch.Method, new Object[] rn htmlfile, new Variant(WORD_HTML) , new int[1]);rn Variant f = new Variant(false);rn Dispatch.call(doc, "Close", f);rn catch (Exception e) rn e.printStackTrace();rn finally rn app.invoke("Quit", new Variant[] );rn rn rnrn public static void main(String[] strs) rn WordToHtml.wordToHtml("e:/中百便民采购资金管理数据接口定义_V1.0.doc", "c:");rn rnrnrnrn报错信息:rncom.jacob.com.ComFailException: Invoke of: SaveAsrnSource: Microsoft WordrnDescription: rnrn at com.jacob.com.Dispatch.invokev(Native Method)rn at com.jacob.com.Dispatch.invokev(Dispatch.java:804)rn at com.jacob.com.Dispatch.invoke(Dispatch.java:483)rn at Test.WordToHtml.wordToHtml(WordToHtml.java:39)rn at Test.WordToHtml.main(WordToHtml.java:55)rnrn注:jar包已经导入到项目中,并且jacob.dll文件在项目的lib下面和tomcat 6.0的bin以及lib目录下都放入了rnrn请问是什么情况? 论坛

没有更多推荐了,返回首页