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

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

问题描述

我目前正在写关于动态类型,我给出了一个Excel interop的例子。我以前几乎没有做任何Office互操作,它显示。 C#4的 MSDN Office Interop教程 _Worksheet 界面,但还有一个 Worksheet 界面。我不知道有什么区别。



在我荒谬的简单演示应用程序(如下所示)或者工作正常 - 但如果最佳实践指示一个或另一个,我

 使用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();

//可以在这里使用Excel._Worksheet。哪个更好?
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的新功能 - 但我不想做任何真正的,真的很蠢。



(可能有东西使用单独的开始/结束单元格而不是只是A1:T1是故意的 - 更容易看到它是真正的一个范围的20个单元格。任何事情)



所以,我应该使用 _Worksheet code>,为什么?

解决方案

如果我记得正确,我的记忆有点模糊,它已经很长时间了我把Excel PIA分开 - 这是这样的。



事件本质上是一个对象调用时发生的事情。在.NET中,事件是委托,简单和简单。但在COM中,组织一大堆事件回调到接口是非常常见的。因此,在给定对象上有两个接口 - 传入接口,希望其他人调用的方法,以及传出接口,当事件发生时,您希望对其他人调用的方法。 p>

在非托管元数据(类型库)中,对于可创建对象,有三个定义:传入接口,传出接口和coclass,我是一个可创建的对象,实现这个传入接口和这个传出接口。



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



_Workbook接口是工作簿coclass上的输入接口。工作簿界面是代表coclass本身的接口,因此继承自_Workbook。



很长的故事,我会使用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天全站免登陆