我有这个Excel宏:
Function Calculate_Something(StartDate As Date, EndDate As Date) As Double
//some math is here, not important
Calculate_Something = Result
End Function
我想将我的日期传递给这个宏,在我的 Java 程序中执行它,最后获得返回值并将其分配给我在 Java 中的值。
我已经使用此函数创建了 VBS 脚本,并尝试在 Java 中像这样执行它:
String[] parms = {"wscript", "calc.vbs", "2017-02-06 09:38:36", "2017-02-06 12:47:41"};
Runtime.getRuntime().exec(parms);
但这没有用。你知道我该怎么做吗?
你会想要使用cscript.exe
代替wscript.exe
,它们都是同一主机,但一个是为 GUI 设计的,另一个是为命令行设计的。
将 VBScript 函数修改为输出Result向屏幕 https://stackoverflow.com/q/4388879/692942 (执行的命令输出流)然后使用检索它Process
从调用派生的对象Runtime.getRuntime().exec(parms);
.
有一种方法是Process
称为的对象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();
}
有用的链接
- vbscript输出到控制台 https://stackoverflow.com/q/4388879/692942
- 什么是输入流和输出流?我们为什么使用它们以及何时使用它们? https://stackoverflow.com/q/1830698/692942
- 了解 getInputStream 和 getOutputStream https://stackoverflow.com/q/22563986/692942
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)