简体   繁体   English

从单元格公式调用Excel 2007中用C#编写的方法

[英]Call methods written in C# in Excel 2007 from cell formulas

I am using Excel 2007. I have C# code written in a separate binary. 我正在使用Excel 2007.我有一个单独的二进制文件编写的C#代码。 The code uses static classes and static methods on the classes. 代码在类上使用静态类和静态方法。 I have a reference to the DLL in my VSTO Excel Worksheet project. 我在VSTO Excel工作表项目中引用了DLL。 What do I have to add or modify to get this to work? 我需要添加或修改哪些才能使其正常工作?

My C# code looks like this: 我的C#代码如下所示:

using System;
using System.Collections.Generic;
using Microsoft.Office.Interop.Excel;
using System.Runtime.InteropServices;

namespace FooStatistics
{
    [ComVisible(true)]
    public static class Statistics
    {
        public static int Count(Range range)
        {
            return range.Count;
        }

I want to be able to put a formula into an Excel cell that looks like this: 我希望能够将公式放入Excel单元格中,如下所示:

=FooStatistic.Statistic.Count(A1:A10)

Or whatever. 管他呢。

I've seen this but it appears to be for non-static classes in Excel 2003. I can't believe the integration is not better by now. 我已经看过这个,但它似乎是Excel 2003中的非静态类。我不相信整合现在还不是更好。

I've looked at a lot of StackOverflow questions on this. 我已经看了很多关于StackOverflow的问题。 They don't appear to provide native integration (many say, "Use X open source library") and, ominously, many are not accepted by the OP. 它们似乎没有提供本机集成(很多人说,“使用X开源库”),并且不祥的是OP很多都不接受它们。 I am not looking for, "Make it into a COM object and call it from VBA." 我不是在寻找,“将它变成一个COM对象并从VBA调用它。”

So I'm looking for: 所以我在寻找:

  • Excel 2007 Excel 2007
  • code in C# DLL C#DLL中的代码
  • call from Excel cell as UDF 从Excel单元格调用UDF
  • native integration 原生整合

So here's another StackOverflow link , in which two responders say: 所以这是另一个StackOverflow链接 ,其中两个响应者说:

  • As far as I know, you cannot directly create UDFs in VSTO. 据我所知,您无法在VSTO中直接创建UDF。
  • VSTO has no support for creating Excel UDFs. VSTO不支持创建Excel UDF。 Automation Add-Ins can be created in .Net, and seem to be the Microsoft approved way of doing it. 自动化加载项可以在.Net中创建,并且似乎是Microsoft批准的方式。

This is a question from June 2009. Is this true -- in 2009 you have to expose your .NET components as COM servers to get callable UDFs for Excel? 这是一个从2009年6月开始的问题。这是真的 - 在2009年你必须将你的.NET组件公开为COM服务器以获得可调用的Excel UDF吗?

If these are your four requirements -- (1) Excel 2007, (2) code in C# DLL, (3) call from Excel cell as UDF, (4) native integration -- then, yes, this can be done, and pretty easily. 如果这些是你的四个要求 - (1)Excel 2007,(2)C#DLL中的代码,(3)从Excel单元格调用UDF,(4)本机集成 - 那么,是的,这可以完成,并且漂亮容易。 One of the best tutorials on how to do this is Eric Carter's article Writing user defined functions for Excel in .NET . 有关如何执行此操作的最佳教程之一是Eric Carter的文章为.NET中的Excel编写用户定义的函数

If you additionally want your code be hosted via VSTO, then I am virtually certain that you are required to use a VBA wrapper in this case. 如果您还希望通过VSTO托管您的代码,那么我几乎可以肯定您需要在这种情况下使用VBA包装器。 See Paul Stubbs' article How to create Excel UDFs in VSTO managed code where he uses a VBA add-in to expose VBA UDFs, which in turn call his Managed UDFs written in VSTO. 请参阅Paul Stubbs的文章如何在VSTO托管代码创建Excel UDF,其中他使用VBA加载项来公开VBA UDF,后者又调用VSTO编写的托管UDF。

To be honest though, for Excel UDFs, I would simply avoid the use of VSTO. 说实话,对于Excel UDF,我只是避免使用VSTO。 VSTO is a great designer for managed COM add-ins, allowing you to easily add Ribbon controls and the like. VSTO是托管COM加载项的优秀设计器,允许您轻松添加功能区控件等。 But it is of no help for UDFs (and in fact, does not even support it). 但它对UDF没有帮助(事实上,甚至不支持它)。 So my advice is to create a managed automation add-in, as per Eric Carter's article , and drop the VSTO requirement. 所以我的建议是根据Eric Carter的文章创建一个托管自动化加载项,并放弃VSTO要求。

If you do this, you will have no problems, I promise. 如果你这样做,你就没有问题,我保证。 :-) :-)

Mike 麦克风

Hugh,

I understand your desire for a 'native' solution, rather than to "Use X open source library". 我理解您对“本机”解决方案的渴望,而不是“使用X开源库”。 But even VSTO does not seem very 'native' to Excel. 但即便是VSTO似乎也不是Excel的“原生”。

Your requirement is exactly what lead me to develop ExcelDna ( http://exceldna.codeplex.com ) after finding the Automation add-ins inadequate. 在找到自动化插件不足之后,您的要求正是让我开发ExcelDna( http://exceldna.codeplex.com )的原因。 The support for Automation Add-Ins has not improved in recent Excel versions, whereas the .xll add-in API (that ExcelDna uses) has seen updated support in recent versions, now supporting multi-threaded recalculation, and with async calls coming in Excel 2010. 自动化加载项的支持在最近的Excel版本中没有改进,而.xll加载项API(ExcelDna使用)在最近的版本中已经看到更新支持,现在支持多线程重新计算,并且在Excel中有异步调用2010。

Even though ExcelDna is an extra part to introduce into your solution, you'll be pleased at the result. 即使ExcelDna是引入您的解决方案的额外部分,您也会对结果感到满意。 Sadly there has been no clear direction from Microsoft on managed UDF add-ins, or any sign of support for this in VSTO, but in practice doing it with ExcelDna is easy, light-weight and it works very well. 遗憾的是,微软没有明确的方向来管理UDF加载项,或者在VSTO中对此有任何支持,但实际上使用ExcelDna这样做很容易,重量轻且效果很好。

Govert 霍弗特

@ hughdbrown: just follow the example in Erics article. @ hughdbrown:只需按照Erics文章中的示例进行操作即可。 If you do that it will work. 如果你这样做它会工作。 No you can't use static class. 不,你不能使用静态类。 your instaniating a .net class with a COM wrapper (registering it for com interop). 使用COM包装器实现一个.net类(将其注册到com interop)。

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

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