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

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

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

(0)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • msm8953之spi配置

    msm8953之spi配置//===========================================spi5msm8953.dtsialiases{spi5=&spi_5;};spi_5:spi@7af5000{/*BLSP1QUP5*/compatible=”qcom,spi-qup-v2″;…

    2022年10月18日
    6
  • PMM

    PMM

    2021年5月28日
    140
  • 2020 年中国程序员薪资和生活现状调查报告[通俗易懂]

    2020 年中国程序员薪资和生活现状调查报告[通俗易懂]作者|程序员客栈来源|ID:proginnwx根据中国互联网络信息中心(CNNIC)近日发布第44次《中国互联网络发展状况统计报告》。截至2019年06月,中国网民规模为8.54亿,较2018年底增加2598万。网上外卖用户规模达4.21亿,较2018年底增长1516万;网络视频用户规模达7.59亿,较2018年底增长3391万;我…

    2022年9月1日
    5
  • Java的反射机制原理[通俗易懂]

    Java的反射机制原理[通俗易懂]一、什么是反射:(1)Java反射机制的核心是在程序运行时动态加载类并获取类的详细信息,从而操作类或对象的属性和方法。本质是JVM得到class对象之后,再通过class对象进行反编译,从而获取对象的各种信息。(2)Java属于先编译再运行的语言,程序中对象的类型在编译期就确定下来了,而当程序在运行时可能需要动态加载某些类,这些类因为之前用不到,所以没有被加载到JVM。通过反射,可以在运行时动态地创建对象并调用其属性,不需要提前在编译期知道运行的对象是谁。二.反射机制的概念指在运行状态中..

    2022年7月8日
    35
  • laravel模版共用数据解决方法

    laravel模版共用数据解决方法

    2021年7月6日
    69
  • SSH Config 允许使用root密码登陆 PermitRootLogin[通俗易懂]

    SSH Config 允许使用root密码登陆 PermitRootLogin[通俗易懂]问题:我用ssh连接服务器的时候,如果不设置密钥登陆,就会登陆失败,没有办法通过密码登陆解决:首先设置允许通过密码登陆,设置PasswordAuthentication为yes设置在/etc/ssh/sshd_config中设置PermitRootLogin为yes重启sshservicesudoservicesshrestart…

    2022年6月6日
    34

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

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