如何从Java代码调用Excel VBA宏? [英] How to call an Excel VBA Macro from Java Code?

查看:3633
本文介绍了如何从Java代码调用Excel VBA宏?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要从数据库通过Java获取的数据生成Excel表格。
为此,我需要在生成Excel时调用一些VBA宏函数。
任何人可以帮助我如何从Java代码调用VBA宏?

解决方案

我真的不明白你的从数据库中的数据生成Excel表的整体方法。通常,我会使用Vivek提出的Apache POI。



但是,如果您真的需要在表单中调用Excel宏,那么需要两件事情: / p>

首先,您需要一个JAVA到COM桥,如 JACOB COM4J 或类似的工具。如果它支持自动化接口就足够了。它不需要完整的COM支持。



其次,使用JAVA到COM桥,您应该启动Excel,加载Excel工作表,运行宏,保存并关闭Excel。所以你必须调用相当于:

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


I need to generate an Excel sheet from the data fetched from a database through Java. For that, I need to call some VBA Macro functions while generating that Excel. Can anybody help me with how to call VBA Macro from Java code?

解决方案

I don't really understand your overall approach to generate Excel sheet from the data in a database. Normally, I'd use Apache POI as proposed by Vivek.

However, if you really need to call an Excel macro in a sheet, then you need two things:

First, you need a JAVA-to-COM bridge like JACOB, COM4J or a similar tool. It is sufficient if it supports automation interfaces. It doesn't need to have full COM support.

Second, using the JAVA-to-COM bridge, you should start Excel, load the Excel sheet, run the macro, save it and close Excel. So you have to call the equivalent of:

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

这篇关于如何从Java代码调用Excel VBA宏?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