Excel 互操作:_Worksheet 还是 Worksheet? [英] Excel interop: _Worksheet or Worksheet?

查看:29
本文介绍了Excel 互操作:_Worksheet 还是 Worksheet?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在撰写有关动态类型的文章,并给出了 Excel 互操作的示例.我以前几乎没有做过任何 Office 互操作,它显示了.C# 4 的 MSDN Office Interop 教程 使用_Worksheet 接口,但也有 Worksheet 接口.我不知道有什么区别.

I'm currently writing about dynamic typing, and I'm giving an example of Excel interop. I've hardly done any Office interop before, and it shows. The MSDN Office Interop tutorial for C# 4 uses the _Worksheet interface, but there's also a Worksheet interface. I've no idea what the difference is.

在我极其简单的演示应用程序(如下所示)中,两者都可以正常工作 - 但如果最佳实践要求其中之一,我宁愿适当地使用它.

In my absurdly simple demo app (shown below) either works fine - but if best practice dictates one or the other, I'd rather use it appropriately.

using System;
using System.Linq;
using Excel = Microsoft.Office.Interop.Excel;

class DynamicExcel
{
    static void Main()
    {
        var app = new Excel.Application { Visible = true };
        app.Workbooks.Add();

        // Can use Excel._Worksheet instead here. Which is better?
        Excel.Worksheet workSheet = app.ActiveSheet;

        Excel.Range start = workSheet.Cells[1, 1];
        Excel.Range end = workSheet.Cells[1, 20];
        workSheet.get_Range(start, end).Value2 = Enumerable.Range(1, 20)
                                                           .ToArray();
    }
}

我试图避免对 COM 或 Office 互操作性进行全面深入研究,而只是强调 C# 4 的新功能 - 但我不想做任何非常非常愚蠢的事情.

I'm trying to avoid doing a full deep-dive into COM or Office interoperability, just highlighting the new features of C# 4 - but I don't want to do anything really, really dumb.

(上面的代码中可能还有一些非常非常愚蠢的东西,在这种情况下请告诉我.使用单独的开始/结束单元格而不是A1:T1"是故意的 - 更容易看出它是真正的范围是 20 个单元格.其他任何事情都可能是偶然的.)

(There may be something really, really dumb in the code above as well, in which case please let me know. Using separate start/end cells instead of just "A1:T1" is deliberate - it's easier to see that it's genuinely a range of 20 cells. Anything else is probably accidental.)

那么,我应该使用 _Worksheet 还是 Worksheet,为什么?

So, should I use _Worksheet or Worksheet, and why?

推荐答案

如果我没记错的话——我的记忆有点模糊,我已经很久没有拆开 Excel PIA 了——就像这个.

If I recall correctly -- and my memory on this is a bit fuzzy, it has been a long time since I took the Excel PIA apart -- it's like this.

事件本质上是对象在发生某事时调用的方法.在 .NET 中,事件是委托,简单明了.但是在 COM 中,将一大堆事件回调组织成接口是很常见的.因此,您在给定对象上有两个接口——传入"接口,您希望其他人调用您的方法,以及传出"接口,您希望在事件发生时调用其他人的方法.

An event is essentially a method that an object calls when something happens. In .NET, events are delegates, plain and simple. But in COM, it is very common to organize a whole bunch of event callbacks into interfaces. You therefore have two interfaces on a given object -- the "incoming" interface, the methods you expect other people to call on you, and the "outgoing" interface, the methods you expect to call on other people when events happen.

在非托管元数据(类型库)中,对于可创建对象,定义了三件事:传入接口、传出接口和 coclass,它表示我是实现此传入的可创建对象接口和这个传出接口".

In the unmanaged metadata -- the type library -- for a creatable object there are definitions for three things: the incoming interface, the outgoing interface, and the coclass, which says "I'm a creatable object that implements this incoming interface and this outgoing interface".

现在,当类型库自动转换为元数据时,遗憾的是,这些关系得以保留.拥有一个手动生成的 PIA 会更好,它可以使类和接口更符合我们在托管世界中的期望,但遗憾的是,这并没有发生.因此,Office PIA 充满了这些看似奇怪的重复,每个可创建的对象似乎都有两个与之关联的接口,上面有相同的东西.其中一个接口表示到 coclass 的接口,其中一个表示到该 coclass 的传入接口.

Now when the type library is automatically translated into metadata, those relationships are, sadly, preserved. It would have been nicer to have a hand-generated PIA that made the classes and interfaces conform more to what we'd expect in the managed world, but sadly, that didn't happen. Therefore the Office PIA is full of these seemingly odd duplications, where every creatable object seems to have two interfaces associated with it, with the same stuff on them. One of the interfaces represents the interface to the coclass, and one of them represents the incoming interface to that coclass.

_Workbook 接口是工作簿 coclass 上的传入接口.Workbook 接口是代表 coclass 本身的接口,因此继承自 _Workbook.

The _Workbook interface is the incoming interface on the workbook coclass. The Workbook interface is the interface which represents the coclass itself, and therefore inherits from _Workbook.

长话短说,如果方便的话,我会使用 Workbook;_Workbook 是一个实现细节.

Long story short, I would use Workbook if you can do so conveniently; _Workbook is a bit of an implementation detail.

这篇关于Excel 互操作:_Worksheet 还是 Worksheet?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