有什么方法可以在 VB.NET 中调用 Excel 函数,因为 Microsoft.Office.Interop.Excel 在服务器中抛出 Class 未注册 (REGB_E_CLASSNOTREG)?

Any way to call Excel functions in VB.NET as Microsoft.Office.Interop.Excel throws Class not registered (REGB_E_CLASSNOTREG) in Server?

应用程序需要使用一些Excel函数(NormSDist、NormSInv)来计算一些结果。这些函数的 Excel 和 .NET 等价物的结果略有不同。由于它是一个银行应用程序,因此用户需要完全匹配。因此,通过引用 Microsoft.Office.Interop.Excel 并调用 Excel 函数 NormSDist、NormSInv return 确切的结果。

参考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 文件交互

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

Install-Package MathNet.Numerics -Version 3.20.0

在.vb文件中,

Imports MathNet.Numerics

由于 ExcelFunctions 是静态的 class,直接调用方法

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

下面link有可用的方法列表 https://numerics.mathdotnet.com/api/MathNet.Numerics/ExcelFunctions.htm