這個文章給的文檔和API下載地址有問題,下載問題,請大家參照下個文章
這些文章好象都有問題,等我整整寫個文章給大家吧
Java 控制Office 控件是非常麻煩的一件事情。
自從有了JACOB后,事情變得簡單多了。
但是要實現Java靈活的控制Word還是一件非常麻煩的事情。
下面介紹幾個WORD常見的對象以及一些典型的處理過程,希望對大家有幫助。
(請注意:JDK1.3.2運行 Jacob比較正常,JDK1.4有問題)
/**?*/
/**
?WORD對象
*/
?
private
?ActiveXComponent?word?
=
?
null
;?

/**?*/
/**
?文檔對象
*/
?
private
?Dispatch?documents?
=
?
null
;?

/**?*/
/**
?selection?對象是比較重要的一個對象?
*/
?
private
?Dispatch?vSelection?
=
?
null
;?

/**?*/
/**
?一個WORD文檔?
*/
?
private
?Dispatch?wordfile?
=
?
null
;?
1,初始化
word?
=
?
new
?ActiveXComponent(
"
Word.Application
"
);?
documents?
=
?word.getProperty(
"
Documents
"
).toDispatch();?
(將JACOB?放在?WINNT\system32\?下比較簡單省事)?
2,打開文件
wordfile?
=
?Dispatch.invoke(?
documents,?
"
Open
"
,?
Dispatch.Method,?

new
?Object[]?
{?
strFileName,?
new
?Variant(
true
),
//
是否進行轉換?ConfirmConversions?
new
?Variant(
false
)
//
是否只讀?
}
,?
new
?
int
[
1
]).toDispatch();?
vSelection?
=
?word.getProperty(
"
Selection
"
).toDispatch();?
在WORD中,選定內容進行轉換時,不用象Java對象一樣來回的重新取,這個對象一直有效。
3,顯示WORD
word.setProperty(
"
Visible
"
,?
new
?Variant(visible));?
4,設置WORD的位置
Dispatch?activeWindow?
=
?Dispatch.get(word,?
"
Application
"
).toDispatch();?
Dispatch.put(activeWindow,?
"
WindowState
"
,?
new
?Variant(
0
));?
Dispatch.put(activeWindow,?
"
Top
"
,?
new
?Variant(
0
));?
Dispatch.put(activeWindow,?
"
Left
"
,?
new
?Variant(
0
));?
Dispatch.put(activeWindow,?
"
Height
"
,?
new
?Variant(
600
));?
Dispatch.put(activeWindow,?
"
width
"
,?
new
?Variant(
800
));?
進行將JAVA內的數據和WORD交換,常用的做法是,在WORD上作一些特殊的標記,利用 FIND 和 Replace的方法進行,這個方法不是太好。
個人覺得使用超鏈接的模式比較方便。
有幾大優點:
1, Hyperlink 有3個區域可以讓開發者自己利用
ActiveDocument.Hyperlinks.Add?
Anchor:
=
Selection.Range,?
Address:
=
"
位置
"
,?
//
地址(可以利用)?有個缺點?
SubAddress:
=
""
,
//
子位置(可以利用)?
ScreenTip:
=
""
,?
//
屏幕提示?
TextToDisplay:
=
"
顯示內容
"
//
最好利用的東西?
個人建議使用TextToDisplay。
Address 會在保存時被替換成絕對路徑。
比如你錄入一個
“AA.BB.CC”
保存時可能會被替換成
C:\Documents and Settings\Administrator \My Documents\AA.BB.CC
2, 可以進行自動定位
利用Hyperlinks 可以將文章中所有的超鏈接得到。
也可以將指定范圍的超鏈接得到。
3, 可以自由排版
4, 可以拷貝粘貼
添加超鏈接:
Dispatch?Hyperlinks?
=
?Dispatch.get(wordfile,?
"
Hyperlinks
"
).toDispatch();?
Dispatch?range?
=
?Dispatch.get(vSelection,?
"
Range
"
).toDispatch();?
Dispatch?h
=
Dispatch.invoke(Hyperlinks,?
"
Add
"
,?Dispatch.Method,?
new
?Object[]?

{?range,?
new
?Variant(
"
Address
"
),?
new
?Variant(
"
SubAddress
"
),?
new
?Variant(
"
{table.fieldName}
"
),
//
建議的數據鏈接處?
new
?Variant(
"
姓名
"
)?}
,?
//
?在WORD中顯示的內容?
new
?
int
[
4
]).toDispatch();?
Dispatch?hRange
=
Dispatch.get(h,?
"
Range
"
).toDispatch();?
Dispatch.call(hRange,
"
select
"
);?
//
設置字體,顏色?
Dispatch?font?
=
?Dispatch.get(vSelection,?
"
Font
"
).toDispatch();?
Dispatch.put(font,
"
Underline
"
,?
new
?Variant(
0
));?
Dispatch.put(font,
"
Color
"
,?
new
?Variant(
0
));?
//
取消選擇?
Dispatch.call(vSelection,
"
MoveRight
"
,
new
?Variant(
1
),
new
?Variant(
1
));?

