Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/287.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 使用Excel加载项_C#_Vsto_Excel 2007_Add In_User Defined Functions - Fatal编程技术网

C# 使用Excel加载项

C# 使用Excel加载项,c#,vsto,excel-2007,add-in,user-defined-functions,C#,Vsto,Excel 2007,Add In,User Defined Functions,我正在用下面的代码开发一个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

我正在用下面的代码开发一个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();

    }
   }
  }
使用系统;
使用System.Collections.Generic;
使用System.Linq;
使用系统文本;
使用System.Runtime.InteropServices;
使用Microsoft.Win32;
命名空间MyCustomAutomation
{
//将下面的Guid替换为您自己的
//使用“工具”菜单中的“创建GUID”生成
[Guid(“5268ABE2-9B09-439d-BE97-2EA60E103EF6”)]
[ClassInterface(ClassInterfaceType.AutoDual)]
[ComVisible(true)]
公共类函数
{
公共职能()
{
}
公共双倍频(双倍数字1、双倍数字2、双倍时间到倍频)
{
双结果=数字1;
for(双i=0;i
我安装了它,该函数在excel中运行良好。我能够使用返回值的MultiplyNTimes函数。然而,我的问题是,当我从单元格调用该函数时,结果显示在同一单元格中,而我希望结果在被调用单元格以外的任何单元格中播放。我完全不知所措,因为我无法访问任何方向。
请帮助

将结果放在另一个单元格中,只需将结果作为数组返回即可。因此,作为multilyntimes函数的另一个版本的示例,它可以写成:

 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;
}
public double[]multilyntimesnextcell(双倍数字1、双倍数字2、双倍时间到多倍)
{
//结果[0]是第一个单元格上返回的值
//结果[1]是下一个单元格上返回的值
double[]result=新的double[]{0,number1};
for(双i=0;i
然后在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;
}
public object[]multilyntimesobj(双倍数字1,双倍数字2,双倍时间到多倍)
{
object[]result=新对象[]{“MultiplyNTimesObj=”,number1};
for(双i=0;i
那么,您希望将结果放在哪个单元格中?假设公式是用A1编写的,我希望答案是B1或C1。对于Excel用户来说,将公式设置在其他单元格中是完全违反直觉的。也许你应该重新考虑你的设计。