Microsoft Excel Macro to run Java program

前端 未结 4 490
一向
一向 2020-12-03 06:12

I have learnt to read and write an Excel file using a Java program with the help of Jxl and POI API. Is it possible to run a Java program with the help of macros?

4条回答
  •  鱼传尺愫
    2020-12-03 06:18

    i've used it.. Attention, use javaw otherwise a black window is popping up

    Dim result As String
    Dim commandstr As String
    
    commandstr = "javaw -jar somejar someparameter"
    
    
    ' try with or without cast to string
    result = CStr( shellRun(commandstr) )
    
    
    'somewhere from SO but forget.. sorry for missing credits
    
    Public Function ShellRun(sCmd As String) As String
    
        'Run a shell command, returning the output as a string'
    
        Dim oShell As Object
        Set oShell = CreateObject("WScript.Shell")
    
        'run command'
        Dim oExec As Object
        Dim oOutput As Object
        Set oExec = oShell.exec(sCmd)
        Set oOutput = oExec.StdOut
    
        'handle the results as they are written to and read from the StdOut object'
        Dim s As String
        Dim sLine As String
        While Not oOutput.AtEndOfStream
            sLine = oOutput.ReadLine
            If sLine <> "" Then s = s & sLine & vbCrLf
        Wend
    
        ShellRun = s
    
    End Function
    

提交回复
热议问题