有什么办法在VB.NET中调用Excel函数作为Microsoft.Office.Interop.Excel在服务器中抛出未注册的类(REGB_E_CLASSNOTREG)?

时间:2017-09-21 05:30:35

标签: vb.net com-interop excel-interop

应用程序需要使用一些Excel函数(NormSDist,NormSInv)来计算一些结果。 Excel和.NET相当于这些函数的结果之间存在细微差别。因为它是一个银行应用程序,用户想要完全匹配。因此,通过引用Microsoft.Office.Interop.Excel并调用Excel函数NormSDist,NormSInv返回确切的结果。

引用Microsoft.Office.Interop.Excel

Dim appExcel As Microsoft.Office.Interop.Excel.Application = New Microsoft.Office.Interop.Excel.Application()
Dim wsf As Microsoft.Office.Interop.Excel.WorksheetFunction = appExcel.WorksheetFunction()

decOne = wsf.NormSInv(someValue) + wsf.NormSInv(someValue)
decTwo = 12.5 * wsf.NormSDist(decNorm)

.NET等效函数

Public Shared Function NORMSDIST(z As Double) As Double
    Dim sign As Double = 1
    If z < 0 Then
        sign = -1
    End If
    Return 0.5 * (1.0 + sign * erf(Math.Abs(z) / Math.Sqrt(2)))
End Function

Private Shared Function erf(x As Double) As Double
    Dim a1 As Double = 0.254829592
    Dim a2 As Double = -0.284496736
    Dim a3 As Double = 1.421413741
    Dim a4 As Double = -1.453152027
    Dim a5 As Double = 1.061405429
    Dim p As Double = 0.3275911
    x = Math.Abs(x)
    Dim t As Double = 1 / (1 + p * x)
    Return 1 - ((((((a5 * t + a4) * t) + a3) * t + a2) * t) + a1) * t * Math.Exp(-1 * x * x)
End Function

' This function is a replacement for the Microsoft Excel Worksheet function NORMSINV.
' It uses the algorithm of Peter J. Acklam to compute the inverse normal cumulative
' distribution. Refer to http://home.online.no/~pjacklam/notes/invnorm/index.html for
' a description of the algorithm.
' Adapted to VB by Christian d'Heureuse, http://www.source-code.biz.
Public Shared Function NormSInv(ByVal p As Double) As Double
    Const a1 = -39.6968302866538, a2 = 220.946098424521, a3 = -275.928510446969
    Const a4 = 138.357751867269, a5 = -30.6647980661472, a6 = 2.50662827745924
    Const b1 = -54.4760987982241, b2 = 161.585836858041, b3 = -155.698979859887
    Const b4 = 66.8013118877197, b5 = -13.2806815528857, c1 = -0.00778489400243029
    Const c2 = -0.322396458041136, c3 = -2.40075827716184, c4 = -2.54973253934373
    Const c5 = 4.37466414146497, c6 = 2.93816398269878, d1 = 0.00778469570904146
    Const d2 = 0.32246712907004, d3 = 2.445134137143, d4 = 3.75440866190742
    Const p_low = 0.02425, p_high = 1 - p_low
    Dim q As Double, r As Double
    Dim strErrMsg As String = ""

    If p < 0 Or p > 1 Then
        strErrMsg = "NormSInv: Argument out of range."
    ElseIf p < p_low Then
        q = Math.Sqrt(-2 * Math.Log(p))
        NormSInv = (((((c1 * q + c2) * q + c3) * q + c4) * q + c5) * q + c6) /
     ((((d1 * q + d2) * q + d3) * q + d4) * q + 1)
    ElseIf p <= p_high Then
        q = p - 0.5 : r = q * q
        NormSInv = (((((a1 * r + a2) * r + a3) * r + a4) * r + a5) * r + a6) * q /
     (((((b1 * r + b2) * r + b3) * r + b4) * r + b5) * r + 1)
    Else
        q = Math.Sqrt(-2 * Math.Log(1 - p))
        NormSInv = -(((((c1 * q + c2) * q + c3) * q + c4) * q + c5) * q + c6) /
     ((((d1 * q + d2) * q + d3) * q + d4) * q + 1)
    End If
End Function

但由于Excel不在服务器中,因此会抛出

Retrieving the COM class factory for component with CLSID{} failed due to the following error: 80040154 Class not registered (Exception from HRESULT: 0x80040154 (REGDB_E_CLASSNOTREG))

有没有办法在.NET中使用Excel函数,还是有任何库可以使用这些Excel函数?

注意:此应用程序只需要Excel功能,并且不与任何Excel文件交互

1 个答案:

答案 0 :(得分:0)

MathNet.Numerics库(https://numerics.mathdotnet.com/)按预期工作。

Install-Package MathNet.Numerics -Version 3.20.0

在.vb文件中,

Imports MathNet.Numerics

由于ExcelFunctions是一个静态类,因此直接将方法称为

decOne = ExcelFunctions.NormSInv(someValue) + ExcelFunctions.NormSInv(someValue)
decTwo = 12.5 * ExcelFunctions.NormSDist(decNorm)    

以下链接包含可用方法列表 https://numerics.mathdotnet.com/api/MathNet.Numerics/ExcelFunctions.htm