天天看點

Java調用ocx控件以及dll和word文檔

http://hpfgc.blog.163.com/blog/static/14799557201011811486481/

最近因為項目需要,特地研究了兩天jacob操作office的一個java插件,因時間緣故,隻研究了操作word,特此共享:

    本人喜歡通過代碼學習東西,是以,直接講操作的代碼拷貝出來,供大家分享。   

public static void main(String args[]) {

ComThread.InitSTA();// 初始化com的線程,非常重要!!使用結束後要調用 realease方法

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

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

Dispatch.put((Dispatch) wordObject, "Visible", new Variant(true));// Variant(true)表示word應用程式可見

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

Dispatch document = Dispatch.call(documents, "Add").toDispatch(); // 使用Add指令建立一個新文檔,用Open指令可以打開一個現有文檔

Dispatch wordContent = Dispatch.get(document, "Content").toDispatch(); // 取得word檔案的内容

Dispatch selection = Dispatch.get(wordObject, "Selection").toDispatch();

Dispatch paragraphFormat=Dispatch.get(selection,"ParagraphFormat").getDispatch();

Dispatch font = Dispatch.get(selection, "Font").toDispatch(); // 字型格式化需要的對象

Dispatch.put(paragraphFormat, "Alignment", "1"); // 設定标題的對齊方式(1:置中 2:靠右 3:靠左)

Dispatch.put(font, "Bold", "1"); // 字型租體

Dispatch.put(font, "Color", "1,0,0,0"); // 字型顔色(1,0,0,0=>紅色  1,1,0,0=>棕色)

Dispatch.put(font, "Italic", "1"); //字型斜體

Dispatch.call(selection, "TypeText", "标題"); // 寫入标題内容

Dispatch.call(selection, "TypeParagraph"); // 空一行段落

Dispatch.put(paragraphFormat, "Alignment", "3"); // 設定正文的對齊方式(1:置中 2:靠右 3:靠左)

Dispatch.put(selection, "Text", "  段落one");

Dispatch.call(selection,"MoveRight");

Dispatch.call(selection, "TypeParagraph"); // 空一行段落

Dispatch.put(selection, "Text", "  段落two");

Dispatch.call(selection,"MoveRight");

Dispatch tables = Dispatch.get(wordContent, "Tables").toDispatch();

        Dispatch tablerange = Dispatch.get(selection, "Range").toDispatch();

        Dispatch newTable = Dispatch.call(tables, "Add", tablerange,

                new Variant(2), new Variant(3)).toDispatch();

        Dispatch newtables = Dispatch.get(wordContent, "Tables").toDispatch();

        Dispatch table = Dispatch.call(newtables, "Item", new Variant(1)).toDispatch(); // 要填充的表格         

        Dispatch cols = Dispatch.get(table, "Columns").toDispatch(); // 表格的所有行

        Dispatch rows = Dispatch.get(table, "Rows").toDispatch(); // 表格的所有列

        int colsCount=Dispatch.get(cols, "Count").toInt();

        int rowsCount=Dispatch.get(rows,"Count").toInt();

        Dispatch col = Dispatch.get(cols, "First").toDispatch();

        for(int i=1;i<=rowsCount;i++)

        {

        for(int m=1;m<=colsCount;m++)

        {

        Dispatch cell=Dispatch.call(table,"Cell",new Variant(i),new Variant(m)).getDispatch();

        Dispatch.call(cell, "Select");

        Dispatch.put(selection, "Text",i*m );

        Dispatch.call(selection,"MoveRight");

        }

        }

        Dispatch.call(selection, "EndKey",6);

Dispatch paragraphs = Dispatch.get(wordContent, "Paragraphs").toDispatch(); // 所有段落

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

for(int i =1;i<=paragraphCount;i++)

{

Dispatch paragraph = Dispatch.call(paragraphs, "Item", new Variant(i)).toDispatch();

Dispatch range = Dispatch.call(paragraph,"Range").toDispatch();

String ptext = Dispatch.get(range, "text").getString();

System.out.println(ptext);

}

String picturePath="E:\\照片\\蘇歆然\\2009-01-08-00.bmp";//圖檔的路徑

Dispatch.call(Dispatch.get(selection, "InLineShapes").toDispatch(),

                "AddPicture", picturePath);

Dispatch.call(document, "SaveAs", new Variant("d://abc1.doc")); // 儲存一個新文檔

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

}     

       生成的word 的格式如下:              

Java調用ocx控件以及dll和word文檔

     補充:jacob操作powerpoint

     ActiveXComponent ppt = new ActiveXComponent("PowerPoint.Application");

Dispatch pptObject=ppt.getObject();

Dispatch.put((Dispatch) pptObject, "Visible", new Variant(true));

// 設定程式界面是否可見

ActiveXComponent presentations = ppt.getPropertyAsComponent("Presentations");

// 生成一個新的ppt 對象

ActiveXComponent presentation =presentations.invokeGetComponent("Add", new Variant(1));

Dispatch windows = presentation.getProperty("Windows").toDispatch();

