jacob插件導出文字、表格、圖片到Word

一、介紹

   Jacob 是Java-COM Bridge的縮寫,它在Java與微軟的COM組件之間構建一座橋樑。使用Jacob自帶的DLL動態鏈接庫,並通過JNI的方式實現了在Java平臺上對COM程序的調用。

二、安裝和配置

   Jacob是一個開源軟件,它的官方站點是:http://danadler.com/jacob/ 大家可以到上面下載源代碼研究,也可以直接下載編譯後的二進制文件。

  • 下載包jacob_x.x.zip,解壓後有幾個文件:jacob.jar、jacob.dll

  • 把jacob.dll拷貝到%JAVA_HOME%\jre\bin目錄下,%JAVA_HOME%就是JDK的安裝目錄或是C:\Windows\System32下面

  • 接着直接在java IDE中引用jacob.jar就可以使用了

三、jacob.jar的結構

jacob包括兩個部分:

  • com.jacob.activeX: ActiveXComponent類

  • com.jacob.com: 其它類和元素

四、Jacob類

Jacob的結構很簡單,包含以下幾個類:

  • ActiveXComponent Class:封裝了Dispatch對象,用於創建一個封裝了COM組件對象的Java Object

  • Dispatch Class:用於指向封裝後的MS數據結構。常用的方法有call,subcall,get,invoke…後面會介紹使用方法。

  • Variant Class:用於映射COM的Variant數據類型。提供Java和COM的數據交換。

  • ComException Class:異常類

五、Jacob方法

用於訪問COM/DLL對象的方法,讀取、修改COM/DLL對象的屬性。

  • call method:屬於Dispatch類。用於訪問COM/DLL對象的方法。方法進行了重載,方便不同場合調用。返回一個Variant類型的值。

  • callSub method:使用方法和call一樣,不過它不返回值。

  • get method:讀取COM對象的屬性值,返回一個Variant類型值。

  • put method:設置COM對象的屬性值。

  • invoke method:call的另一種用法,更復雜一些。

  • invokesub method:subcall的另一種用法

  • getProperty method:屬於ActiveXComponent類,讀取屬性值,返回一個Variant類型值。

  • setProperty method:屬於ActiveXComponent類,設置屬性值。

六、實例

package cn.jcl.test;


import com.jacob.activeX.ActiveXComponent;

import com.jacob.com.ComThread;

import com.jacob.com.Dispatch;

import com.jacob.com.Variant;


public class exportPicToWord {

   ActiveXComponent msWordApp = null;

   Dispatch document = null;    

   public exportPicToWord() {

       if (msWordApp == null) {

           ComThread.InitSTA();

           msWordApp = new ActiveXComponent("Word.Application");

       }

   }


   //TODO 設置文檔是否前臺打開word

   public void setVisible(boolean visible) {

       msWordApp.setProperty("Visible", new Variant(visible));

       // 與上一句作用相同

       // Dispatch.put(msWordApp, "Visible", new Variant(visible));

   }


   //TODO 創建一個新的文檔

   public void createNewDocument() {

       // document表示word的所有文檔窗口,(word是多文檔窗口應用程序)

       Dispatch documents = Dispatch.get(msWordApp, "Documents").toDispatch();

       // 調用add方法來創建一個新的文檔

       document = Dispatch.call(documents, "Add").toDispatch();

   }


   //TODO 向文檔中插入格式化文檔

