How to call an Excel VBA Macro from Java Code?

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

    solution that works for me: java code:

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

    myVBS.vbs script:

    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 
    

提交回复
热议问题