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)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • 二十三又是谁的二十三

    二十三又是谁的二十三23岁那年你正处在哪个状态?现在呢?我,23岁,应届毕业生。生活,工作,爱情都处于人生的低谷,一穷二白,一无所有,一事无成。分享一下成长的建议吧。匿名用户23岁那年…就是去年…在22岁的时候我毕业,同时第二年准备考研,结果因为压力太大,期望太高,又失利了,但是我依然满怀信心和憧憬在我23岁那年四月,当我深爱的女孩(在这之前我追了她四年)说她要去北京时,我在毫无准备的情况下,带了2000块钱冲到北京,那会的北京还有点冷…但是我只是想打好前站,在她来的时候能提供一点帮助,在前两周里,每天面试两家公

    2022年7月25日
    11
  • 香农编码的matlab实现实验总结_香农编码C语言

    香农编码的matlab实现实验总结_香农编码C语言中南大学《信息论与编码》实验报告题目信源编码实验指导教师学院专业班级姓名学号日期目录一、香农编码…………………………………………..3实验目的………………………………………………………………………3实验要求……………..

    2025年10月18日
    1
  • laravel 5.4 导出excel表格

    laravel 5.4 导出excel表格

    2021年10月24日
    46
  • 量子光学中的分束器[通俗易懂]

    量子光学中的分束器[通俗易懂]量子光学中的分束器

    2022年4月22日
    137
  • 小程序列表跳转至详情_小程序跳转链接怎么获取

    小程序列表跳转至详情_小程序跳转链接怎么获取效果展示:列表页js部分:onLoad:function(options){varthat=this;wx.request({url:’你的接口’,data:{ 接口参数},header:{‘content-type’:’ap…

    2022年8月19日
    8
  • spdlog使用示例「建议收藏」

    spdlog使用示例「建议收藏」////Copyright(c)2015GabiMelman.//DistributedundertheMITLicense(http://opensource.org/licenses/MIT)//spdlogusageexample/*参考文献https://blog.csdn.net/haojie_superstar/article/details/89383433?ops_request_misc=&request_id=&biz_id=102

    2022年6月23日
    42

发表回复

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

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