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


相关推荐

  • python股票数据分析_用Python抓取新浪的股票数据「建议收藏」

    最近做数据分析,先是找到了Tushare这个免费开源的第三方财经包,但后来用了几天之后发现,它的日交易历史数据有时候有不准确的情况,查看源代码发现,这个包的数据源是凤凰财经,而对比凤凰网站其站点的数据本身就是有出入的,所以到也不是Tushare的问题。于是百度了一圈,发现很多网友都是获取新浪的股票数据,包括其历史数据和实时数据。于是乎试了一下,发现速度还挺快,没有具体去测时间但从感官上要比Tush…

    2022年4月7日
    174
  • 词向量简介「建议收藏」

    词向量简介「建议收藏」最近深度学习技术有了突飞猛进的发展,为语音识别、图像识别、自然语言处理(NLP)提供了强大的工具,为这些领域今后的快速发展提供了新的契机。深度学习为自然语言处理带来的最令人兴奋的突破是词向量(wordembedding)技术。词向量技术是将词转化成为稠密向量,并且对于相似的词,其对应的词向量也相近。在自然语言处理应用中,词向量作为深度学习模型的特征进行输入。因此,最终模型的效果很大程度上取

    2022年6月6日
    41
  • 视觉SLAM——特征点法与直接法对比以及主流开源方案对比 ORB LSD SVO DSO

    视觉SLAM——特征点法与直接法对比以及主流开源方案对比 ORB LSD SVO DSO单目视觉SLAM可以根据其前端视觉里程计或是后端优化的具体实现算法进行分类:前端可以分为特征点法与直接法,后端可以分为基于滤波器和基于非线性优化。其中在后端上目前已经公认基于非线性优化的方法在同等计算量的情况下,比滤波器能取得更好的结果。而前端的两种方法则各有优劣。本文将具体分析直接法相较于特征点法的优劣处,并具体介绍目前主流的开源方案,以供大家参考。

    2022年5月13日
    40
  • 一个多道批处理仅有p1p2_nx300h的缺点

    一个多道批处理仅有p1p2_nx300h的缺点(注:%0就是该batch文件的文件名 )%~dp0的意思是 更改当前目录为批处理文件的目录 比如你有个批处理a.bat在D:/qq文件夹下  a.bat内容为 cd/d%~dp0 在这里 cd/d%~dp0的意思就是cd/dd:/qq %0代表批处理本身 d:/qq/a.bat ~dp是变量扩充 d既是扩充到分区号 d: p就是扩充到路径 

    2022年9月16日
    1
  • 谷歌打开微信定位服务器地址,使用Chrome修改user agent模拟微信内置浏览器

    谷歌打开微信定位服务器地址,使用Chrome修改user agent模拟微信内置浏览器很多时候,我们需要模拟微信内置浏览器,今天教大家用chrome简单模拟。如图设置:F12或者右键审查元素进入开发者模式,点击Emulation,然后点击Network,把Spoofuseragent改成Other,并把下面的带复制进去,有三句,大家自己尝试。回车然后刷新页面即可。Mozilla/5.0(iPhone;CPUiPhoneOS5_1likeMacOSX)Appl…

    2022年6月1日
    56
  • python画图常用颜色

    python画图常用颜色’.’pointmarker’,’pixelmarker’o’circlemarker’v’triangle_downmarker’^’triangle_upmarker’<’triangle_leftmarker’>’triangle_rightmarker’…

    2022年6月12日
    38

发表回复

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

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