How to call an Excel VBA Macro from Java Code?

前端 未结 5 1449
孤城傲影
孤城傲影 2020-12-15 01:36

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 anyb

5条回答
  •  情书的邮戳
    2020-12-15 02:28

    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
    

提交回复
热议问题