Application和ApplicationClass的联系和区别
Application和ApplicationClass都继承自接口_Application。
Application为接口。ApplicationClass为类。
Application和ApplicationClass所拥有的属性、方法基本相同,但是也有一些小的差别。

比如:ApplicationClass有一个方法:OpenText;而Application却没有这个方法。通过这个方法,可以直接操作Excel去打开用分隔符分割的.txt文件。(注意,是.txt文件而不是.csv文件。)

namespace Microsoft.Office.Interop.Excel
{
[CoClass(typeof(ApplicationClass))]
[Guid("000208D5-0000-0000-C000-000000000046")]
public interface Application : _Application, AppEvents_Event
{
}
}
namespace Microsoft.Office.Interop.Excel
{
[ComSourceInterfaces("Microsoft.Office.Interop.Excel.AppEvents")]
[Guid("00024500-0000-0000-C000-000000000046")]
[TypeLibType()]
[ClassInterface()]
public class ApplicationClass : _Application, Application, AppEvents_Event {} }

Don't use ApplicationClass (unless you have to)

http://blogs.msdn.com/b/ptorr/archive/2004/02/05/67872.aspx

A comment on Mike Howard's blog exhibits a common problem that I see time and time again: developers are creating instances of Word.ApplicationClass or Excel.ApplicationClass in their projects.

Even though it's the wrong thing to do, I don't blame them for doing that. I blame IntelliSense.

First things first: What's the right way to do it? Well, just use Word.Application or Excel.Application (or any other type that follows the same Thinggy / ThinggyClass pattern).

exhibit  展览品;证据;展示会

IntelliSense  智能感知

First things first 先说重要的

But IntelliSense doesn't show me that as a valid option!

That's because IntelliSense is not as intelligent as its name might suggest. IntelliSense has a simple rule that says "after the user types the new keyword, show the user a list of new-able things." (Note I may be simplifying things here as I don't work on IntelliSense... but it explains how the system works. Feel free to correct me if you work on that team ;-) ). IntelliSense believes that the only things you can new are concrete, visible classes with one or more visible constructors. And it's almost right.

intelligent 智能的;聪明的;理解力强的

suggest  vt. 提议,建议;启发;使人想起;显示;暗示

In general, you cannot new an interface because interfaces have no implementation. But the default interfaces on COM CoClasses are a little different;

the CLR knows something that IntelliSense's parents forgot to teach it.

Crack open the Excel PIA using ILDASM and have a look at the Application interface. Among the gobbledy-gook, you will see the following:

gobbledygook  官样文章

custom instance void [mscorlib]System.Runtime.InteropServices.CoClassAttribute::.ctor(class [mscorlib]System.Type) = ( 01 00 2F 4D 69 63 72 6F 73 6F 66 74 2E 4F 66 66

69 63 65 2E 49 6E 74 65 72 6F 70 2E 45 78 63 65

6C 2E 41 70 70 6C 69 63 61 74 69 6F 6E 43 6C 61

73 73 00 00 ) // ../Microsoft.Office.Interop.Excel.ApplicationClass

This tells the CLR that when someone wants to create an instance of type Application, it should really go ahead and create an instance of ApplicationClass.

Another tip: In Microsoft Word, you may want to handle the Quit event to do something when the user closes down the application. But the Word.Application interface defines Quit as a method, not an event. What to do?

Well, you cast it to a Word.ApplicationEvents4_Event (you could find this out by using the Object Browser):

Word.Application app;

app = new Word.Application();

app.Visible = true;

Word.ApplicationEvents4_Event appEvents = app;

appEvents.Quit += new Word.ApplicationEvents4_QuitEventHandler(QuitHandler);

Both of these are due to the way COM works (again I am going to gloss over the details here, so feel free to correct me or point readers to a more accurate, in-depth blog if you have one).

COM doesn't really have the notion of a 'class' as a first-class (ha ha) object; everything to COM is an interface.

Now of course in order to get a real live implementation of an interface in your hot little hand, you need to be able to instantiate a concrete implementation of the interface through a function such as CreateObject or CoCreateInstance.

But these things simply use the ProgID or ClassID to look up the implementation provider in the registry, and from then on you're pretty much dealing with interfaces.

The COM coclass Application gets turned into the managed class ApplicationClass and a new interface Application is created to represent it and given the custom attribute you see above.

You should always bind to this interface. I believe this is for versioning reasons, but I can't really remember now (this topic discusses the versioning problem when exposing .NET objects to COM, but we're doing the reverse here).

The reason you have to have the funky cast to get the Quit event is that COM has no notion of overloads and, indeed, no native notion of events: interfaces can only have methods on them. Instead of having events, a class can expose one or more "source" interfaces which say to clients of the interface "if you implement this interface and then register it with me then I will call the methods on it in an event-like fashion."

Wherever possible, the CLR will "collapse" the events from the class' source interfaces into the main interface (eg, the Application.WorkbookOpen event in Excel actually comes from the AppEvents_Event interface), but when they have the same name as a method it can't do that so you need to explicitly cast to the original interface.

Like I said, I missed a lot of detail there (and maybe even told some fibs) so if someone else wants to give a more detailed (and correct!) explanation, please do so.

But the gist of it all is this: Just as Luke Skywalker puts away the computer targeting system and is told to "use the force" in Star Wars Episode IV, so too should you put away IntelliSense and never use ApplicationClass when programming against Office.

