代码之家  ›  专栏  ›  技术社区  ›  Saravanan Sachi TheSharpieOne

在VB中调用Excel函数的任何方法。NET作为Microsoft。办公室互操作。Excel在服务器中抛出未注册的类(REGB\u E\u CLASSNOTREG)?

  •  0
  • Saravanan Sachi TheSharpieOne  · 技术社区  · 7 年前

    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)
    

    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))

    有没有办法在中使用Excel函数。NET或是否有任何库可以使用这些Excel函数?

    1 回复  |  直到 7 年前
        1
  •  0
  •   Saravanan Sachi TheSharpieOne    7 年前

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

    Install-Package MathNet.Numerics -Version 3.20.0

    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