Dispatch window = Dispatch.call(windows, "Item", new Variant(1)).toDispatch();

Dispatch selection = Dispatch.get(window, "Selection").toDispatch();

ActiveXComponent slides = presentation.getPropertyAsComponent("Slides");

//添加第一張幻燈片;

slides.invoke("Add", new Variant(1), new Variant(1));

Dispatch slideRange=Dispatch.get(selection, "SlideRange").getDispatch();

Dispatch shapes=Dispatch.get(slideRange, "Shapes").getDispatch();

Dispatch shape1 = Dispatch.call(shapes, "Item", new Variant(2)).toDispatch();

Dispatch.call(shape1, "Select");

Dispatch shapeRange=Dispatch.get(selection, "ShapeRange").getDispatch();

Dispatch textFrame=Dispatch.get(shapeRange, "TextFrame").getDispatch();

Dispatch textRange=Dispatch.get(textFrame, "TextRange").getDispatch();

Dispatch.call(textRange, "Select");

Dispatch.put(textRange,"Text","測試");

//添加第二張幻燈片;

slides.invoke("Add", new Variant(2), new Variant(1));

// powerpoint幻燈展示設定對象

ActiveXComponent setting = presentation.getPropertyAsComponent("SlideShowSettings");

setting.invoke("Run");

//儲存ppt

presentation.invoke("SaveAs", new Variant(PPT_FILE));

// 釋放控制線程

ComThread.Release();

二:Java調用ocx控件以及dll

通過Java調用OCX控件有幾種方法,JNI、JACOB、Jawin等

1.JNI 最直接的方式,也是最麻煩的方式,需要自己完成所有的工作,不推薦。

2.Jawin 嘗試了一下,效果不錯,但相對來說,其程式設計風格更貼近Windows,離Java有點遠 。

3.Jacob 使用Jacob非常友善,Java程式設計風格,需要了解的知識比較少。

Jacob的使用方法:

首先--

1.在工程中導入 jacob.jar 這個包。

2.把 jacob.dll 拷貝到 jdk\bin或者C:\WINDOWS\system32目錄下。

以上的兩個檔案您可以到網上http://danadler.com/jacob/ 下載下傳的1.7的版本

jacob.jar 與 jacob.dll 版本一緻。

3.注冊ocx控件

l  先把***.ocx控件放入c:\windows\system32 檔案夾下

l  在cmd指令行下,輸入regsvr32  ***.ocx指令進行注冊

Java代碼:

1.初始化 

ActiveXComponent com = new ActiveXComponent("元件的ProgID") ;

Dispatch disp = com.getObject();

2.調用控件裡面的方面

2.1調用無參的方法,并傳回一個short值

Dispatch.call(disp, "Init").getShort();

2.2調用有一個參數的方法,并傳回一個boolean值

Dispatch.call(disp,"Method",new Variant(args)).getBoolean();

調用多個參數依次類推,注意在傳遞參數前,将Java中的參數轉換成Variant。

bug解決:

1.java.lang.UnsatisfiedLinkError: no jacob in java.library.path

原因是:沒有把 jacob.dll 拷貝到 jdk\bin或者C:\WINDOWS\system32目錄下

2.Exception in thread "main" com.jacob.com.ComFailException: Can't get object clsid from progid

 原因是:regsvr32  ***.ocx指令進行注冊後 ,擷取的progid有誤;

3.

Exception in thread "main" com.jacob.com.ComFailException: A COM exception has been encountered:

At Invoke of: LoadFile

Description: 災難性故障

    at com.jacob.com.Dispatch.invokev(Native Method)

    at com.jacob.com.Dispatch.invokev(Dispatch.java:858)

    at com.jacob.com.Dispatch.callN(Dispatch.java:455)

    at com.jacob.com.Dispatch.call(Dispatch.java:544)

    at com.lv.office.AIPToTest.jacob(AIPToTest.java:24)

    at com.lv.office.AIPToTest.main(AIPToTest.java:14)

原因是:

在OCX控件中,重寫COleControl.IsInvokeAllowed (DISPID)方法,即在控件的CMyNameCtrl中增加一個方法

BOOL CMyNameCtrl::IsInvokeAllowed (DISPID)

   {

      return TRUE;

   }

用這個方法的理由是:

這個錯誤是由ActiveX結構設計造成的。

在Ole4.0版本之前,外部程式是可以直接調用OCX中方法的。Ole4.0之後,每次調用控件中的方法,系統會自動檢查是否允許調用,即運作COleControl.IsInvokeAllowed (DISPID)

該方法檢查控件是否正确的初始化或者是否通過持久存儲接口正确加載,如果兩個條件有一個滿足,即傳回TRUE,否則傳回FALSE。

當控件在MFC中使用時,很多細節,如初始化,都被過濾了,這樣,大多數使用者都不會遇到這個問題。但是,當我們從C、C++的dll中調用控件時,不滿足上述條件,該方法傳回FALSE,這時候再調用任何控件方法,都會出現上述異常。

OCX

繼續閱讀