我正在用下面的代码开发一个excel插件。
我创建了一个类库并添加了以下代码

using System;
using System.Collections.Generic;
 using System.Linq;
 using System.Text;
  using System.Runtime.InteropServices;
  using Microsoft.Win32;

  namespace MyCustomAutomation
  {

// Replace the Guid below with your own guid that

// you generate using Create GUID from the Tools menu

[Guid("5268ABE2-9B09-439d-BE97-2EA60E103EF6")]
[ClassInterface(ClassInterfaceType.AutoDual)]
[ComVisible(true)]
public class MyFunctions
{
    public MyFunctions()
    {

    }

    public double MultiplyNTimes(double number1, double number2, double timesToMultiply)
    {
        double result = number1;
        for (double i = 0; i < timesToMultiply; i++)
        {
            result = result * number2;
        }

        return result;


    }


    [ComRegisterFunctionAttribute]
    public static void RegisterFunction(Type type)
    {

        Registry.ClassesRoot.CreateSubKey(

          GetSubKeyName(type, "Programmable"));

        RegistryKey key = Registry.ClassesRoot.OpenSubKey(

          GetSubKeyName(type, "InprocServer32"), true);

        key.SetValue("",

          System.Environment.SystemDirectory + @"\mscoree.dll",

          RegistryValueKind.String);
    }

    [ComUnregisterFunctionAttribute]
    public static void UnregisterFunction(Type type)
    {

        Registry.ClassesRoot.DeleteSubKey(

          GetSubKeyName(type, "Programmable"), false);
    }

    private static string GetSubKeyName(Type type,

      string subKeyName)
    {

        System.Text.StringBuilder s =

          new System.Text.StringBuilder();

        s.Append(@"CLSID\{");

        s.Append(type.GUID.ToString().ToUpper());

        s.Append(@"}\");

        s.Append(subKeyName);

        return s.ToString();

    }
   }
  }


我安装了它,并且该函数在excel中工作正常。我能够使用返回值的MultiplyNTimes函数,但是我的问题是,当我从单元格调用该函数时,结果显示在同一单元格本身中,而我想要的结果可以在除被叫单元以外的任何单元中播放。由于无法到达任何方向,我一无所知。
请帮忙

最佳答案

要将结果保存在另一个单元格中,只需将结果作为数组返回即可。因此,以MultiplyNTimes函数的另一个版本为例,可以这样写:

 public double[] MultiplyNTimesNextCell(double number1, double number2, double timesToMultiply)
{
       // result[0] is the value returned on the first cell
       // result[1] is the value returned on the next cell
       double[] result = new double[] {0, number1};
       for (double i = 0; i < timesToMultiply; i++)
       {
            // hardcoded to result[1] where to return the result
            result[1] = result[1] * number2;
       }

        return result;
}


然后在Excel中使用此函数时,必须使用数组公式语法,这意味着如果在A1中输入该函数,然后选择单元格A1和B1,然后按F2,然后按Ctrl + Shift + Enter

另请注意,我只是在要输入公式的单元格中使用0来返回。如果希望将此值更改为不同类型的另一个值,则可以使用对象数组作为结果数据类型。

因此,例如,您可以通过以下方式重写它:

public object[] MultiplyNTimesObj(double number1, double number2, double timesToMultiply)
{
     object[] result = new object[] { "MultiplyNTimesObj=", number1 };
     for (double i = 0; i < timesToMultiply; i++)
     {
         result[1] = (double)result[1] * number2;
     }

     return result;
}

关于c# - 使用Excel加载项,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/14751601/

10-14 11:45
查看更多