歡迎您光臨本站 註冊首頁

Java調用com組件操作word使用總結(jacob)

←手機掃碼閱讀     火星人 @ 2014-03-09 , reply:0
一、準備工作
先了解一下概念,JACOB 就是 JAVA-COM Bridge的縮寫,提供自動化的訪問com的功能,也是通過JNI功能訪問windows平台下的com組件或者win32系統庫的.這是一個開始於 1999年的開源項目的成果,有很多使用者對該項目進行了修改,做出了自己的貢獻.
Jacob下載地址:http://sourceforge.net/project/showfiles.php?group_id=109543&package_id=118368
我在這裡下載了Jacob1.14.3和jacob1.9的版本兩個版本
這裡下載的是目前最新的Jacob1.14.3的Release版.
另外java操作word方式還有(個人認為通過jacob最好,自己可以擴展,網上除poi之外幾乎全是java-com技術實現的):
(1):Apache POI - Java API To Access Microsoft Format Files(http://poi.apache.org/);對word處理不夠強處理Excel功能可以,但是全是通過java完成的,不需要com組件支持;
(2):java2word 是一個在java程序中調用 MS Office Word 文檔的組件(類庫).該組件提供了一組簡單的介面,以便java程序調用他的服務操作Word 文檔.(好象也是用的java-com技術);
(3)web開發語言操作word的功能最好還是用第三方的控制項,看看這個SOAOFFICE,還可以使用js 寫VBA呢 http://www.kehansoft.com/soaoffice/doclist.asp
二、安裝Jacob
Jacob的安裝非常的簡單,我們解開下載的jacob_1.9.zip,在文件夾中找到jacob.dll和jacob.jar兩個文件,如果是 Jacob1.14.3則是jacob-1.14.3-x86.dll(32位,機和jacob-1.14.3-x64.dll(64位)和 jacob.jar兩個文件.Jacob.dll直接放到系統的system32文件夾下就行了,連註冊都不用的(或者拷貝到jdk或者jre的bin目錄下也行,當前測試文件所在的目錄也行,就是只要在java.library.path中就可以).而jacob.jar設置到classpath中去就可以了,或者在IDE開發環境的工程中設置擴展庫也一樣的,我是這樣使用的將jacob-1.14.3-x86.dll或複製到%Tomcat5%bin目錄下將jacob.jar複製到%Tomcot5%Sharelib目錄下,我使用過程中感覺放到這裡是一個最終解決辦法,當你放哪都有問題的時候.我這樣用之後再沒有出過因為系統不一樣出現的各種各樣的問題,當然你作的是web的項目.
注意使用jacob一寫要安裝word,我裝的word2003,如果是操作word2007就不用jacob了(好像這方面的API).
對jacob.dll幾種配置方法 (網上看到):
2008-07-31 11:59:49
1、把jacob.dll文件,複製到 windowssystem32 目錄下.(註:我用的時候這個方法不能運行)
2、 把jacob.dll放入 Javajdk1.5.0_06jrebin目錄下.把jacob.jar放入 Javajdk1.5.0_0jrelibext目錄下.可以正常運行.
3、把jacob.dll放入 glcsrc目錄下.把jacob.jar放入WEB-INFlib目錄下,也是可以正常運行.
三、使用(以下是我改寫的一個word操作類,希望有興趣的朋友完善,記得發給我一份)


//注意java操作word關鍵是定位操作對象;
import com.jacob.activeX.ActiveXComponent;
import com.jacob.com.Dispatch;
import com.jacob.com.Variant;
public class WordBean {
// word文檔
private Dispatch doc;
// word運行程序對象
private ActiveXComponent word;
// 所有word文檔集合
private Dispatch documents;
// 選定的範圍或插入點
private Dispatch selection;
private boolean saveOnExit = true;
public WordBean()throws Exception{
if (word == null) {
word = new ActiveXComponent("Word.Application");
word.setProperty("Visible", new Variant(false)); //不可見打開word
word.setProperty("AutomationSecurity", new Variant(3)); //禁用宏
}
if (documents == null)
documents = word.getProperty("Documents").toDispatch();
}
public void setSaveOnExit(boolean saveOnExit) {
this.saveOnExit = saveOnExit;
}
public void createNewDocument() {
doc = Dispatch.call(documents, "Add").toDispatch();
selection = Dispatch.get(word, "Selection").toDispatch();
}
public void openDocument(String docPath) {
closeDocument();
doc = Dispatch.call(documents, "Open", docPath).toDispatch();
selection = Dispatch.get(word, "Selection").toDispatch();
}
public void openDocumentOnlyRead(String docPath, String pwd)throws Exception {
closeDocument();
// doc = Dispatch.invoke(documents, "Open", Dispatch.Method,
// new Object[]{docPath, new Variant(false), new Variant(true), new Variant(true), pwd},
// new int[1]).toDispatch();//打開word文件
doc = Dispatch.callN(documents, "Open", new Object[]{docPath, new Variant(false),
new Variant(true), new Variant(true), pwd, "", new Variant(false)}).toDispatch();
selection = Dispatch.get(word, "Selection").toDispatch();
}
public void openDocument(String docPath, String pwd)throws Exception {
closeDocument();
doc = Dispatch.callN(documents, "Open", new Object[]{docPath, new Variant(false),
new Variant(false), new Variant(true), pwd}).toDispatch();
selection = Dispatch.get(word, "Selection").toDispatch();
}
public void moveUp(int pos) {
if (selection == null)
selection = Dispatch.get(word, "Selection").toDispatch();
for (int i = 0; i < pos; i )
Dispatch.call(selection, "MoveUp");
}
public void moveDown(int pos) {
if (selection == null)
selection = Dispatch.get(word, "Selection").toDispatch();
for (int i = 0; i < pos; i )
Dispatch.call(selection, "MoveDown");
}
public void moveLeft(int pos) {
if (selection == null)
selection = Dispatch.get(word, "Selection").toDispatch();
for (int i = 0; i < pos; i ) {
Dispatch.call(selection, "MoveLeft");


}
}
public void moveRight(int pos) {
if (selection == null)
selection = Dispatch.get(word, "Selection").toDispatch();
for (int i = 0; i < pos; i )
Dispatch.call(selection, "MoveRight");
}
public void moveStart() {
if (selection == null)
selection = Dispatch.get(word, "Selection").toDispatch();
Dispatch.call(selection, "HomeKey", new Variant(6));
}
@SuppressWarnings("static-access")
public boolean find(String toFindText) {
if (toFindText == null || toFindText.equals(""))
return false;
// 從selection所在位置開始查詢
Dispatch find = word.call(selection, "Find").toDispatch();
// 設置要查找的內容
Dispatch.put(find, "Text", toFindText);
// 向前查找
Dispatch.put(find, "Forward", "True");
// 設置格式
Dispatch.put(find, "Format", "True");
// 大小寫匹配
Dispatch.put(find, "MatchCase", "True");
// 全字匹配
Dispatch.put(find, "MatchWholeWord", "True");
// 查找並選中
return Dispatch.call(find, "Execute").getBoolean();
}
public boolean replaceText(String toFindText, String newText) {
if (!find(toFindText))
return false;
Dispatch.put(selection, "Text", newText);
return true;
}
public void replaceAllText(String toFindText, String newText) {
while (find(toFindText)) {
Dispatch.put(selection, "Text", newText);
Dispatch.call(selection, "MoveRight");
}
}
public void insertText(String newText) {
Dispatch.put(selection, "Text", newText);
}
public boolean replaceImage(String toFindText, String imagePath) {
if (!find(toFindText))
return false;
Dispatch.call(Dispatch.get(selection, "InLineShapes").toDispatch(),
"AddPicture", imagePath);
return true;
}
public void replaceAllImage(String toFindText, String imagePath) {
while (find(toFindText)) {
Dispatch.call(Dispatch.get(selection, "InLineShapes").toDispatch(),
"AddPicture", imagePath);
Dispatch.call(selection, "MoveRight");
}
}
public void insertImage(String imagePath) {
Dispatch.call(Dispatch.get(selection, "InLineShapes").toDispatch(),
"AddPicture", imagePath);
}
public void mergeCell(int tableIndex, int fstCellRowIdx, int fstCellColIdx,
int secCellRowIdx, int secCellColIdx) {
// 所有表格
Dispatch tables = Dispatch.get(doc, "Tables").toDispatch();
// 要填充的表格
Dispatch table = Dispatch.call(tables, "Item", new Variant(tableIndex))
.toDispatch();
Dispatch fstCell = Dispatch.call(table, "Cell",
new Variant(fstCellRowIdx), new Variant(fstCellColIdx))
.toDispatch();


Dispatch secCell = Dispatch.call(table, "Cell",
new Variant(secCellRowIdx), new Variant(secCellColIdx))
.toDispatch();
Dispatch.call(fstCell, "Merge", secCell);
}
public void putTxtToCell(int tableIndex, int cellRowIdx, int cellColIdx,
String txt) {
// 所有表格
Dispatch tables = Dispatch.get(doc, "Tables").toDispatch();
// 要填充的表格
Dispatch table = Dispatch.call(tables, "Item", new Variant(tableIndex))
.toDispatch();
Dispatch cell = Dispatch.call(table, "Cell", new Variant(cellRowIdx),
new Variant(cellColIdx)).toDispatch();
Dispatch.call(cell, "Select");
Dispatch.put(selection, "Text", txt);
}
public String getTxtFromCell(int tableIndex, int cellRowIdx, int cellColIdx) {
// 所有表格
Dispatch tables = Dispatch.get(doc, "Tables").toDispatch();
// 要填充的表格
Dispatch table = Dispatch.call(tables, "Item", new Variant(tableIndex))
.toDispatch();
Dispatch cell = Dispatch.call(table, "Cell", new Variant(cellRowIdx),
new Variant(cellColIdx)).toDispatch();
Dispatch.call(cell, "Select");
String ret = "";
ret = Dispatch.get(selection, "Text").toString();
ret = ret.substring(0, ret.length()-1); //去掉的回車符;
return ret;
}
public void pasteExcelSheet(String pos) {
moveStart();
if (this.find(pos)) {
Dispatch textRange = Dispatch.get(selection, "Range").toDispatch();
Dispatch.call(textRange, "Paste");
}
}
public void copyTable(String pos, int tableIndex) {
// 所有表格
Dispatch tables = Dispatch.get(doc, "Tables").toDispatch();
// 要填充的表格
Dispatch table = Dispatch.call(tables, "Item", new Variant(tableIndex))
.toDispatch();
Dispatch range = Dispatch.get(table, "Range").toDispatch();
Dispatch.call(range, "Copy");
if (this.find(pos)) {
Dispatch textRange = Dispatch.get(selection, "Range").toDispatch();
Dispatch.call(textRange, "Paste");
}
}
public void copyTableFromAnotherDoc(String anotherDocPath, int tableIndex,
String pos) {
Dispatch doc2 = null;
try {
doc2 = Dispatch.call(documents, "Open", anotherDocPath)
.toDispatch();
// 所有表格
Dispatch tables = Dispatch.get(doc2, "Tables").toDispatch();
// 要填充的表格
Dispatch table = Dispatch.call(tables, "Item",
new Variant(tableIndex)).toDispatch();
Dispatch range = Dispatch.get(table, "Range").toDispatch();
Dispatch.call(range, "Copy");
if (this.find(pos)) {
Dispatch textRange = Dispatch.get(selection, "Range")
.toDispatch();
Dispatch.call(textRange, "Paste");
}
} catch (Exception e) {
e.printStackTrace();


} finally {
if (doc2 != null) {
Dispatch.call(doc2, "Close", new Variant(saveOnExit));
doc2 = null;
}
}
}


[火星人 ] Java調用com組件操作word使用總結(jacob)已經有598次圍觀

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