   public void insertFormatStr() {

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

       // 得到段落格式對象

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

       // 設置正文的對齊方式:1.置中,2.靠右,3.靠左

       Dispatch.put(paragraphFormat, "Alignment", "1");

       // 得到字體對象

       Dispatch font = Dispatch.get(selection, "Font").toDispatch();

       // 設置黑體

       Dispatch.put(font, "Bold", new Variant(true));

       // 字型顏色(1,0,0,0=>紅色  1,1,0,0=>棕色)

       Dispatch.put(font, "Color", "1,0,0,0");

       // 設置斜體

       Dispatch.put(font, "Italic", new Variant(true));

       // 設置宋體

       Dispatch.put(font, "Name", new Variant("宋體"));

       // 設置大小

       Dispatch.put(font, "Size", new Variant(12));


       // 寫入標題內容

       Dispatch.call(selection, "TypeText", "標題");

       // 空一行段落

       Dispatch.call(selection, "TypeParagraph");

       Dispatch.put(paragraphFormat, "Alignment", "3");

       Dispatch.put(font, "Color", "1,1,0,0");

       Dispatch.put(selection, "Text", "  第一段內容");

       Dispatch.call(selection,"MoveRight");

       // 空一行段落

       Dispatch.call(selection, "TypeParagraph");

       Dispatch.put(selection, "Text", "  第二段內容");

       Dispatch.call(selection,"MoveRight");

   }


   //TODO 向文檔中添加文字.

   public void insertText(String text) {

       // 得到選中的內容,如果創建一個新的文檔,因裏面沒有內容,光標應在開頭.

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

       // 空一行段落

       Dispatch.call(selection, "TypeParagraph");

       Dispatch font = Dispatch.get(selection, "Font").toDispatch();

       Dispatch.put(font, "Color", "0,1,1,0");

       Dispatch.put(selection, "Text", text);

       Dispatch.call(selection,"MoveRight");

   }


   //TODO 向文檔中添加表格.

   public void insertTable(int row, int column) {

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

// 空一行段落

       Dispatch.call(selection, "TypeParagraph");

// 建立表格

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

       // 當前光標位置或者選中的區域

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

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

               new Variant(row), new Variant(column), new Variant(1)) // 設置row,column,表格外框寬度

               .toDispatch();

       Dispatch cols = Dispatch.get(newTable, "Columns").toDispatch(); // 此表的所有列  

       int colCount = Dispatch.get(cols, "Count").toInt(); // 一共有多少列 實際上這個數==column

       for (int i = 1; i <= colCount; i++) { // 循環取出每一列  

           Dispatch col = Dispatch.call(cols, "Item", new Variant(i)).toDispatch();  

           Dispatch cells = Dispatch.get(col, "Cells").toDispatch(); // 當前列中單元格  

           int cellCount = Dispatch.get(cells, "Count").toInt(); // 當前列中單元格數 實際上這個數等於row  

           for (int j = 1; j <= cellCount; j++) { // 每一列中的單元格數

putTxtToCell(newTable, j, i, "第" + j + "行,第" + i + "列");// 與上面四句的作用相同

           }

       }

       Dispatch.call(selection,"MoveRight");

       Dispatch.call(selection, "TypeParagraph");

