Jacob操作Word文档插入表格并表格中插入图片案例

一、环境配置

1)、在项目中引入jacob.jar

2)、将jacob-1.17-M2-x64.dll和jacob-1.17-M2-x86.dll文件放在jdk的path目录下面,通过System.getProperty("java.library.path");可以看到path路径,或者直接放到jdk/bin、jre/bin、system32/system64下面都放一份,总能找得到的。

二、案例代码

public class JacobTest {
    public static void main(String[] args) {
        System.getProperty("java.library.path");
        //1、初始化com线程
        ComThread.InitSTA();

        //2、创建应用程序对象
        ActiveXComponent wordApp = new ActiveXComponent("Word.Application");//word
        wordApp.setProperty("Visible", new Variant(false));//设置应用操作是文档不在明面上显示,只在后台静默处理。
        Dispatch document = wordApp.getProperty("Documents").toDispatch();
        Dispatch doc = Dispatch.call(document, "Open",new Variant("D:\\test\\test.doc")).toDispatch();

        //2、把插入点移动到文件首位置
        Dispatch.call(Dispatch.get(wordApp, "Selection").toDispatch(), "HomeKey", new Variant(6));

        //3、创建表表格
        Dispatch tables = Dispatch.get(doc, "Tables").toDispatch();
        Dispatch range = Dispatch.get(Dispatch.get(wordApp, "Selection").toDispatch(), "Range").toDispatch();
        Dispatch newTable = Dispatch.call(tables, "Add", range,
                new Variant(6), new Variant(5)).toDispatch();//5行6列
        Dispatch.call(Dispatch.get(wordApp, "Selection").toDispatch(), "MoveRight");

        //4、获取表格单元格,插入图片
        Dispatch image = Dispatch.get(Dispatch.get(wordApp, "Selection").toDispatch(),  "InLineShapes" )
                .toDispatch();
        Dispatch table = Dispatch.call(tables,  "Item" ,  new  Variant(1)) .toDispatch();//获取第一个表
        Dispatch cell = Dispatch.call(table,  "Cell" ,  new  Variant(0),//0行
                new  Variant(0)).toDispatch();//光标移动到(0,0)单元格
        Dispatch.call(cell,  "Select" );//选中单元格D:\test\2.png
        Dispatch.call(image,  "AddPicture" , "D:\\test\\2.png");



        //5、操作文档,每页插入一个图片,并调整大小和位置
        /*for(int i = 0; i < 2;i++){
            Dispatch selection = Dispatch.get(wordApp, "Selection").toDispatch();
            Dispatch inLineShapes = Dispatch.get(selection, "InLineShapes").toDispatch();
            Dispatch picture = Dispatch.call(inLineShapes, "AddPicture", "D:\\test\\2.png").toDispatch();
            //选中图片
            Dispatch.call(picture, "Select");
            //设置宽度高度

            Dispatch.put(picture, "Width", new Variant(600));
            Dispatch.put(picture, "Height", new Variant(300));

            //设置图片相对左上角偏移位置
            selection = Dispatch.get(wordApp, "Selection").toDispatch();
            Dispatch shapeRange = Dispatch.get(selection, "ShapeRange").toDispatch();
            Dispatch.put(shapeRange, "Left", new Variant(1000));
            Dispatch.put(shapeRange, "Top", new Variant(1000));

            //翻到下一页
            Dispatch browser = Dispatch.get(wordApp, "Browser").toDispatch();
            Dispatch.call(browser, "Next");
        }*/

        //6、获取文档页数
       /* Dispatch selection = Dispatch.get(wordApp, "Selection").toDispatch();
        //如果调用方法或者属性要得到某个值的话,直接tostring或者别的就可以了
        String pages = Dispatch.call(selection, "Information", new Variant(4)).toString();
        System.out.println("文档页数:"+pages);*/

        //保存
        Dispatch.call(doc, "Save");
        //退出
        wordApp.invoke("Quit", new Variant[] {});


        //关闭com线程
        ComThread.Release();
    }



}

结果:

©️2020 CSDN 皮肤主题: 大白 设计师:CSDN官方博客 返回首页