简体   繁体   English

com interop:如何从Excel或Access中使用C#

[英]com interop: how to consume c# from Excel or Access

I see plenty on the web about VSTO and automating excel from c#, but very little to get started on wiring up ac# assembly to make it visible from Excel. 我在网上看到了很多有关VSTO和从c#自动化excel的信息,但很少有关于连接ac#程序集以使其在Excel中可见的入门。

Can someone point me to some very basic info explaining how to create a COM interface, GUIDS, ComVisible etc? 有人可以指出一些非常基本的信息来解释如何创建COM接口,GUIDS,ComVisible等吗?

There are many books out there that might get you going. 那里有很多书可以帮助您前进。 "Pro C# 2008" from Wrox has a great chapter on this. Wrox的“ Pro C#2008”在这方面开了很长的篇章。

Also, here is a MSDN blog article about COM Visible to Excel. 另外,这是有关COM对Excel可见的MSDN博客文章。

Basically all you need to do is 基本上,您需要做的就是

  1. Make your assembly COM visible using the respective attribute in the project property Assembly version information dialog 使用项目属性“装配版本信息”对话框中的相应属性,使装配COM可见
  2. For every public class, add the following block (see [1]) of code above the class definition 对于每个公共类,在类定义上方添加以下代码块(请参见[1])
  3. Register DLL using the regasm.exe tool found in the .NET 2 installation folder 使用.NET 2安装文件夹中的regasm.exe工具注册DLL

Also, make sure to add a descriptive name to both application name and description in the Assembly version information dialog (they are later used to pick the COM classes). 另外,请确保在“程序集版本信息”对话框中为应用程序名称和描述添加描述性名称(它们以后将用于选择COM类)。

[1] Block of code to add before class definition: [1]在类定义之前添加的代码块:

[ClassInterface(ClassInterfaceType.AutoDual)]
[ProgId("SomeNameHere")]
[ComVisible(true)]
[Guid("xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxx")]

After that you should be able to use the class like any other normal COM class. 之后,您应该能够像使用其他任何普通COM类一样使用该类。

EDIT 编辑
It should be noted that I do not have experience with Excel and C# COM classes - I'm using C# together with Microsoft Navision, which works great the way I described above. 应该注意的是,我没有使用Excel和C#COM类的经验-我将C#与Microsoft Navision一起使用,这与我上面描述的方式非常有效。

Rather than go down the COM route, it is considerably easier (and less of an install issue) to use something like ExcelDNA . 与其沿用COM路由,不如使用诸如ExcelDNA之类的东西要容易得多(并且不会造成安装问题)。 It allow you to write XLLs in .Net that don't require registering. 它允许您在.Net中编写不需要注册的XLL。 It is also open-source and very well community supported. 它也是开源的,并且得到了社区的很好支持。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM