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");
}
}
tip:
转ppt不如iSpring pro转换效果好