Java靈活的控制Word__Java

來源:互聯網
上載者:User
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編輯器,協助工具功能,都能發掘出來 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.