在 Jxl 和 POI API 的帮助下,我学会了使用 Java 程序读写 Excel 文件。是否可以在宏的帮助下运行 Java 程序?
4 回答
对的,这是可能的。
实际上有很多方法,我希望你喜欢我的例子。
为了证明这一点,我创建了一个程序,其中一些文本作为参数发送,程序以修改后的版本进行响应。我做了一个可运行的罐子。第一个示例从 args 读取参数,从标准输入读取其他参数。
文件Hello.java和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());
}
}
文件Hello2.java和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());
}
}
您可以将它们保存在一个 jar 中,但随后您需要创建和使用清单(这有点矫枉过正)。
现在在 Excel 中,我添加了一个模块和对 Windows 脚本宿主对象的引用。如果您不喜欢sleep
,则可以将其替换为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
为了测试它,我将文件保存到c:\
驱动器并使用了代码:
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
就我而言,我得到了以下答复:
标准输出:你好马格斯。
标准输出:你好马格斯。
Your VBA can write the output to a file and Java can poll for file modifications periodically and read from the file. And write the data back to VBA through another file. VBA - Java integration is next to impossible unless you just want to fire a Java program from the shell through System.execute(...).
我已经使用了..注意,使用 javaw 否则会弹出一个黑色窗口
Dim result As String
Dim commandstr As String
commandstr = "javaw -jar somejar someparameter"
' try with or without cast to string
result = CStr( shellRun(commandstr) )
'somewhere from SO but forget.. sorry for missing credits
Public Function ShellRun(sCmd As String) As String
'Run a shell command, returning the output as a string'
Dim oShell As Object
Set oShell = CreateObject("WScript.Shell")
'run command'
Dim oExec As Object
Dim oOutput As Object
Set oExec = oShell.exec(sCmd)
Set oOutput = oExec.StdOut
'handle the results as they are written to and read from the StdOut object'
Dim s As String
Dim sLine As String
While Not oOutput.AtEndOfStream
sLine = oOutput.ReadLine
If sLine <> "" Then s = s & sLine & vbCrLf
Wend
ShellRun = s
End Function
比其他建议的解决方案更好的是使用 Jinx ( https://exceljava.com ) 在 Java 中创建 Excel 插件。
有关如何用 Java 编写 Excel 宏的详细信息,请参阅https://exceljava.com/docs/macros.html 。
除了宏,还可以编写用户定义的功能和菜单。
事实上,您可以使用 Java 作为 VBA 的完全替代品!请参阅https://github.com/exceljava/jinx-com4j了解如何调用 Excel 对象模型以允许您在 Java 中使用 VBA 执行所有操作。