C# Excel UDF 范围写回收到错误,来自 HRESULT 的异常:0x800A03EC

本文关键字:HRESULT 来自 异常 0x800A03EC 错误 UDF Excel 范围 写回 | 更新日期: 2023-09-27 18:32:23

public string Name(string code)
    {
        MyExcelAppInstance.Volatile(true);
        MsExcel.Application oApp = new MsExcel.Application();
        oApp.Visible = true;
        oApp.UserControl = true;
        Object oBooks = oApp.Workbooks;
        System.Globalization.CultureInfo ci = new System.Globalization.CultureInfo("en-US");
        oBooks.GetType().InvokeMember("Add", System.Reflection.BindingFlags.InvokeMethod, null, oBooks, null, ci);
        oApp.Visible = true;
        oApp.UserControl = true;
        System.Globalization.CultureInfo oldCI = System.Threading.Thread.CurrentThread.CurrentCulture;
        System.Threading.Thread.CurrentThread.CurrentCulture = new System.Globalization.CultureInfo("en-US");
        oApp.Workbooks.Add();
        System.Threading.Thread.CurrentThread.CurrentCulture = oldCI;
        MsExcel.Range rng = MyExcelAppInstance.get_Range("A1:D4", Type.Missing);
        //Get a 2D Array of values from the range in one shot: 
        object[,] myArray = (object[,])rng.get_Value(Type.Missing);
        // Process 'myArray' however you want here. 
        // Note that the Array returned from Excel is base 1, not base 0. 
        // To be safe, use GetLowerBound() and GetUpperBound: 
        for (int row = myArray.GetLowerBound(0); row <= myArray.GetUpperBound(0); row++)
        {
            for (int column = myArray.GetLowerBound(1); column <= myArray.GetUpperBound(1); column++)
            {
                if (myArray[row, column] is double)
                {
                    myArray[row, column] = (double)myArray[row, column] * 2;
                }
            }
        }
        // Pass back the results in one shot: 
        rng.set_Value(Type.Missing, myArray);  // where gets an error
        return code + code;
    }

我收到一个错误,"来自 HRESULT 的异常:0x800A03EC"。 我尝试设置区域设置,但没有工作。没有set_value,它可以正常工作。 知道为什么这不起作用吗?

C# Excel UDF 范围写回收到错误,来自 HRESULT 的异常:0x800A03EC

我曾经遇到过类似的问题,结果发现错误是由无效参数引起的(我以无效格式传递范围字符串)。确保范围和所有数组连接在调用 set_Value 时有效。