excel宏 java,Microsoft Excel宏运行Java程序

excel宏 java,Microsoft Excel宏运行Java程序IhavelearnttoreadandwriteanExcelfileusingaJavaprogramwiththehelpofJxlandPOIAPI.IsitpossibletorunaJavaprogramwiththehelpofmacros?解决方案Yes,itispossible.Therearequit…

大家好,又见面了,我是你们的朋友全栈君。

excel宏 java,Microsoft Excel宏运行Java程序

I have learnt to read and write an Excel file using a Java program with the help of Jxl and POI API. Is it possible to run a Java program with the help of macros?

解决方案

Yes, it is possible.

There are quite a few ways actually and I hope you like my examples.

To demonstrate this, I create a program where some text is send as arguments and program responds with an altered version of it. I made a runnable jar of it. First example reads the argument from args and other from standard input.

File Hello.java and H1.jar:

public class Hello {

public static void main(String[] args) {

StringBuilder sb = new StringBuilder(“Hello”);

if (args.length > 0)

sb.append(‘ ‘).append(args[0]);

System.out.println(sb.append(‘.’).toString());

}

}

File Hello2.java and H2.jar:

import java.util.Scanner;

public class Hello2 {

public static void main(String[] args) {

Scanner sc = new Scanner(System.in);

StringBuilder sb = new StringBuilder(“Hello”);

sb.append(‘ ‘).append(sc.nextLine());

System.out.println(sb.append(‘.’).toString());

}

}

You can save them in a single jar, but then you need create and use a manifest (that’s a bit overkill).

Now in Excel I add a module and a reference to Windows Script Host Object. If you do not like the sleep, then you can replace it with DoEvents:

‘add a reference to Windows Script Host Object Model

‘for example : Tools-References

Option Explicit

Private Declare Sub Sleep Lib “kernel32” (ByVal dwMilliseconds As Long)

Private Sub RunSleep( _

exec As WshExec, _

Optional timeSegment As Long = 20 _

)

Do While exec.Status = WshRunning

Sleep timeSegment

Loop

End Sub

Private Function RunProgram( _

program As String, _

Optional command As String = “” _

) As WshExec

Dim wsh As New WshShell

Dim exec As WshExec

Set exec = wsh.exec(program)

Call exec.StdIn.WriteLine(command)

Call RunSleep(exec)

Set RunProgram = exec

End Function

And to test it I saved the files to c:\ drive and used the code:

Public Sub Run()

Dim program As WshExec

Set program = RunProgram(“java -jar “”C:\\H1.jar”” Margus”)

Debug.Print “STDOUT: ” & program.StdOut.ReadAll

Set program = RunProgram(“java -jar “”C:\\H2.jar”, “Margus”)

Debug.Print “STDOUT: ” & program.StdOut.ReadAll

End Sub

In my case I get a responce of :

STDOUT: Hello Margus.

STDOUT: Hello Margus.

If you found this useful, do not forget to upvote :D

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。

发布者:全栈程序员-用户IM,转载请注明出处:https://javaforall.cn/136394.html原文链接:https://javaforall.cn

【正版授权,激活自己账号】: Jetbrains全家桶Ide使用,1年售后保障,每天仅需1毛

【官方授权 正版激活】: 官方授权 正版激活 支持Jetbrains家族下所有IDE 使用个人JB账号...

(0)
blank

相关推荐

  • 判断处理器是大端还是小端_网络字节序是大端还是小端

    判断处理器是大端还是小端_网络字节序是大端还是小端最近用杰理AC6966B调试博通的BK9527U段发射芯片,一直没调通,经过测试IIC通讯是通,硬件还是好的,但是怎么都调不到与接收端成功连接。最后咨询原厂得知提供的demo代码是大端编码模式的MCU代码,如果是小端模式,在部分写寄存器操作的过程中,如果直接传指针数据会反掉。杰理的MCU应该是小端模式,平时写代码用memcpy函数操作指针赋值最后得到的结果都是低位在前。为了进一步验证,网上找了一段代码验证,原理跟memcpy给指针赋值是类似的,最后成功验证到杰理的AC,AD系列都是小端模式:

  • 深入浅出地理解机器人手眼标定

    深入浅出地理解机器人手眼标定所谓手眼系统,就是人眼镜看到一个东西的时候要让手去抓取,就需要大脑知道眼镜和手的坐标关系。如果把大脑比作B,把眼睛比作A,把手比作C,如果A和B的关系知道,B和C的关系知道,那么C和A的关系就知道了,也就是手和眼的坐标关系也就知道了。相机知道的是像素坐标,机械手是空间坐标系,所以手眼标定就是得到像素坐标系和空间机械手坐标系的坐标转化关系。在实际控制中,相机检测到目标在图像中的像素位置…

  • 基于java的动态口令_java动态口令登录实现过程详解

    基于java的动态口令_java动态口令登录实现过程详解1.实现一个ItsClient客户端用来实例化调用验证功能publicclassItsClient{privatestaticfinalStringrouting=”/v1.0/sectoken/otp_validation”;//!HTTPS消息验证地址privateStringhttpsVerifyUrl=””;//!otpipAddrprivateSt…

  • curl_init()

    curl_init()
    $ch=curl_init();
    $c_url=’http://?’;
     $c_url_data=”product_id=”.$product_id.”&type=”.$type.””;
     curl_setopt($ch,CURLOPT_URL,$c_url);
     curl_setopt($ch,CURLOPT_POST,1);
     curl_setopt($ch,CURLOPT_RETURNTRANSFER,true);

  • CString Find & ReverseFind[通俗易懂]

    CString Find & ReverseFind[通俗易懂]CStringszTemp="d:\VSproject\IniPractice\debug\IniPractice.exe";intnIndex1=szTemp.Find(‘\\’); //nIndex1=2;从左往右查找,并给出索引值intnIndex2=szTemp.ReverseFind(‘\\’); //nIndex2=31;从右往左查找,并反向给出索引值…

  • git下载安装教程

    git下载安装教程git下载安装教程前言:因为最近突然对使用github搭建一个自己的网站并绑定域名特别着迷,但是前提条件是必须得安装git,于是便把安装过程记录下来,便利自己,帮助他人。1.访问git官网下载最新版本git官方网页:https://git-scm.com/download/win在git官网中,有不同操作系统下的git,选择符合自己电脑版本的进行下载就可以了这里我选择的windows,然后根据自己电脑是32位还是64位,在下面两个选项中选择选择好了静待其下好就好了或许会有下载缓慢或无法下

发表回复

您的电子邮箱地址不会被公开。

关注全栈程序员社区公众号