如何使用Java中的参数运行VBS函数并将结果赋给变量

时间:2022-10-16 07:28:28

I have this excel macro:

我有这个excel宏:

Function Calculate_Something(StartDate As Date, EndDate As Date) As Double

//some math is here, not important

Calculate_Something = Result
End Function

And I want to pass my dates to this macro, execute it in my Java program and finally get returned value and assign it to my value in Java.

我想把我的日期传递到这个宏,在我的Java程序中执行它,最后得到返回值并在Java中赋值给我的值。

I have created VBS script with this function and try to execute it like this in Java:

我用这个函数创建了VBS脚本,并尝试在Java中这样执行:

 String[] parms = {"wscript", "calc.vbs", "2017-02-06 09:38:36", "2017-02-06 12:47:41"};
 Runtime.getRuntime().exec(parms);

But it didn't work. Do you know how could I do that?

但它不工作。你知道我怎么做吗?

1 个解决方案

#1


1  

You will want to use cscript.exe instead of wscript.exe, they are both the same host but one is designed for GUI while the other is designed for the command line.

您将希望使用cscript。exe不是wscript。exe,它们都是相同的主机,但是一个是为GUI设计的,另一个是为命令行设计的。

Modify the VBScript function to output the Result to screen (the executed command output stream) then retrieve it using the Process object derived from calling Runtime.getRuntime().exec(parms);.

修改VBScript函数,将结果输出到屏幕(已执行的命令输出流),然后使用调用Runtime.getRuntime().exec(parms)派生的Process对象检索结果。

There is a method of the Process object called getInputStream() which should allow you to access and read the value you returned by the script output.

有一个名为getInputStream()的过程对象的方法,该方法应该允许您访问并读取脚本输出返回的值。

try {
    String[] parms = {"cscript", "calc.vbs", "2017-02-06 09:38:36", "2017-02-06 12:47:41"};
    Process p = Runtime.getRuntime().exec(parms);

    // Get Input Stream from the Process
    BufferedReader is = new BufferedReader(new InputStreamReader(p.getInputStream()));

    // Do something with stream, read etc.
    String line;
    while ((line = is.readLine()) != null)
        System.out.println(line);

} catch (Exception ex) {
    ex.printStackTrace();
}

Useful Links

#1


1  

You will want to use cscript.exe instead of wscript.exe, they are both the same host but one is designed for GUI while the other is designed for the command line.

您将希望使用cscript。exe不是wscript。exe,它们都是相同的主机,但是一个是为GUI设计的,另一个是为命令行设计的。

Modify the VBScript function to output the Result to screen (the executed command output stream) then retrieve it using the Process object derived from calling Runtime.getRuntime().exec(parms);.

修改VBScript函数,将结果输出到屏幕(已执行的命令输出流),然后使用调用Runtime.getRuntime().exec(parms)派生的Process对象检索结果。

There is a method of the Process object called getInputStream() which should allow you to access and read the value you returned by the script output.

有一个名为getInputStream()的过程对象的方法,该方法应该允许您访问并读取脚本输出返回的值。

try {
    String[] parms = {"cscript", "calc.vbs", "2017-02-06 09:38:36", "2017-02-06 12:47:41"};
    Process p = Runtime.getRuntime().exec(parms);

    // Get Input Stream from the Process
    BufferedReader is = new BufferedReader(new InputStreamReader(p.getInputStream()));

    // Do something with stream, read etc.
    String line;
    while ((line = is.readLine()) != null)
        System.out.println(line);

} catch (Exception ex) {
    ex.printStackTrace();
}

Useful Links