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


相关推荐

  • [数据库] 一文搞懂case when所有使用场景「建议收藏」

    [数据库] 一文搞懂case when所有使用场景「建议收藏」前几天,为了给产品分析当前用户数据结构,写sql的时候使用到了casewhen,今天来总结一下casewhen的使用方法,以此为戒,感觉写的不好请拍砖,感觉写的还可以,给哥们点个赞,或者回复一下,让我意识到我不是一个人在战斗,好了废话不多说了,进入正题。关于casewhen的使用情况,我总结下来有三种,第一、等值转换,第二、范围转换,第三、列转行操作。等值转换咱们在设计数据库的…

    2025年9月17日
    5
  • MySQL字符拼接_mysql查询字符串拼接

    MySQL字符拼接_mysql查询字符串拼接第一种:mysql自带语法CONCAT(string1,string2,…),此处是直接把string1和string2等等的字符串拼接起来(无缝拼接哦)说明:此方法在拼接的时候如果有一个值为NULL,则返回NULL如:1.SELECTCONCAT(“name=”,”lich”,NULL)AStest;2.SELECTCONCAT(“name=”,”lich”)AStest;第…

    2022年9月28日
    4
  • JVM常见面试题及详解

    JVM常见面试题及详解虚拟机是一种抽象化的计算机,通过在实际的计算机上仿真模拟各种计算机功能来实现的。Java虚拟机有自己完善的硬体架构,如处理器、堆栈、寄存器等,还具有相应的指令系统。Java虚拟机屏蔽了与具体操作系统平台相关的信息,使得Java程序只需生成在Java虚拟机上运行的目标代码(字节码),就可以在多种平台上不加修改地运行。简单来说JVM是用来解析和运行Java程序的。

    2022年8月29日
    5
  • awk的内置函数_awk引用变量

    awk的内置函数_awk引用变量awk 系列Part10:如何使用 awk 内置变量

    2022年4月22日
    59
  • 南京字节跳动公司招聘_字节跳动入职期权有多少

    南京字节跳动公司招聘_字节跳动入职期权有多少1.前言相信大家对ZooKeeper应该不算陌生。但是你真的了解ZooKeeper到底有啥用不?如果别人/面试官让你给他讲讲对于ZooKeeper的认识,你能回答到什么地步呢?拿我自己来说吧!我本人曾经使用Dubbo来做分布式项目的时候,使用了ZooKeeper作为注册中心。为了保证分布式系统能够同步访问某个资源,我还使用ZooKeeper做过分布式锁。另外,我在学习Kafka的时候,知道Kafka很多功能的实现依赖了ZooKeeper。前几天,总结项目经验的时候,

    2022年10月3日
    3
  • flume整合kafka

    flume整合kafka整合流程Flume发送数据到Kafka上主要是通过KafkaSink来实现的,主要步骤如下:1.启动Zookeeper和Kafka这里启动一个单节点的Kafka作为测试:#启动ZookeeperzkServer.shstart#启动kafkabin/kafka-server-start.shconfig/server.properties2.创建主题创建一个主题flume-kafka,之后Flume收集到的数据都会发到这个主题上:#…

    2022年6月23日
    30

发表回复

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

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