简体   繁体   English

如何从 Java 在 Excel 电子表格中调用 VBA 代码?

[英]How do I call VBA code in an Excel spreadsheet from Java?

I have an Excel file with a large set of VBA code.我有一个包含大量 VBA 代码的 Excel 文件。 There are 4 public subroutines that take no parameters that can be called by the user when the document is opened in Excel, these manipulate the data in the various sheets as needed.当在 Excel 中打开文档时,有 4 个不带参数的公共子例程可以由用户调用,这些子例程根据需要操作各种工作表中的数据。 We have a large Java application that we would like to interact with this document by calling the Macros from the Java environment.我们有一个大型 Java 应用程序,我们希望通过从 Java 环境调用宏来与该文档进行交互。 The point is that we only have to write the VBA code once and then Java can call it for execution.关键是我们只需要编写一次 VBA 代码,然后 Java 就可以调用它来执行。 Furthermore, we want to assume that the user of the Java application does not necessarily have immediate access to Excel, but is operating on a Windows machine.此外,我们想假设 Java 应用程序的用户不一定可以立即访问 Excel,而是在 Windows 机器上操作。 How should one go about doing this?应该怎么做呢?

Do we compile the VBA code into a DLL, and call it from within Java?我们是否将 VBA 代码编译成 DLL,并从 Java 内部调用它? How do you compile the DLL, does that require the use of Visual Studio?你如何编译 DLL,这是否需要使用 Visual Studio? How do we call the DLL from Java?我们如何从 Java 调用 DLL? Should we try some sort of COM object?我们应该尝试某种 COM 对象吗?

I basically see three options for calling VBA code in Excel from a Java application:我基本上看到了从 Java 应用程序在 Excel 中调用 VBA 代码的三个选项:

  1. Java COM Bridge : There are several tools available that allow you to call COM or COM Automation components from Java. Java COM 桥:有多种工具允许您从 Java 调用 COM 或 COM 自动化组件。 Excel is such a component. Excel 就是这样一个组件。 I know of Jacob and JCom , but there might more such tools available.我知道JacobJCom ,但可能有更多这样的工具可用。

  2. Java / VBScript / COM Automation : Since you obviously don't need to pass data to the VBA code, the simplest solution is probably to write a VBScript that starts Excel, opens the document, calls the macro and closes Excel. Java / VBScript / COM 自动化:由于您显然不需要将数据传递给 VBA 代码,最简单的解决方案可能是编写一个 VBScript 来启动 Excel、打开文档、调用宏并关闭 Excel。 This script is started from Java with Runtime.getRuntime().exec("cmd /c start script.vbs");这个脚本是从 Java 启动的Runtime.getRuntime().exec("cmd /c start script.vbs");

  3. JNI : You could write a specific DLL for your applications. JNI :您可以为您的应用程序编写特定的 DLL。 It implements the JNI interface so it can be called from Java.它实现了 JNI 接口,因此可以从 Java 调用。 And its implementation uses COM calls to work with Excel.它的实现使用 COM 调用来处理 Excel。 Such a DLL is best written with VisualStudio and it's support for C++ to call COM objects.这样的 DLL 最好用 VisualStudio 编写,它支持 C++ 调用 COM 对象。

Whatever your solution will be, you basically want to execute the following commands on Excel automation interface (sample in VBScript):无论您的解决方案是什么,您基本上都希望在 Excel 自动化界面(VBScript 中的示例)上执行以下命令:

Dim xl
Set xl = CreateObject("Excel.Application")
xl.Workbooks.Open ("workbook.xlsx")
xl.Application.Run "MyMacro"
xl.Application.Quit
Set xl = Nothing 

You cannot compile VBA code into a DLL.不能将 VBA 代码编译成 DLL。 There exists no tool for that (in contrast to the full Visual Basic).不存在用于此的工具(与完整的 Visual Basic 相比)。

I hope this answer is helpful even though I didn't understand what you mean by: "we want to assume that the user of the Java application does not necessarily have immediate access to Excel, but is operating on a Windows machine."尽管我不明白您的意思,但我希望这个答案有所帮助: “我们想假设 Java 应用程序的用户不一定可以立即访问 Excel,而是在 Windows 机器上运行。”

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何从Java代码调用Excel VBA宏? - How to call an Excel VBA Macro from Java Code? 如何使用Java以编程方式将宏添加到Excel电子表格 - How do I add a Macro programmatically to an Excel spreadsheet with Java 如何从Wicket中的JavaScript代码调用Java代码? - How do I call Java code from JavaScript code in Wicket? 如何将Java封装到DLL中,然后从Excel VBA调用DLL? - How can I encapsulate Java into a DLL and then call the DLL from Excel VBA? 如何从Linux上的Java代码调用C函数 - How do I call C functions from Java code on Linux 如何将方法从JRuby调用到Java代码中? - How do i call methods from JRuby into my Java code? 如何使用Java从Excel电子表格中的多个工作表中读取特定单元格的内容? - How do you read a particular cell's content from multiple sheets in an Excel spreadsheet using Java? 如何从电子表格中读取这些坐标? - How do I read these coordinates from a spreadsheet? 主要方法中的Java代码与否? 如果没有,我如何从 main 调用新方法? - Java- code in main method or not? If not, how do I call new method from main? 如何根据我的 Java 代码中的条件有条件地调用 JavaScript 中的警报()? - How do I conditionally call an alert() in JavaScript based on conditions from my Java code?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM