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)


相关推荐

  • pycharm中pyqt5使用方法_对中仪使用方法视频

    pycharm中pyqt5使用方法_对中仪使用方法视频PyCharm中PyQt的使用方法一级目录二级目录三级目录一级目录二级目录三级目录

  • 交叉线 与 直通线

    交叉线 与 直通线交叉线  交叉线:又叫反线,线序按照一端568B,一端568A的标准排列好线序,并用RJ45水晶头夹好。      具体的线序制作方法是:一端采用568B(即白橙,橙,白绿,蓝,白蓝,绿,白棕,棕的顺序)做线标准不变,另一端在这个基础上将这八根线中的1,3号线和2,6号线互换一下位置,这时网线的线序就变成了:1、白绿、2、绿、3、白橙、4、蓝、5、白蓝、6、橙、7、白棕、8、棕(即正线的1,

  • pycharm中格式化快捷键是什么_pycharm快捷键大全

    pycharm中格式化快捷键是什么_pycharm快捷键大全(1)快捷键:Ctrl+Alt+L可以将代码格式工整化(2)鼠标点击

    2022年10月27日
  • 隐式转换函数_隐函数可以转化为显函数

    隐式转换函数_隐函数可以转化为显函数隐式转换函数是以implicit关键字声明的带有单个参数的函数。这种函数将会自动应用,将值从一种类型转换为另一种类型objectDemo1Main{defmain(args:Array[String]):Unit={valnum:Int=f1(3.5);valnum1:Int=3.5;print(num)}implic…

  • vmware16安装centos8_虚拟机centos6安装教程

    vmware16安装centos8_虚拟机centos6安装教程VMware12安装centOS8(vm虚拟机安装centos8教程)前几天Centos8发布了,尽管他是8的第一个版本,有着许多的bug那么今天我们就在VM12上面安装centOS8吧,8这个图形化界面我个人感觉有点丑首先下载iso文件百度下点击进入官网点击马上获得centos然后选择这个选择离你近的镜像地址,点击下载打开vm12点击新建虚拟机点击下一步,如下图这样…

  • 移动端touchmove卡顿

    网上提到的优化技术:1.window.requestAnimationFrame()  a.不用定义时间间隔,避免间隔长:卡顿,间隔短:浏览器漏帧的情况。由浏览器在绘制完一帧后自动再次调用绘制下一帧。2.transform3D代替transform3.增添惯性滑动效果,(不要小看惯性效果,效果会提升一个档次)。转载于:https://www.cnblogs.com/…

发表回复

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

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