Excel的互操作:_Worksheet或工作表? [英] Excel interop: _Worksheet or Worksheet?

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

问题描述

我目前正在写关于动态类型,和我给Excel的互操作的一个例子。我几乎没有做过任何Office互操作,它显示了。对于C#4 MSDN办公室互操作教程使用 _Worksheet 接口,但也有一个工作表接口。我不知道有什么区别。

在我的荒谬简单演示应用程序(如下图所示),要么工作正常 - 但如果最佳实践规定一个或另一个,我宁愿适当地使用它。

 使用系统;
使用System.Linq的;
使用Excel =的Microsoft.Office.Interop.Excel;类DynamicExcel
{
    静态无效的主要()
    {
        VAR应用=新Excel.Application {可见=真};
        app.Workbooks.Add();        //而是可以在这里使用Excel._Worksheet。哪个更好?
        Excel.Worksheet工作表= app.ActiveSheet;        Excel.Range开始= workSheet.Cells [1,1];
        Excel.Range结束= workSheet.Cells [1,20]。
        workSheet.get_Range(起点,终点).Value2 = Enumerable.Range(1,20)
                                                           .ToArray();
    }
}

我试图避免做一个全面深入了解COM或Office的互操作性,只是突出了C#4的新功能 - 但我不想做任何事情真的,真的很愚蠢的。

(可能有一些真正的,在code以上以及非常愚蠢的,在这种情况下,请让我知道使用单独的开始/结束的细胞,而不是仅仅A1:T1是故意的 - 它更容易看到它是真正的为20的细胞,还有什么可能是偶然的。)

所以,我应该使用 _Worksheet 工作表,为什么?


解决方案

如果我没有记错 - 我对这个记忆有点模糊,它已经很长一段时间,因为我把Excel的PIA分开 - 就像这一点。

这是事件本质上是有事情发生时,一个对象调用的方法。在.NET中,事件是代表,简单明了。但在COM中,这是很常见的一大堆事件的回调整理成接口。因此,你有一个给定对象的两个接口 - 在进入界面,你还指望别人给你调用的方法,而外向的界面,您所期望的方法就当事件发生时其他人叫

在非托管元数据 - 类型库 - 一个可创建对象有三件事定义:入接口,出接口和组件类,它说:我实现这个呼入可创建对象接口和该出接口。

现在,当该类型库自动转换成元数据,这些关系,可悲的是,preserved。这本来是更好的有一个手工生成PIA,使得类和接口更符合我们会在管理世界期望的,但可悲的是,这种情况并未发生。因此,办公室PIA充满了这些看似奇怪的重复,其中的每个可创建对象似乎都与它相关联,与他们同样的东西两个接口。其中一个接口进行重新presents接口的组件类,其中一人重新presents入接口到组件类。

该_Workbook接口的工作簿组件类的入接口。该工作簿接口是重新presents组件类本身的接口,并因此从_Workbook继承。

长话短说,我会用工作簿,如果你可以这样做方便; _Workbook有点实现细节的。

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();
    }
}

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.

(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.)

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

解决方案

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.

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.

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".

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.

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.

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

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

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