使用 C# 触发 Essbase 宏

Trigering Essbase Macro with C#

我正在尝试使一些 Excel 报告自动化。目前我需要从 Essbase 服务器检索一些数据,为了实现这一点,我创建了一个宏来检索和设置 Excel sheet 中的数据,我的 VBA 代码是以下:

Option Explicit
Declare Function EssVRetrieve Lib "ESSEXCLN.XLL" (ByVal sheetName As Variant, ByVal range As Variant, ByVal lockflag As Variant) As Long
Declare Function EssVConnect Lib "ESSEXCLN.XLL" (ByVal sheetName As Variant, ByVal userName As Variant, ByVal password As Variant, ByVal server As Variant, ByVal application As Variant, ByVal database As Variant) As Long
Declare Function EssVDisconnect Lib "ESSEXCLN.XLL" (ByVal sheetName As Variant) As Long

Sub Essbase_Update_Pulls()
Dim rangeString As String
rangeString = "B3:AC5033"
MsgBox ("Starting macro")
Dim wbSrc As Workbook
Dim m As Variant
Dim mySheetname As Variant, myUserName As Variant, myPassword As Variant, myServer, myApp As Variant, myDB As Variant
Dim lockflag As Integer
Dim myrng As range
Dim x As Integer
Dim y As Integer
Dim z As Integer
Dim strMsgTxt As String
Dim blnRetVal As Boolean

Set wbSrc = ActiveWorkbook

Set myrng = range(rangeString)

lockflag = 1
MsgBox ("Data set")
        mySheetname = "Sheet"
        myServer = "Server"
        myApp = "App"
        myDB = "DB"
        myUserName = "User"
        myPassword = "Pass"

MsgBox ("Trying connection")
        x = EssVConnect(mySheetname, myUserName, myPassword, myServer, myApp, myDB)
        MsgBox (CStr(x))
        If x < 0 Then
           blnRetVal = False
           strMsgTxt = "Essbase Login - Local Failure"
           MsgBox (strMsgTxt)
        ElseIf x > 0 Then
           blnRetVal = False
           strMsgTxt = "Essbase Login - Server Failure"
           MsgBox (strMsgTxt)
        Else
           blnRetVal = True
           strMsgTxt = "Success"
           MsgBox ("Connection Succeeded")
           y = EssVRetrieve(mySheetname, myrng, lockflag)
            If y = 0 Then
                MsgBox ("Retrieve successful.")
                z = EssVDisconnect(mySheetname)
                If z = 0 Then
                    MsgBox ("Disconnect Succeed.")
                    Else
                    MsgBox ("Disconnect failed.")
                End If
                Else
                MsgBox ("Retrieve failed.")
            End If
        End If
End Sub

变量 x 应该 return 状态代码(0 表示成功,其他表示失败)。 所以技巧来了,每当我在 Excel 中 运行 这个宏时,它 运行 是完美的,但是当我使用 xlApp.Run("Essbase_Update_Pulls"); 从 C# 调用它时,它 return 是状态码 -3。 通过一些研究,我发现每当在代码中创建 Excel 应用程序时,它都没有加载加载项,因此必须手动加载它们 https://community.oracle.com/thread/2480398。 我遍历 xlApp.AddIns 并发现 "essexcln.xll" 已正确安装,所以我不知道现在该做什么。我还发现可以在 运行 时间内添加加载项,但这只会导致异常,这是来源: http://www.network54.com/Forum/58296/thread/957392331/Visual+Basic-Excel+Api+call+to+Essbase

发现 excel 没有加载连接到 de Essbase 服务器所需的所有 dll 和 xll。为了使其工作,有必要将 excel 作为进程启动并获取实例并将其关联到互操作 class。我在这里找到了解决方案:
Excel interop loading XLLs and DLLs
用户几乎遇到了同样的问题,但使用的是 Bloomberg。我只想在 SearchExcelInterop 方法中添加它需要 Thread.Sleep() 来等待 Excel 正确加载,在我的例子中它抛出了 WhosebugException。