用C#testingEssbasemacros

我正在尝试自动化一些Excel报告。 目前我需要从Essbase服务器中检索一些数据,为了实现这个function,我创build了一个macros来检索和设置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 

variablesx应该返回状态码(0是其他任何失败的成功)。 所以这里有个诀窍,每当我在Excel中运行这个macros,它就会完美运行,但是当我使用xlApp.Run("Essbase_Update_Pulls");从C#调用它时xlApp.Run("Essbase_Update_Pulls"); 它返回一个状态码-3。 做一些研究,我发现每当一个Excel应用程序在代码中创build时,它没有加载插件,所以他们必须手动加载https://community.oracle.com/thread/2480398 。 我迭代了xlApp.AddIns ,发现“essexcln.xll”被正确安装,所以我不知道现在该做什么。 另外,我发现可以在运行时添加插件,但这只是一个例外,这里是来源: http : //www.network54.com/Forum/58296/thread/957392331/Visual+Basic-Excel+Api +电话+到+的Essbase

发现excel没有加载连接到de Essbase服务器所需的所有dll和xll。 为了使它工作,有必要启动excel作为一个过程,并获得实例,并将其与interop类相关联。 我在这里find了解决scheme:
Excel互操作加载XLL和DLL 。
用户几乎有同样的问题,但与彭博。 我只是在SearchExcelInterop方法中添加它需要一个Thread.Sleep()等待Excel加载正确,在我的情况下,它抛出一个StackOverflowException。