jacob将word转换为html

1.导包jacob.jar

   

2.将下面两个文件复制到C:WindowsSystem32路径下

                  

 3.代码如下

 // 8 代表word保存成html  

 public static final int WORD_HTML = 8;  

public static void main(String[] args) {
String docfile = "需要转换的文档的路径";
String htmlfile = "转换完成的路径";
test1.wordToHtml(docfile, htmlfile);
}

/**
* WORD转HTML
* @param docfile WORD文件全路径
* @param htmlfile 转换后HTML存放路径
*/
public static void wordToHtml(String docfile, String htmlfile)
{
// 启动word应用程序(Microsoft Office Word 2003)
ActiveXComponent app = new ActiveXComponent("Word.Application");
System.out.println("*****正在转换...*****");
try
{
// 设置word应用程序不可见
app.setProperty("Visible", new Variant(false));
// documents表示word程序的所有文档窗口,(word是多文档应用程序)
Dispatch docs = app.getProperty("Documents").toDispatch();
// 打开要转换的word文件
Dispatch doc = Dispatch.invoke(
docs,
"Open",
Dispatch.Method,
new Object[] { docfile, new Variant(false),
new Variant(true) }, new int[1]).toDispatch();
// 作为html格式保存到临时文件
Dispatch.invoke(doc, "SaveAs", Dispatch.Method, new Object[] {
htmlfile, new Variant(WORD_HTML) }, new int[1]);
// 关闭word文件
Dispatch.call(doc, "Close", new Variant(false));
}
catch (Exception e)
{
e.printStackTrace();
}
finally
{
//关闭word应用程序
app.invoke("Quit", new Variant[] {});
}
System.out.println("*****转换完毕********");
}

4.注意错误

Exception in thread "main" java.lang.UnsatisfiedLinkError: no jacob-1.18-x86 in java.library.path
at java.lang.ClassLoader.loadLibrary(ClassLoader.java:1886)
at java.lang.Runtime.loadLibrary0(Runtime.java:849)
at java.lang.System.loadLibrary(System.java:1088)
at com.jacob.com.LibraryLoader.loadJacobLibrary(LibraryLoader.java:184)
at com.jacob.com.JacobObject.<clinit>(JacobObject.java:110)
at tests.test1.wordToHtml(test1.java:27)
at tests.test1.main(test1.java:16)

没有启动文件

 需要在你的jdk文件复制这两个文件到C:Program Files (x86)Javajdk1.7.0_55jrein

     

 

 

 

 

 

 

原文地址:https://www.cnblogs.com/fjkgrbk/p/jacob_word-html.html