首页 > 技术文章 > java 使用jacob将html页面写入word

liudaihuablogs 2018-09-30 16:57 原文

 

 

在此先声明最重要的一点:使用jacob将html导入word时图片是使用链接的方式引入的,也就是说如果你的图片删除了,那么word中图片也没了。

原文链接:https://blog.csdn.net/feicy101/article/details/52134938?locationNum=15

本人是对原文链接中 :jacob替换图片 进行了测试

maven:

<dependency>
        <groupId>net.sf.jacob-project</groupId>
        <artifactId>jacob</artifactId>
        <version>1.14.3</version>
    </dependency>

原文代码:

package com.thinkgem.jeesite.test;

import com.jacob.activeX.ActiveXComponent;
import com.jacob.com.Dispatch;
import com.jacob.com.Variant;

/**
 * @author liuwei
 * @date 2018年9月30日 上午10:58:23
 * 
 */
public class InsertPicToWord {
    /**
     * 给指定的word文档在字符串指定位置插入图片
     * @param wordFile word文档
     * @param imagePath  待添加图片的路径
     * @param tarStr 指定的字符串位置
     */
    public static void insertImage(String wordFile, String imagePath,
                                   String tarStr) {
        ActiveXComponent app = new ActiveXComponent("Word.Application");// 启动word
        try {
            app.setProperty("Visible", new Variant(false));// 设置word不可见

            Dispatch docs = app.getProperty("Documents").toDispatch();

            Dispatch doc = Dispatch.invoke(
                    docs,
                    "Open",
                    Dispatch.Method,
                    new Object[] { wordFile, new Variant(false),
                            new Variant(false) }, new int[1]).toDispatch();
            // 打开word文件,注意这里第三个参数要设为false,这个参数表示是否以只读方式打开,
            // 因为我们要保存原文件,所以以可写方式打开。

            Dispatch selection = app.getProperty("Selection").toDispatch();

            Dispatch.call(selection, "HomeKey", new Variant(6));// 移到开头

            Dispatch find = Dispatch.call(selection, "Find").toDispatch();// 获得Find组件

            Dispatch.put(find, "Text", tarStr);// 查找字符串tarStr

            Dispatch.call(find, "Execute");// 执行查询

            Dispatch.call(Dispatch.get(selection, "InLineShapes").toDispatch(),
                    "AddPicture", imagePath);// 在指定位置插入图片

            Dispatch.call(doc, "Save");// 保存

            Dispatch.call(doc, "Close", new Variant(false));
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            app.invoke("Quit", new Variant[] {});
            app.safeRelease();
        }
    }

}

测试:

package com.thinkgem.jeesite.test;
/**
 * @author liuwei
 * @date 2018年9月30日 上午11:00:30
 * 
 */
public class InsertPicToWordTest {

    @SuppressWarnings("static-access")
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        InsertPicToWord word = new InsertPicToWord();
        String wordFile = "C:\\Users\\admin\\Desktop\\1.doc";
        String imagePath = "C:\\Users\\admin\\Desktop\\2.jpg";
        String tarStr = "${image1}";
        word.insertImage(wordFile, imagePath, tarStr);
    }

}
你要下载jacob(要和自己项目里dependience中的版本一致),在找到下载jacob里的jacob-1.14.3-x64.dll【和操作系统版本一致】,
将这个文件依次贴到E:\jdk\jre1.8\bin和E:\jdk\1.8\jre\bin【否则会报错:no jacob-1.14.3-x64 in java.library.path】

 

执行测试后手动创建的1.doc里确实有了图片,但是程序也在我的桌面上创建了一个文件夹,该文件夹里存有一张被更名的图片【即原图2.jpg】,我将该文件夹删除后,1.doc中的图片也消失了,也就是说,这种实现方式是将图片以链接的引入word里的。

这种方式适合向word文档里写入文字,对于图片实现方式不太好。

推荐阅读