超鏈接替換內容:?
1
,?得到所有的超鏈接?
//
選擇對象?
Dispatch.call(dObject,?
"
select
"
);?
//
得到超鏈接集合?
Dispatch?Hyperlinks?
=
?Dispatch.get(vSelection,?
"
Hyperlinks
"
).toDispatch();?
//
得到有多少個超鏈接?
int
?nHyperlink?
=
?Dispatch.get(Hyperlinks,?
"
count
"
).toInt();?
//
得到一個超鏈接?
Dispatch?hyperlink
=
Dispatch.invoke(Hyperlinks,?
"
item
"
,?

Dispatch.Method,?
new
?Object[]?
{?
new
?Integer(i?
+
?
1
)}
,?
new
?
int
[
1
]).toDispatch()));?
2, 替換內容
Dispatch.put(hyperlink, "TextToDisplay", information);
3, 取消超鏈接,將超鏈接變成普通文字。
Dispatch.call(hyperlink, "delete");
如何實現批量數據自動擴展,建議使用表格進行自動擴展,方便簡單。
結合使用上面超鏈接的技術。會非常簡單:
比如有如下數據:
DataA
DataB
1, 列出所有表格
和列出所有超鏈接基本一樣:
private
?
void
?getTables01(Dispatch?objcet,Vector?vTableStore)?
{?
Dispatch?tables?
=
?Dispatch.get(objcet,?
"
tables
"
).toDispatch();?
int
?nTableAmount?
=
?Dispatch.get(tables,?
"
count
"
).toInt();?

for
?(
int
?i?
=
?
0
;?i?
<
?nTableAmount;?i
++
)?
{?
Dispatch?table?
=
?
Dispatch?
.invoke(?
tables,?
"
item
"
,?
Dispatch.Method,?

new
?Object[]?
{?
new
?Integer(i?
+
?
1
)}
,?
new
?
int
[
1
])?
.toDispatch();?
vTableStore.add(
new
?DTable(table));?
getTables01(table,vTableStore);
//
處理表格套用表格的情況?
}
?
}
?
2, 表格的可以控制的對象
Dispatch?dRows?
=
?Dispatch.get(dTable,?
"
rows
"
).toDispatch();
//
所有行?
int
?nRows?
=
?Dispatch.get(dRows,?
"
count
"
).toInt();?
3
,?取得一行的內容?
Dispatch?dRow?
=
?
Dispatch?
.invoke(?
rows,?
"
item
"
,?
Dispatch.Method,?

new
?Object[]?
{?
new
?Integer(row?
+
?
1
)}
,?
new
?
int
[
1
])?
.toDispatch();?
return
?dRow;?
}
catch
(ComFailException?cfe)?

{?

/**?*/
/**
?帶有合并行的情況
*/
?
return
?
null
;?
}
?
4, 得到一行的超鏈接
DHyperlink dhInRow[] = listHyperlinks(dRow);
5, 將某一行拷貝很多次
Dispatch.call(dRow,?
"
select
"
);?
Dispatch.call(vSelection,?
"
Copy
"
);?
int
?nCopyNow?
=
?nDataBlockRow?
-
?
1
;?

for
?(
int
?nCopys?
=
?
0
;?nCopys?
<
?nCopyNow;?nCopys
++
)?
{?

try
?
{?
Dispatch.call(vSelection,?
"
Paste
"
);?

}
catch
(Exception?e)?
{?e.printStackTrace();?
//
有時候文檔損壞,可以忽略本問題,實際上已經粘貼上了?
}
?
}
?
6, 替換內容,讀到這里就不用介紹了。
打印預覽:
Dispatch.call(wordfile,"PrintPreView");
其他的功能發掘
利用WORD的宏錄制,以及VB編輯器,輔助功能,都能發掘出來
地震讓大伙知道:居安思危,才是生存之道。