把VBA转换成JAVA,从Java调用VBA代码

Greetings StackOverflow,

I've searched the greater Internet and have been encumbered by numerous wastes of posts, or answers that explain the exact opposite order of what I'm attempting.

I have an Excel file with a large set of VBA code. 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. We have a large Java application that we would like to interact with this document by calling the Macros from the Java environment. The point is that we only have to write the VBA code once and then Java can call it for execution. 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. How should one go about doing this?

Do we compile the VBA code into a DLL, and call it from within Java? How do you compile the DLL, does that require the use of Visual Studio? How do we call the DLL from Java? Should we try some sort of COM object?

Please note, as the the most common answer to my question elsewhere, I understand quite clearly how to call Java from within VBA, but not to call the VBA from Java.

Thank you in advance.

解决方案

I basically see three options for calling VBA code in Excel from a Java application:

Java COM Bridge: There are several tools available that allow you to call COM or COM Automation components from Java. Excel is such a component. I know of Jacob and JCom, but there might more such tools available.

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. This script is started from Java with Runtime.getRuntime().exec("cmd /c start script.vbs");

JNI: You could write a specific DLL for your applications. It implements the JNI interface so it can be called from Java. And its implementation uses COM calls to work with Excel. Such a DLL is best written with VisualStudio and it's support for C++ to call COM objects.

Whatever your solution will be, you basically want to execute the following commands on Excel automation interface (sample in 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. There exists no tool for that (in contrast to the full 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."

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值