Excel中Application和ApplicationClass的区别的更多相关文章

  1. Odoo中Application与modules的区别

    转载请注明原文地址:https://www.cnblogs.com/cnodoo/p/9278681.html 一:Application(应用) application一般是针对大功能的模块,如提供 ...

  2. NET平台下的Excel编程|C#操作Excel|Application和ApplicationClass的联系和区别

    NET平台下的Excel编程|C#操作Excel|Application和ApplicationClass的联系和区别 1. Interop含义Interop是互操作的含义.Microsoft.Off ...

  3. 浅谈JS中的!=、== 、!==、===的用法和区别 JS中Null与Undefined的区别 读取XML文件 获取路径的方式 C#中Cookie,Session,Application的用法与区别? c#反射 抽象工厂

    浅谈JS中的!=.== .!==.===的用法和区别   var num = 1;     var str = '1';     var test = 1;     test == num  //tr ...

  4. 使用Sharepoint定时运行Excel中宏程序

    需求:因为Excel中数据量很大,其中包含了几个宏程序从其他数据源读取数据,运行一次宏需要比较长的时间,为了不影响使用,要求每天半夜运行一次Excel中的宏(无参数),Excel存放在共盘上. 解决方 ...

  5. NOPI读取模板导出(Excel中追加数据)

    在Controller里,我们定义一个FileResult的Action,返回值是一个文件形式被浏览器下载下来. [HttpGet] public FileResult ExportProductLi ...

  6. 如何将页面的<br/>在Excel中正确换行

    在页面的<br />导致导出Excel中是会以多行的方式显示,达不到页面在一个单元格中进行换行,为此我们有以下两种方式: 1.CSS样式方式 <br style='mso-data- ...

  7. c#.net Excel中的数据导入到SQL数据库中

    /// <summary>        /// 从Excel 导入学生        /// </summary>        /// <param name=&qu ...

  8. c#.net循环将DataGridView中的数据赋值到Excel中,并设置样式

    Microsoft.Office.Interop.Excel.Application excel =                new Microsoft.Office.Interop.Excel ...

  9. php中urlencode与rawurlencode的区别有那些呢

    urlencode 函数: 返回字符串,此字符串中除了 -_. 之外的所有非字母数字字符都将被替换成百分号(%)后跟两位十六进制数,空格则编码为加号(+).此编码与 WWW 表单 POST 数据的编码 ...

随机推荐

  1. 洛谷P2602 数字计数 [ZJOI2010] 数位dp

    正解:数位dp 解题报告: 传送门! 打算在寒假把学长发过题解的题目都做辣然后把不会的知识点都落实辣! ⁄(⁄ ⁄•⁄ω⁄•⁄ ⁄)⁄ 然后这道题,开始想到的时候其实想到的是大模拟,就有点像之前考试贪 ...

  2. xpath教程 3 - xpath的小结

    一.xpath提取内容 1.提取节点中最表层的文本 htmlobj.xpath("./text()") 在scrapy中用extract()[0]方法抽取文本.如: temp['t ...

  3. Python json pickle 模块 区别

    json 支持 str.list.dict.int.tuple 数据类型 pickle 支持Python所有里的所有数据类型 缺点: 只能在Python使用 总结: 1.JSON只能处理基本数据类型. ...

  4. PHP开发接口使用RSA进行加密解密方法

    网络安全问题很重要,尤其是保证数据安全,遇到很多在写接口的程序员直接都是明文数据传输,在我看来这是很不专业的.本人提倡经过接口的数据都要进行加密解密之后进行使用. 这篇文章主要介绍使用PHP开发接口, ...

  5. [wx]自然数学规律

    有趣的数学规律 椭圆 双曲线 抛物线都叫圆锥曲线 它们跟圆锥有着怎样的关系? 他们都是圆锥与平面在不同姿势下交配的产物. 参考 椭圆 抛物线 小结 e: 离线率 P: 任意一点 F: 焦点 准线: 一 ...

  6. POJ2983 Is the Information Reliable?

    http://acm.sdut.edu.cn:8080/vjudge/contest/view.action?cid=267#problem/B                        B -  ...

  7. MQTT协议学习研究 & Mosquitto简要教程(安装和使用)

    若初次接触MQTT协议,可先理解以下概念: [MQTT协议特点]——相比于RESTful架构的物联网系统,MQTT协议借助消息推送功能,可以更好地实现远程控制. [MQTT协议角色]——在RESTfu ...

  8. 024-linux中动态库libXXX.so

    1.动态库的概念.动态链接库与普通的程序相比而言,没有main函数,是一系列函数的实现.通过shared和fPIC编译参数生产so动态链接库文件.程序在调用库函数时,只需要连接上这个库即可. 2.动态 ...

  9. ModelSim使用$display查看变量值和输出信息

    打开ModelSim,新建工程->新建Verilog文件demo.v 输入文件内容 module demo(); reg[3:0] a,b; initial begin $display(&qu ...

  10. 解读jquery.filtertable.min

    jQuery.FilterTable是一款表格搜索过滤和单元格高亮插件. 该插件允许你对任意表格进行条件过滤,并且它会将搜索到的结果单元格高亮显示,非常实用和强大. 使用方法在页面中引入jquery和 ...