歡迎您光臨本站 註冊首頁

如何利用Java-JACOB操作WORD文檔

←手機掃碼閱讀     火星人 @ 2014-03-10 , reply:0

JACOB是一個 JAVA到微軟的COM介面的橋樑.使用JACOB允許任何JVM訪問COM對象,從而使JAVA應用程序能夠調用COM對象.如果你要對 MS Word、Excel 進行處理,JACOB 是一個好的選擇.JACOB目前已經成為sourceforge(http://sourceforge.net/projects/jacob- project/)的一個開源項目,本文使用的版本是1.10.1.

因為在項目中用到了這個技術,但是在網上沒有查到很符合題目的文章,經過我自己的探索,總結寫出了這篇文章.

這篇文章可能不能完全滿足你的要求,你也可以按照我的探索方法進行探索:參閱VBA操作Office的組件的書籍,然後參考下面的Tip完成需要的功能.文章附完整的測試代碼.

生成WORD文檔的簡單講解:

1. 初始化com的線程,非常重要,否則第二次創建com對象的時候會出現can』t co-create object異常 (參見jacob的幫助文檔),完成操作com組件后要調用 realease方法
ComThread.InitSTA();// 初始化com的線程,非常重要!!使用結束后要調用 realease方法

2. 初始化word應用程序,新建一個空白文檔,取得文檔內容對象//Instantiate objWord //Declare word object
ActiveXComponent objWord = new ActiveXComponent("Word.Application");

//Assign a local word object
Dispatch wordObject = (Dispatch) objWord.getObject();

//Create a Dispatch Parameter to show the document that is opened
Dispatch.put((Dispatch) wordObject, "Visible", new Variant(true));// new Variant(true)表示word應用程序可見
Tip:設置一個對象的屬性的時候,利用Dispatch的put方法,給屬性賦值.上面這行語句相當於vb的 wordObject.Visible = true 語句

//Instantiate the Documents Property
Dispatch documents = objWord.getProperty("Documents").toDispatch(); //documents表示word的所有文檔窗口,(word是多文檔應用程序)

//Add a new word document, Current Active Document
Dispatch document = Dispatch.call(documents, "Add").toDispatch(); // 使用Add命令創建一個新文檔,用Open命令可以打開一個現有文檔
Tip:調用一個對象的方法的時候,利用Dispatch的call方法,上面的語句相當於vb的document = documents.Add() 語句.

Dispatch wordContent = Dispatch.get(document, "Content").toDispatch(); // 取得word文件的內容
Tip:取得一個對象的成員變數(屬性)時利用Dispatch的get方法,上面的語句相當於vb的wordContent = document.Content語句

3. 取得word文檔的內容后,可以對其內容進行操作
Dispatch.call(wordContent, "InsertAfter", "這裡是一個段落的內容");//插入一個段落

4. 設置剛插入的段落的文字格式
Dispatch paragraphs = Dispatch.get(wordContent, "Paragraphs").toDispatch(); // 所有段落
int paragraphCount = Dispatch.get(paragraphs, "Count").toInt(); // 一共的段落數



// 找到剛輸入的段落,設置格式
Dispatch lastParagraph = Dispatch.call(paragraphs, "Item",
new Variant(paragraphCount)).
toDispatch(); // 一段
Dispatch lastParagraphRange = Dispatch.get(lastParagraph, "Range").
toDispatch();
Dispatch font = Dispatch.get(lastParagraphRange, "Font").toDispatch();
Dispatch.put(font, "Bold", new Variant(true)); // 設置為黑體
Dispatch.put(font, "Italic", new Variant(true)); // 設置為斜體
Dispatch.put(font, "Name", new Variant("宋體")); //
Dispatch.put(font, "Size", new Variant(12)); //小四
注意:如果想插入一個新的空白行,也需要設置段落的文字格式,否則新插入行的文字格式會於剛插入的段落的格式相同.

5. 將當前文檔保存
Dispatch.call(document, "SaveAs", new Variant("C:
abc.doc")); // 保存一個新文檔

6. 釋放COM線程
ComThread.Release();//釋放com線程.根據jacob的幫助文檔,com的線程回收不由java的垃圾回收器處理

完整測試代碼:(StudyJacob.java 附件中有本文章和java源文件)
import com.jacob.activeX.ActiveXComponent;
import com.jacob.com.Dispatch;
import com.jacob.com.Variant;
import com.jacob.com.ComThread;

public class StudyJacob {

public static void main(String[] args) {
ComThread.InitSTA();// 初始化com的線程,非常重要!!使用結束后要調用 realease方法

//Instantiate objWord //Declare word object
ActiveXComponent objWord = new ActiveXComponent("Word.Application");

//Assign a local word object
Dispatch wordObject = (Dispatch) objWord.getObject();

//Create a Dispatch Parameter to show the document that is opened
Dispatch.put((Dispatch) wordObject, "Visible", new Variant(true));// new Variant(true)表示word應用程序可見

//Instantiate the Documents Property
Dispatch documents = objWord.getProperty("Documents").toDispatch(); //documents表示word的所有文檔窗口,(word是多文檔應用程序)

//Add a new word document, Current Active Document
Dispatch document = Dispatch.call(documents, "Add").toDispatch(); // 使用Add命令創建一個新文檔,用Open命令可以打開一個現有文檔

Dispatch wordContent = Dispatch.get(document, "Content").toDispatch(); // 取得word文件的內容

Dispatch.call(wordContent, "InsertAfter", "這裡是一個段落的內容");//插入一個段落

Dispatch paragraphs = Dispatch.get(wordContent, "Paragraphs").toDispatch(); // 所有段落
int paragraphCount = Dispatch.get(paragraphs, "Count").toInt(); // 一共的段落數

// 找到剛輸入的段落,設置格式
Dispatch lastParagraph = Dispatch.call(paragraphs, "Item",
new Variant(paragraphCount)).
toDispatch(); // 一段
Dispatch lastParagraphRange = Dispatch.get(lastParagraph, "Range").
toDispatch();
Dispatch font = Dispatch.get(lastParagraphRange, "Font").toDispatch();


Dispatch.put(font, "Bold", new Variant(true)); // 設置為黑體
Dispatch.put(font, "Italic", new Variant(true)); // 設置為斜體
Dispatch.put(font, "Name", new Variant("宋體")); //
Dispatch.put(font, "Size", new Variant(12)); //小四

Dispatch.call(document, "SaveAs", new Variant("C:
abc.doc")); // 保存一個新文檔
ComThread.Release();//釋放com線程.根據jacob的幫助文檔,com的線程回收不由java的垃圾回收器處理
}

}


[火星人 ] 如何利用Java-JACOB操作WORD文檔已經有1844次圍觀

http://coctec.com/docs/java/show-post-62223.html