通过jacob实现word转为HTML

jopen 13年前
     <p style="text-indent:10.5pt;margin:0cm 0cm 0pt;" class="MsoNormal"><span style="font-size:small;"><span>这几天研究</span><span lang="EN-US"><span style="font-family:Calibri;">jacob</span></span><span>原本的需求是将</span><span lang="EN-US"><span style="font-family:Calibri;">word</span></span><span>显示到</span><span lang="EN-US"><span style="font-family:Calibri;">FCK</span></span><span>中,于是就想到用</span><span lang="EN-US"><span style="font-family:Calibri;">jacob</span></span><span>去做。<span style="letter-spacing:0.4pt;">它允许在</span></span><span style="letter-spacing:0.4pt;" lang="EN-US"><span style="font-family:Calibri;">java</span></span><span>中调用</span><span style="letter-spacing:0.4pt;" lang="EN-US"><span style="font-family:Calibri;">com</span></span><span>接口自动组件,它使用</span><span style="letter-spacing:0.4pt;" lang="EN-US"><span style="font-family:Calibri;">JNI</span></span><span>(本地调用程序)来进行本地调用</span><span style="letter-spacing:0.4pt;" lang="EN-US"><span style="font-family:Calibri;">COM</span></span><span>库。它可运行在</span><span style="letter-spacing:0.4pt;" lang="EN-US"><span style="font-family:Calibri;">x86</span></span><span>和支持</span><span style="letter-spacing:0.4pt;" lang="EN-US"><span style="font-family:Calibri;">32</span></span><span>位和</span><span style="letter-spacing:0.4pt;" lang="EN-US"><span style="font-family:Calibri;">64</span></span><span>位</span><span style="letter-spacing:0.4pt;" lang="EN-US"><span style="font-family:Calibri;">Java</span></span></span><span style="font-size:small;"><span style="letter-spacing:0.4pt;" lang="EN-US"><span lang="EN-US">虚拟机</span><span style="font-family:Calibri;"> </span></span></span><span style="font-size:small;"><span style="letter-spacing:0.4pt;" lang="EN-US"><span style="font-family:Calibri;">X64</span></span><span>的环境。</span></span></p>    <p style="margin:0cm 0cm 0pt;" class="MsoNormal"><span style="letter-spacing:0.4pt;" lang="EN-US"><span style="font-family:Calibri;font-size:small;"> </span></span></p>    <p style="text-indent:5.25pt;margin:0cm 0cm 0pt;" class="MsoNormal"><span style="font-size:small;"><span>首先下载</span><span lang="EN-US"><span style="font-family:Calibri;">Jacob</span></span><span>包,</span><span lang="EN-US"><span style="font-family:Calibri;">JDK1.5</span></span><span>以上需要使用</span><span lang="EN-US"><span style="font-family:Calibri;">Jacob1.9</span></span><span>版本(</span><span lang="EN-US"><span style="font-family:Calibri;">JDK1.6</span></span><span>尚未测试)</span></span></p>    <p style="margin:0cm 0cm 0pt 21pt;" class="MsoNormal"><span style="font-size:small;"><span lang="EN-US"><span style="font-family:Calibri;">1</span></span><span>、将压缩包解压后,</span><span lang="EN-US"><span style="font-family:Calibri;">Jacob.jar</span></span><span>添加到</span><span lang="EN-US"><span style="font-family:Calibri;">Libraries</span></span><span>中;</span></span></p>    <p style="margin:0cm 0cm 0pt 21pt;" class="MsoNormal"><span style="font-size:small;"><span lang="EN-US"><span style="font-family:Calibri;">2</span></span><span>、将</span><span lang="EN-US"><span style="font-family:Calibri;">Jacob.dll</span></span><span>放至“</span><span lang="EN-US"><span style="font-family:Calibri;">WINDOWS\SYSTEM32</span></span><span>”下面。</span></span></p>    <p style="margin:0cm 0cm 0pt;" class="MsoNormal"><span lang="EN-US"><span style="font-family:Calibri;font-size:small;"> </span></span></p>    <p style="margin:0cm 0cm 0pt;" class="MsoNormal"><span style="font-size:small;"><span>需要注意的是:</span><span lang="EN-US"><span style="font-family:Calibri;"> </span></span></span></p>    <p style="margin:0cm 0cm 0pt;" class="MsoNormal"><span style="font-size:small;"><span>【使用</span><span lang="EN-US"><span style="font-family:Calibri;">IDE</span></span><span>启动</span><span lang="EN-US"><span style="font-family:Calibri;">Web</span></span><span>服务器时,系统读取不到</span><span lang="EN-US"><span style="font-family:Calibri;">Jacob.dll</span></span><span>,例如用</span><span lang="EN-US"><span style="font-family:Calibri;">MyEclipse</span></span><span>启动</span><span lang="EN-US"><span style="font-family:Calibri;">Tomcat</span></span><span>,就需要将</span><span lang="EN-US"><span style="font-family:Calibri;">dll</span></span><span>文件</span><span lang="EN-US"><span style="font-family:Calibri;">copy</span></span><span>到</span><span lang="EN-US"><span style="font-family:Calibri;">MyEclipse</span></span><span>安装目录的“</span><span lang="EN-US"><span style="font-family:Calibri;">jre\bin</span></span><span>”下面。</span><span lang="EN-US"><span style="font-family:Calibri;"> </span></span></span></p>    <p style="margin:0cm 0cm 0pt;" class="MsoNormal"><span style="font-size:small;"><span>一般系统没有加载到</span><span lang="EN-US"><span style="font-family:Calibri;">Jacob.dll</span></span><span>文件时,报错信息为:“</span><span style="color:red;"><span style="font-family:Calibri;">java.lang.UnsatisfiedLinkError: no jacob in java.library.path</span></span><span>”】</span></span></p>    <p style="margin:0cm 0cm 0pt;" class="MsoNormal"><span lang="EN-US"><span style="font-family:Calibri;font-size:small;"> </span></span></p>    <p style="margin:0cm 0cm 0pt;" class="MsoNormal"><span style="font-size:small;"><span>下面是代码:</span></span></p>    <pre class="brush:java; toolbar: true; auto-links: false;">package com.gra.util;   import com.jacob.activeX.ActiveXComponent; import com.jacob.com.Dispatch; import com.jacob.com.Variant;   public class JacobUtil {           public static final int WORD_HTML = 8;           public static final int WORD_TXT = 7;           public static final int EXCEL_HTML = 44;              /**            * WORD转HTML            *             * @param docfile            *            WORD文件全路径            * @param htmlfile            *            转换后HTML存放路径            */           public void wordToHtml(String docfile, String htmlfile) {                     ActiveXComponent app = new ActiveXComponent("Word.Application"); // 启动word                     try {                              // 设置word不可见                              app.setProperty("Visible", new Variant(false));                              //获得documents对象                              Dispatch docs = (Dispatch) app.getProperty("Documents")                                                 .toDispatch();                              //打开文件                              Dispatch doc = Dispatch.invoke(                                                 docs,                                                 "Open",                                                 Dispatch.Method,                                                 new Object[] { docfile, new Variant(false),                                                                   new Variant(true) }, new int[1]).toDispatch();                              //保存新的文件                              Dispatch.invoke(doc, "SaveAs", Dispatch.Method, new Object[] {                                                 htmlfile, new Variant(WORD_HTML) }, new int[1]);                              Variant f = new Variant(false);                              Dispatch.call(doc, "Close", f);                     } catch (Exception e) {                              e.printStackTrace();                     } finally {                              app.invoke("Quit", new Variant[] {});                     }           } }</pre>    <p></p> 测试代码:    <pre class="brush:java; toolbar: true; auto-links: false;">public class JunitTest {      @Test      public void test2() {         JacobUtil ju = new JacobUtil();         String docfile = "D:\\2012.doc";         String htmlfile = "D:\\2012.html";         ju.wordToHtml(docfile, htmlfile);      }  }</pre>