分享

jacob插件导出文字、表格、图片到Word

 三十的狼 2019-05-20

一、介绍

   Jacob 是Java-COM Bridge的缩写,它在Java与微软的COM组件之间构建一座桥梁。使用Jacob自带的DLL动态链接库,并通过JNI的方式实现了在Java平台上对COM程序的调用。

二、安装和配置

   Jacob是一个开源软件,它的官方站点是:http:///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

      本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
      转藏 分享 献花(0

      0条评论

      发表

      请遵守用户 评论公约

      类似文章 更多