How to call an Excel VBA Macro from Java Code?

前端 未结 5 1446
孤城傲影
孤城傲影 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:38

    If you can't use JACOB or COM4J you can make a Visual Basic Script and run the script from your Java program.

    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:

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

提交回复
热议问题