Excel interop: _Worksheet or Worksheet?

前端 未结 5 1864
刺人心
刺人心 2020-11-28 07:46

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

相关标签:
5条回答
  • 2020-11-28 08:21

    MSDN shows that the Worksheet interface simply inherits from the _Worksheet and DocEvents_Event interfaces. It would seem that one simply provides the events that a worksheet object might raise in additional to everything else. As far as I can see, Worksheet doesn't provide any other members of its own. So yeah, you might as well just go with using the Worksheet interface in all cases, since you don't lose anything by it, and potentially might need the events it exposes.

    0 讨论(0)
  • 2020-11-28 08:26

    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.

    0 讨论(0)
  • 2020-11-28 08:27

    If you look at the PIA assembly (Microsoft.Office.Interop.Excel) in Reflector, the Workbook interface has this definition ...

    public interface Workbook : _Workbook, WorkbookEvents_Event
    

    Workbook is _Workbook but adds events. Same for Worksheet (sorry, just noticed you were not talking about Workbooks) ...

    public interface Worksheet : _Worksheet, DocEvents_Event
    

    DocEvents_Event ...

    [ComVisible(false), TypeLibType((short) 0x10), ComEventInterface(typeof(DocEvents),
                         typeof(DocEvents_EventProvider))]
    public interface DocEvents_Event
    {
        // Events
        event DocEvents_ActivateEventHandler Activate;
        event DocEvents_BeforeDoubleClickEventHandler BeforeDoubleClick;
        event DocEvents_BeforeRightClickEventHandler BeforeRightClick;
        event DocEvents_CalculateEventHandler Calculate;
        event DocEvents_ChangeEventHandler Change;
        event DocEvents_DeactivateEventHandler Deactivate;
        event DocEvents_FollowHyperlinkEventHandler FollowHyperlink;
        event DocEvents_PivotTableUpdateEventHandler PivotTableUpdate;
        event DocEvents_SelectionChangeEventHandler SelectionChange;
    }
    

    I would say it's best bet to use Worksheet, but that's the difference.

    0 讨论(0)
  • 2020-11-28 08:33

    I have seen and written quite a bit of C# / Excel COM Interop code over the last few years and I've seen Worksheet used in almost every case. I have never seen anything definitive from Microsoft on the subject.

    0 讨论(0)
  • 2020-11-28 08:38

    Classes and Interfaces for Internal Use Only

    Avoid directly using any of the following classes and interfaces, which are used internally and are typically not used directly.

    Class/Interface : Examples

    classid Class : ApplicationClass (Word or Excel), WorksheetClass (Excel)

    classid Events x _SinkHelper : ApplicationEvents4_SinkHelper (Word), WorkbookEvents_SinkHelper (Excel)

    _classid : _Application (Word or Excel), _Worksheet (Excel)

    classid Events x : ApplicationEvents4 (Word), AppEvents (Excel)

    I classid Events x : IApplicationEvents4 (Word), IAppEvents (Excel)

    http://msdn.microsoft.com/en-gb/library/ms247299(office.11).aspx

    edit: (re: formatting of this answer) cannot correctly format an escaped underscore followed immediately by italic text. Shows correctly in preview but broken when posted

    edit2: works if you make the underscore itself italic which is conceptually horrible but looks the same I suppose

    0 讨论(0)
提交回复
热议问题