我正在尝试自动化一些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+Basic-Excel+Api+call+to+Essbase
答案 0 :(得分:1)
发现excel没有加载连接到de Essbase服务器所需的所有dll和xll。为了使其工作,有必要启动excel作为进程并获取实例并将其与interop类相关联。我在这里找到了解决方案:
Excel interop loading XLLs and DLLs。用户几乎有同样的问题,但彭博社。我只想在SearchExcelInterop
方法中添加它需要Thread.Sleep()
等待Excel正确加载,在我的情况下它会抛出StackOverflowException。