Java 使用 jacob(1.19) 修改配置以及实际应用Bartender、Word、Excel

一、IDEA  使用配置
1、下载  jacob1.19  文件   
jacob-1.19.zip
链接:https://pan.baidu.com/s/1F1yKd1PvdeEliuf6ihTtyw 
提取码:eoyq

下载jacob的,解压之后得到里面的三个文件:

1. jacob.jar                                                            
2. jacob-1.19-x64.dll     
3. jacob-1.19-x86.dll   
以64位为例:
1.将jacob.jar放入项目,  将jacob.jar放入C:\Program Files\Java\jre1.8.0_241\lib\ext文件夹中
2.将(jacob-1.19-x64.dll&& jacob-1.19-x86.dll )放入Java 的jdk 的bin目录和/WINDOWS/system32目录下
配置完成

将本地自定义jar安装到maven仓库

mvn install:install-file -DgroupId=com.jacob -DartifactId=jacob -Dversion=1.19 -Dpackaging=jar -Dfile=jacob.jar


点击 jacob-1.19.bat

二、发布运行Java程序
1、2.将(jacob-1.19-x64.dll&& jacob-1.19-x86.dll )放入Java 的jre 的bin目录下

三、pom引入

<dependency>
    <groupId>com.jacob</groupId>
    <artifactId>jacob</artifactId>
    <version>1.19</version>
</dependency>

四、实际应用
 

调用 BartEnder  打印标签模板
 

 public static void main(String[] args) {
 String filepath = "C:\\Users\\Administrator\\Desktop\\临时文件\\文档1.btw";
        System.out.println("开始打印word");
        //初始化COM线程   要运行COM组件,需要先添加jacob包
        ComThread.InitSTA();
        //使用Jacob创建 ActiveX部件对象:
        ActiveXComponent btApp = new ActiveXComponent("BarTender.Application");
        //加载Bartender模板控件
        Dispatch btFormats = btApp.getProperty("Formats").toDispatch();

        //调用模板,bo.getTemplatePath()方法(获取模版路径)你自己定义
        Dispatch btFormat = Dispatch.call(btFormats, "Open", filepath, false, "Deli DL-888B(NEW)555").toDispatch();
//        // 型号
//        Dispatch.call(btFormat, "SetNamedSubStringValue", "code", "Power:24VDC200mA V4.1");
//        // 品牌
//        Dispatch.call(btFormat, "SetNamedSubStringValue", "name", "IDX:001");


        //设置打印份数,默认是1份可以不设置
        Dispatch printSetup = Dispatch.get(btFormat, "PrintSetup").toDispatch();
        Dispatch.put(printSetup, "IdenticalCopiesOfLabel", 1);

        // 打印,Bartender会自动选择模板中设置的打印机打印
        Dispatch.call(btFormat, "PrintOut", false, false);

        //打印完成,关闭模板控件
        Dispatch.call(btFormat, "Close", 0);

        //关闭Bartender进程
        Dispatch.call(btApp, "Quit", 0);

        ComThread.Release();
    }

调用Word      动态 替换word 中的 图片

  //动态 替换word 中的 图片
    public static void main(String[] args)throws Exception {
        //加载文档
        Document doc = new Document();
        doc.loadFromFile("C:\\Users\\Administrator\\Desktop\\临时文件\\PaymentApplicationForm1.docx");

        TextSelection[] textSelection = doc.findAllString("图片2",true,false);
        int index ;
        //加载图片替换文本字符串
        for (Object obj : textSelection) {
            TextSelection Selection = (TextSelection)obj;
            DocPicture pic = new DocPicture(doc);
            pic.loadImage("C:\\Users\\Administrator\\Pictures\\l.jpg");
            pic.setWidth(50);
            pic.setHeight(50);
            TextRange range = Selection.getAsOneRange();
            index = range.getOwnerParagraph().getChildObjects().indexOf(range);
            range.getOwnerParagraph().getChildObjects().insert(index,pic);
            range.getOwnerParagraph().getChildObjects().remove(range);
        }
        //保存文档
        doc.saveToFile("C:\\Users\\Administrator\\Desktop\\临时文件\\PaymentApplicationForm1.docx", FileFormat.Docx_2013);
        doc.dispose();

    }

获取excel 动态变更没有保存的值

public static void main(String[] args) {
        ActiveXComponent excel = new ActiveXComponent("Excel.Application");
//        String filePath = "C:\\Users\\Administrator\\Desktop\\临时文件\\PLC_OPC20240810_Final_Version.xlsx";
        String filePath = "C:\\PLC_OPC20240810_Final_Version.xlsx";
        try {
            excel.setProperty("Visible", new Variant(true));
            Dispatch workbooks = excel.getProperty("Workbooks").toDispatch();
            Dispatch workbook = Dispatch.call(workbooks, "Open", filePath).toDispatch();
            Dispatch sheets = Dispatch.get(workbook, "Sheets").toDispatch();
            // 获取第一个 sheet
            Dispatch sheet = Dispatch.call(sheets, "Item", new Variant(1)).toDispatch();
            //获取  第一个 sheet (D3)
            Dispatch cell = Dispatch.invoke(sheet, "Range", Dispatch.Get, new Object[]{"D177"}, new int[1]).toDispatch();
            //在循环中监视单元格值
            String previousValue = "";
            while (true) {
                try {
                    Variant cellValue = Dispatch.get(cell, "Value2");
                    String currentValue = cellValue.toString();
                    if (!currentValue.equals(previousValue)) {
                        System.out.println("Value of cell A1 changed: " + currentValue);
                        previousValue = currentValue;
                    }
                } catch (ComFailException e) {
                    System.out.println("Failed to get cell value: " + e.getMessage());
                }
                try {
                    Thread.sleep(1000); // Check every second
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        } finally {
            // excel.invoke("Quit");
        }
    }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值