用c#触发Essbase宏

本文关键字:Essbase 触发 | 更新日期: 2023-09-27 18:16:13

我正在尝试自动化一些Excel报告。目前我需要从Essbase服务器检索一些数据,为了实现这一目标,我创建了一个宏来检索和设置Excel工作表中的数据,我的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应该返回状态码(0表示成功,其他表示失败)。所以这里的技巧,每当我在Excel中运行这个宏,它运行完美,然而,当我使用xlApp.Run("Essbase_Update_Pulls");从c#调用它时,它返回一个状态码-3。做了一些研究后,我发现每当用代码创建Excel应用程序时,它都没有加载加载项,所以它们必须手动加载https://community.oracle.com/thread/2480398。我对xlApp.AddIns进行了迭代,发现"essexcln"。xll"是正确安装的,所以我不知道现在该怎么办。另外,我发现插件可以在运行时添加,但这只是导致一个异常,这是源代码:http://www.network54.com/Forum/58296/thread/957392331/Visual +基本的excel + Api +电话+ + Essbase

用c#触发Essbase宏

发现excel没有加载连接到Essbase服务器所需的所有dll和xll。为了使其工作,有必要启动excel作为一个过程,并获取实例,并将其与互操作类联系起来。我在这里找到了解决方案:
Excel互操作加载xsl和dll。
这个用户也遇到了同样的问题,只不过是彭博社。我只是在SearchExcelInterop方法中添加,它需要Thread.Sleep()等待Excel正确加载,在我的情况下,它抛出了StackOverflowException。