       Dispatch.call(selection,"MoveRight");

   }


   //TODO 在指定的單元格里填寫數據

   public void putTxtToCell(Dispatch table, int cellRowIdx, int cellColIdx, String txt) {

       Dispatch cell = Dispatch.call(table, "Cell", new Variant(cellRowIdx),

               new Variant(cellColIdx)).toDispatch();

       Dispatch.call(cell, "Select");

       Dispatch selection = Dispatch.get(msWordApp, "Selection").toDispatch(); // 輸入內容需要的對象

       Dispatch.put(selection, "Text", txt);

   }

   //TODO 在指定的單元格里填寫數據

   public void putTxtToCell(int tableIndex, int cellRowIdx, int cellColIdx, String txt) {  

       // 所有表格  

       Dispatch tables = Dispatch.get(document, "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 selection = Dispatch.get(msWordApp, "Selection").toDispatch(); // 輸入內容需要的對象  

       Dispatch.put(selection, "Text", txt);  

   }


   /**

    * 設置圖片水印

    * @param waterMarkPath 水印路徑

    */

   public void setWaterMark(String waterMarkPath) {

       Dispatch activeWindow = Dispatch.get(msWordApp, "ActiveWindow").toDispatch();

       // 取得活動窗格對象

       Dispatch activePan = Dispatch.get(activeWindow, "ActivePane").toDispatch();

       // 取得視窗對象

       Dispatch view = Dispatch.get(activePan, "View").toDispatch();

       // 打開頁眉,值爲9,頁腳爲10

       Dispatch.put(view, "SeekView", new Variant(9));

       Dispatch docSelection = Dispatch.get(activeWindow, "Selection").toDispatch();

       // 獲取頁眉和頁腳

       Dispatch headfooter = Dispatch.get(docSelection, "HeaderFooter").toDispatch();

       // 獲取水印圖形對象

       Dispatch shapes = Dispatch.get(headfooter, "Shapes").toDispatch();

       // 給文檔全部加上水印,設置了水印效果,內容,字體,大小,是否加粗,是否斜體,左邊距,上邊距。

       // 調用shapes對象的AddPicture方法將全路徑爲picname的圖片插入當前文檔

       Dispatch picture = Dispatch.call(shapes, "AddPicture", waterMarkPath).toDispatch();    

       // 選擇當前word文檔的水印

       Dispatch.call(picture, "Select");

       Dispatch.put(picture, "Left", new Variant(100));

       Dispatch.put(picture, "Top", new Variant(300));

       Dispatch.put(picture, "Width", new Variant(100));

       Dispatch.put(picture, "Height", new Variant(30));        

       // 關閉頁眉

       Dispatch.put(view, "SeekView", new Variant(0));        

   }


   //TODO 向文檔中添加圖片

   public void insertPic(String jpegFilePath) {

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

       // 空一行段落

       Dispatch.call(selection, "TypeParagraph");

       // 得到段落格式對象

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

       // 設置正文的對齊方式:1.置中,2.靠右,3.靠左

       Dispatch.put(paragraphFormat, "Alignment", "1");        

       Dispatch image = Dispatch.get(selection, "InLineShapes").toDispatch();

       Dispatch.call(image, "AddPicture", jpegFilePath);

       Dispatch.call(selection, "MoveRight");

   }


   public void saveFileAs(String fileName) {

       Dispatch.call(document, "SaveAs", fileName);

   }


   public void closeDocument() {

       Dispatch.call(document, "Close", "-1");

       document = null;

   }


   public void closeWord() {

       Dispatch.call(msWordApp, "Quit");

       ComThread.Release();

       msWordApp = null;

       document = null;

   }


//TODO 打開一個word文檔

   public void openFile(String wordFilePath) {

       Dispatch documents = Dispatch.get(msWordApp, "Documents").toDispatch();

       document = Dispatch.call(documents, "Open", wordFilePath, new Variant(true) // 是否進行版本轉換        

       , new Variant(false) // 是否是隻讀        

       ).toDispatch();

   }


   //TODO 讀出一個word文檔

   public void readWord() {

// 取得word文檔的內容

       Dispatch wordContent = Dispatch.get(document, "Content").toDispatch();

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);

}

   }


   public void save() {

       Dispatch.call(document, "Save");

   }    


   public static void main(String[] args) {

       exportPicToWord bean = new exportPicToWord();

       bean.setVisible(false);

       bean.createNewDocument();        

       bean.insertFormatStr();

       bean.insertText("1、第一次添加的文字!");

       bean.insertText("2、第二次添加的文字!");

       bean.insertTable(3, 5);

       bean.setWaterMark("c:\\ztest\\pic.jpg");

       bean.insertPic("c:\\ztest\\pic2.jpg");

       bean.insertPic("c:\\ztest\\pic.jpg");

       //bean.readWord();

       bean.saveFileAs("c:\\ztest\\test.doc");        

       bean.closeDocument();

       bean.closeWord();

   }

}


// JDK1.6使用jacob_1.9沒問題

// jacob_1.9.zip


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章