简体   繁体   English

如何从Java代码调用Excel VBA宏?

[英]How to call an Excel VBA Macro from Java Code?

I need to generate an Excel sheet from the data fetched from a database through Java. 我需要从通过Java从数据库中获取的数据生成Excel工作表。 For that, I need to call some VBA Macro functions while generating that Excel. 为此,我需要在生成Excel时调用一些VBA宏函数。 Can anybody help me with how to call VBA Macro from Java code? 任何人都可以帮助我如何从Java代码调用VBA宏?

I don't really understand your overall approach to generate Excel sheet from the data in a database. 我真的不了解从数据库中的数据生成Excel工作表的整体方法。 Normally, I'd use Apache POI as proposed by Vivek. 通常,我会使用Vivek提出的Apache POI。

However, if you really need to call an Excel macro in a sheet, then you need two things: 但是,如果您确实需要在工作表中调用Excel宏,那么您需要两件事:

First, you need a JAVA-to-COM bridge like JACOB , COM4J or a similar tool. 首先,您需要一个JAVA到COM桥,如JACOBCOM4J或类似的工具。 It is sufficient if it supports automation interfaces. 如果它支持自动化接口就足够了。 It doesn't need to have full COM support. 它不需要完整的COM支持。

Second, using the JAVA-to-COM bridge, you should start Excel, load the Excel sheet, run the macro, save it and close Excel. 其次,使用JAVA-to-COM桥接器,您应该启动Excel,加载Excel工作表,运行宏,保存并关闭Excel。 So you have to call the equivalent of: 所以你必须调用相当于:

Set Wb = Application.Workbooks.Open FileName
Application.Run MacroName
Wb.Save
Application.Quit

If you can't use JACOB or COM4J you can make a Visual Basic Script and run the script from your Java program. 如果您不能使用JACOBCOM4J ,则可以创建Visual Basic脚本并从Java程序运行该脚本。

To create the script open notepad and write something like this: 要创建脚本打开记事本并写下这样的内容:

Set objExcel = CreateObject("Excel.Application")
Set objWorkbook = objExcel.Workbooks.Open("myExcel.xlsm")

objExcel.Application.Run "myExcel.xlsm!MyMacroName" 
objExcel.ActiveWorkbook.Close

objExcel.Application.Quit
WScript.Quit

Save it as myVBS.vbs and you can call it like this from your Java code: 将它保存为myVBS.vbs ,您可以从Java代码中调用它:

cmd = "you_path\\myVBS.vbs";
Runtime.getRuntime().exec(cmd);

solution that works for me: java code: 适用于我的解决方案:java代码:

try {
Runtime.getRuntime().exec("wscript yourPth\\myVBS.vbs");
} catch (IOException e) {
System.out.println(e);
System.exit(0);
}

myVBS.vbs script: myVBS.vbs脚本:

Set objShell = CreateObject("WScript.Shell")
Dim cur
cur = "urpath to myVBS.vbs script"
WScript.Echo cur

ExcelMacroExample

Sub ExcelMacroExample() 

Dim xlApp 
Dim xlBook 
Dim xlsFile
xlsFile = cur & "\myExcel.xlsm"

Set xlApp = CreateObject("Excel.Application") 
Set xlBook = xlApp.Workbooks.Open(xlsFile) 
xlApp.Run "moduleName"
xlApp.Save
xlApp.Quit 

End Sub 

I am not sure if it is possible to call macro directly from Java. 我不确定是否可以直接从Java调用宏。 But you can populate the data in excel sheet & call the macro when the user opens the excel sheet for the first time. 但是,当用户第一次打开Excel工作表时,您可以在Excel工作表中填充数据并调用宏。 You would be able to populate data in a excel sheet containing macros using Apache POI tool - http://poi.apache.org/spreadsheet/index.html 您可以使用Apache POI工具在包含宏的Excel工作表中填充数据 - http://poi.apache.org/spreadsheet/index.html

You might also be able to capture an event in excel when a worksheet changes you can have the even call whatever macro you want it to, so if you want to call the macro "Blue" you can write "blue" in a hidden worksheet then Excel will capture the change, when you capture the change you can see what was written and do some if ifelse statements to get to the macro you want to call for that instance. 您可能还能够在工作表更改时在Excel中捕获事件,您甚至可以调用所需的宏,因此如果要调用宏“蓝色”,则可以在隐藏的工作表中写入“蓝色” Excel将捕获更改,当您捕获更改时,您可以看到写入的内容,并执行一些ifelse语句以获取要为该实例调用的宏。 Not really good coding, but an easy workaround. 不是很好的编码,但一个简单的解决方法。 I would do the other methods myself though. 我会自己做其他方法。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM