使用java和jacob技术在word指定内容之后添加数据

注意jacob只能在windows使用
参考jacob字体修改链接:https://bbs.csdn.net/topics/390149961?spm=1001.2014.3001.5506
参考jacob方法链接:https://blog.csdn.net/jerome_wang/article/details/84676725?spm=1001.2014.3001.5506
对比:
在这里插入图片描述
在这里插入图片描述

1、添加依赖
<!--添加本地的jacob.jar包 -->
        <dependency>
            <groupId>jacob</groupId>
            <artifactId>jacob</artifactId>
            <version>1.19</version>
        </dependency>
2、把jacob-1.19-x64.dll放在以下文件夹下面,根据你的系统来使用,我的是64位

文件下载地址:https://pan.baidu.com/s/1Fx543YqaMDgbzyyU3_a2NQ
提取码: 63ai
在这里插入图片描述
jdk和jre的bin目录下
C:\Windows\System32和C:\Windows\SysWOW64目录下

3、测试
package com.cao.ftlword.controller;

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

import java.io.File;

/**
 * 替换文字
 */
public class TestHyperlinks {
    public static ActiveXComponent word;
    public static Dispatch selection;
    public static Dispatch wordFile;
    public static Dispatch documents;

    //查找指定文字
    public static boolean find(String toFindText) {
        if (toFindText == null || toFindText.equals("")){
            return false;
        }
        // 从selection所在位置开始查询
        Dispatch find = Dispatch.call(selection, "Find").toDispatch();
        // 设置要查找的内容
        Dispatch.put(find, "Text", toFindText);
        // 向前查找
        Dispatch.put(find, "Forward", "True");
        // 设置格式
        Dispatch.put(find, "Format", "True");
        // 大小写匹配
        Dispatch.put(find, "MatchCase", "True");
        // 全字匹配
        Dispatch.put(find, "MatchWholeWord", "True");
        // 查找并选中
        boolean cc = Dispatch.call(find, "Execute").getBoolean();
        return cc;
    }

    // 在当前插入点插入字符串
    public static void insertText(String newText) {
        Dispatch.put(selection, "Text", newText);
    }

    public static void main(String[] args) {
        String filePath = "E:\\work-peihan\\test.docx";
        // 打开文件
        word = new ActiveXComponent("Word.Application");
        // 后台处理
        word.setProperty("Visible", false);
        Dispatch documents = word.getProperty("Documents").toDispatch();
        Dispatch wordFile = Dispatch.invoke(documents, "Open", Dispatch.Method, new Object[]{filePath, new Variant(true), new Variant(false)}, new int[1]).toDispatch();
        selection = word.getProperty("Selection").toDispatch();
        if (find("项目名称:")) {
            // 光标右移,否则找不到下一个
            Dispatch.call(selection, "MoveRight");
            insertText("测试插入项目名称1");
            // 插入的时候修改字体大小
             Dispatch font = Dispatch.get(selection, "Font").toDispatch();
            Dispatch.put(font, "Size", new Variant(7.5));
            // 光标右移,否则找不到下一个
            Dispatch.call(selection, "MoveRight");
        }
        if(find("报告编号:")){
            // 光标右移,否则找不到下一个
            Dispatch.call(selection, "MoveRight");
            insertText("测试插入报告编号1");
            // 光标右移,否则找不到下一个
            Dispatch.call(selection, "MoveRight");
        }
        if(find("项目负责人:")){
            // 光标右移,否则找不到下一个
            Dispatch.call(selection, "MoveRight");
            insertText("测试人1");
        }
        System.err.println("扫描完毕!");
        Dispatch.call(wordFile, "Close", new Variant(true));
        Dispatch.call(word, "Quit");
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值