[英]Can we pass a custom object from Excel to C# ExcelDNA?
I have an object: 我有一个对象:
public class Shampoo
{
public string Name {get;set;}
public string Id {get;set;}
public string PriceRegion {get;set;}
}
Can I pass this object to a method in C# via Excel DNA: 我可以通过Excel DNA将此对象传递给C#中的方法:
C# method is: C#方法是:
public static string PassShampoo(Shampoo shampoo)
{
//Some Code...
}
Is this possible from VBA to C# via Excel DNA? 这可能是通过Excel DNA从VBA到C#吗?
Your .NET library can export and register the Shampoo
class as a COM visible type, which you can then instantiate from VBA as Dim sh = New Shampoo()
. 您的.NET库可以将Shampoo
类导出并注册为COM可见类型,然后您可以从VBA实例化为Dim sh = New Shampoo()
。 Similarly other types could be define, like 类似地,可以定义其他类型
public class Shopper()
{
public string ReadLabel(Shampoo shampoo)
{
return shampoo.Name;
}
}
and then a New Shopper()
could be passed the shampoo
: 然后New Shopper()
可以通过shampoo
:
Dim anne As Shopper = New Shopper()
Dim dove As Shampoo = New Shampoo()
Dim label As String
label = anne.ReadLabel(dove)
The method using Shampoo
( Shopper.ReadLabel
above) would not be static
and would not be available to Excel as a UDF or anything - just as a method on a .NET object onvoked via COM interop from VBA. 使用Shampoo
(上面的Shopper.ReadLabel
)的方法不是static
,并且不能作为UDF或任何东西用于Excel - 就像通过VBA的COM互操作上的.NET对象上的方法一样。
So far - no Excel-DNA involved. 到目前为止 - 没有涉及Excel-DNA 。 You could do all of this with a standard .NET assembly that is compiled with the right flags and attributes and registered for COM interop on your machine. 您可以使用标准.NET程序集完成所有这些操作,该程序集使用正确的标志和属性进行编译,并在计算机上注册COM interop。
However, Excel-DNA also allows your add-in to be a COM Server. 但是,Excel-DNA还允许您的加载项成为COM服务器。 This means that the .xll can host the COM classes you've defined in your library, your add-in can do the COM registration (instead of an installer) without requiring Administrator access, and your COM Objects will live in the same AppDomain as the rest of your add-in. 这意味着.xll可以托管您在库中定义的COM类,您的加载项可以在不需要管理员访问权限的情况下进行COM注册(而不是安装程序),并且您的COM对象将存在于同一个AppDomain中。你的加载项的其余部分。 So Excel-DNA helps a bit in gluing things up, but the actual interaction between the VBA code and your .NET assembly is the standard .NET-to-COM interop, which works very well once you've climbed the learning curve a bit. 所以Excel-DNA有点混淆了东西,但VBA代码和.NET程序集之间的实际交互是标准的.NET-to-COM互操作,一旦你爬上学习曲线,它就能很好地工作。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.