- //java 調用word
- package com.test;
- import com.jacob.activeX.ActiveXComponent;
- import com.jacob.com.Dispatch;
- import com.jacob.com.Variant;
- public class WordBean extends java.awt.Panel {
- private ActiveXComponent MsWordApp = null;
- private Dispatch document = null;
- public WordBean() {
- super();
- }
- // 打開word文檔
- public void openWord(boolean makeVisible) {
- if (MsWordApp == null) {
- MsWordApp = new ActiveXComponent("Word.Application");
- }
- // 設置打開word文檔是否可見
- Dispatch.put(MsWordApp, "Visible", new Variant(makeVisible));
- }
- // 打開wordDocument
- public void openWordDocument(String openFile) {
- Dispatch documents = Dispatch.get(MsWordApp, "Documents").toDispatch();
- document = Dispatch.invoke(documents, "Open",Dispatch.Method,
- new Object[] { openFile, new Variant(false),
- new Variant(true) }, new int[1]).toDispatch();
- }
- // 創建word文檔
- public void createNewDocument() {
- // Find the Documents collection object maintained by Word
- Dispatch documents = Dispatch.get(MsWordApp, "Documents").toDispatch();
- document = Dispatch.call(documents, "Add").toDispatch();
- }
- // 插入內容
- public void insertText(String textToInsert) {
- // Get the current selection within Word at the moment. If
- // a new document has just been created then this will be at
- // the top of the new doc
- Dispatch selection = Dispatch.get(MsWordApp, "Selection").toDispatch();
- Dispatch.put(selection, "Text", textToInsert);
- }
- // 文件另存爲
- public void saveFileAs(String saveFile) {
- Dispatch.invoke(document, "SaveAs", Dispatch.Method, new Object[] {
- saveFile, new Variant(0) }, new int[1]);
- Variant variant = new Variant(false);
- Dispatch.call(document, "Close", variant);
- // Dispatch.call(document, "SaveAs", saveFile);
- }
- public void printFile() {
- // Just print the current document to the default printer
- Dispatch.call(document, "PrintOut");
- }
- // 關閉word文檔
- public void closeDocument() {
- // 0 = wdDoNotSaveChanges
- // -1 = wdSaveChanges
- // -2 = wdPromptToSaveChanges
- Dispatch.call(document, "Close", new Variant(0));
- document = null;
- }
- // 關閉officeWord
- public void closeWord() {
- Dispatch.call(MsWordApp, "Quit");
- MsWordApp = null;
- document = null;
- }
- // 替換文本
- public void replace(String oldText, String newText) {
- Dispatch selection = Dispatch.get(MsWordApp, "Selection").toDispatch();
- Dispatch find = MsWordApp.call(selection, "Find").toDispatch();
- Dispatch.put(find, "Text", oldText);
- Dispatch.call(find, "Execute");
- Dispatch.put(selection, "Text", newText);
- Dispatch.call(selection, "MoveRight");
- }
- }
- package com.test;
- public class WordTest {
- public static void main(String[] args) {
- WordBean word = new WordBean();
- word.openWord(false);
- // word.createNewDocument();
- // word.insertText("1234567890");
- // word.saveFileAs("d:""1.doc");
- word.openWordDocument("d:""1.doc");
- word.replace("2", "xx"); //替換
- word.replace("3", "yy");
- word.saveFileAs("d:""2.doc");
- // word.closeDocument();
- word.closeWord();
- }
- }
Java 調用word
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.