jacob環境配置:利用jacob操做word的配置和示例

配置說明:

       一、解壓文件:jacob1.1.5-M4.zip ,copy jacob.jar文件加入到classpath 和項目lib下;

       二、 copy jacob-1.15-M4-x86.dll 放在java jdk bin目錄下,系統目錄/WINDOWS/system32目錄下,若是服務器寫入WORD報錯能夠檢查一下服務器目錄下/bin下是否有jacob- 1.15-M4-x86.dll文件;
      jacob對應不一樣的JDK有不一樣的版本,此jacob版本(固然也有人說1.9版本和JDK1.6能夠,可是沒有具體試過,有興趣的朋友不妨能夠試如下)對應JDK1.6,做者在具體項目中實際用到,肯定沒問題。。

如下是一個測試操做word的實例:僅供參考java

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

public class JacobTest {

    // 聲明一個word對象
    private ActiveXComponent objWord;
    // 聲明四個word組件
    private Dispatch custDocprops;
    private Dispatch builtInDocProps;
    private Dispatch document;
    private Dispatch wordObject;

 public JacobTest() {
 
 }

/**
* 打開word文擋
*/
     public void open(String filename) {
        // 建立一個word對象
        objWord = new ActiveXComponent("Word.Application");
        // 爲wordobject組件附值
        wordObject = (Dispatch) (objWord.getObject()); // 改了這裏
        // 生成一個只讀方式的word文擋組件
        Dispatch.put(wordObject, "Visible", new Variant(false));
        // 獲取文擋屬性
        Dispatch documents = objWord.getProperty("Documents").toDispatch();
        // 打開激活文擋
         document = Dispatch.call(documents, "Open", filename).toDispatch();
}

public void selectCustomDocumentProperitiesMode() {
      custDocprops = Dispatch.get(document, "CustomDocumentProperties").toDispatch();
}

public void selectBuiltinPropertiesMode() {
      builtInDocProps = Dispatch.get(document, "BuiltInDocumentProperties").toDispatch();
}
/**
* 關閉文擋
*/
public void close() {
      Dispatch.call(document, "Close");
}

public String getCustomProperty(String cusPropName) {
     try {
           cusPropName = Dispatch.call((Dispatch)custDocprops,   "Item",cusPropName).toString();
     } catch (ComException e) {
           cusPropName = null;
   }
          return cusPropName;
}

public String getBuiltInProperty(String builtInPropName) {
       try {
          builtInPropName = Dispatch.call((Dispatch)builtInDocProps,"Item",  builtInPropName).toString();
       } catch (ComException e) {
          builtInPropName = null;
    }
          return builtInPropName;
}
public static void main(String[] args) {
     try {
          JacobTest jacTest = new JacobTest();
          jacTest.open("c:/s.doc");
          jacTest.selectCustomDocumentProperitiesMode();
          jacTest.selectBuiltinPropertiesMode();
          String custValue = jacTest.getCustomProperty("Information Source");
          String builtInValue =  jacTest.getBuiltInProperty("Author");
           jacTest.close();
           System.out.println("Document Val One:" + custValue);
           System.out.println("Document Author:" + builtInValue);
        } catch (Exception e) {
            System.out.println(e);
}
}
}

jacob1.1.5-M4.zip下載地址:http://115.com/file/beeqxg90#jacob-1.15-M4.zip附帶一個操做word示例代碼和一個操做文檔c#

 

     結束語:  第一次在開源中國發文,若有錯誤之處,你們能夠盡情留言進行探討。
服務器

相關文章
相關標籤/搜索