博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
[转].NET平台下的Excel编程|C#操作Excel|Application和ApplicationClass的联系和区别
阅读量:6690 次
发布时间:2019-06-25

本文共 6507 字,大约阅读时间需要 21 分钟。

 出处:http://hi.baidu.com/st_heping/blog/item/d589e7225acbcd589822ed12.html
 

Understanding Office Primary Interop Assembly Classes and Interfaces 

Office 2003
 
0 out of 6 rated this helpful 
 
Understanding Office Primary Interop Assembly Classes and Interfaces

The Office Primary Interop Assemblies (PIAs) expose many classes and interfaces that were previously hidden. Most of these classes and assemblies are displayed in the Object Browser in Visual Studio; however, some do not appear. Although these classes and interfaces can be confusing at first glance, it helps to understand the relationships between them, and how they are used.

This topic outlines some key points to keep in mind while working with PIA classes and interfaces.

Application Classes and Interfaces

Although the Object Browser displays Application interfaces for the Microsoft Word and Microsoft Excel PIAs, it does not directly expose Application classes for Word and Excel. However, when you use the Application interface to instantiate an Application object, the common language runtime internally uses the Application class.

For example, the following code uses the Excel Microsoft.Office.Interop.Excel.Application interface. At run time, it uses the Application class to instantiate an ExcelApplication object and open a worksheet.

 
 
Private Sub btnRunExcel_Click(ByVal sender As System.Object, _  ByVal e As System.EventArgs) Handles btnRunExcel.Click    Dim xl As Microsoft.Office.Interop.Excel.Application    xl = New Microsoft.Office.Interop.Excel.Application    Dim wb As Microsoft.Office.Interop.Excel.Workbook    wb = xl.Workbooks.Add()    Dim ws As Microsoft.Office.Interop.Excel.Worksheet    ws = wb.ActiveSheet    xl.Visible = TrueEnd Sub

classidClass Classes

It is possible to use a classidClass class, such as the ApplicationClass class in Microsoft Word and Microsoft Excel, or the WorkbookClass or WorksheetClass classes in Microsoft Excel, to instantiate an object. However, this practice should be avoided.

Using these classes has the potential to cause ambiguities if some members share the same name. For example, Microsoft Word exposes both anMicrosoft.Office.Interop.Word._Application.Quit(System.Object,System.Object,System.Object) method and anMicrosoft.Office.Interop.Word.ApplicationEvents4_Event.Quit event. These ambiguities can generate a compiler error.

Instead, use the exposed interface for a class—such as the ApplicationMicrosoft.Office.Interop.Excel.Workbook, or Microsoft.Office.Interop.Excel.Worksheet interface—to instantiate an object of that class. The following Visual Basic example uses the Microsoft.Office.Interop.Word.Application interface with the Quit method in Microsoft Word.

 
 
Private wd As Microsoft.Office.Interop.Word.ApplicationPrivate Sub btnRunWord_Click(ByVal sender As System.Object, _  ByVal e As System.EventArgs) Handles btnRunWord.Click    wd = New Microsoft.Office.Interop.Word.Application    wd.Visible = TrueEnd SubPrivate Sub btnQuitWord_Click(ByVal sender As System.Object, _  ByVal e As System.EventArgs) Handles btnQuitWord.Click    wd.Quit()    wd = NothingEnd Sub

_classid Interfaces

To avoid the potential for ambiguities, members of classes in which some members share the same name are displayed in the Object Browser as members of the corresponding _classid interface. For example, Application class members are displayed as members of the _Application interface. Although they are not displayed as members of their class interface in the Object Browser, internally they are associated with the class when you use them in code.

The following table lists the classes that have corresponding _classid interfaces.

 

Class _Class Interface PIA

Application

_Application

Word

Document

_Document

Word

Font

_Font

Word

Global

_Global

Word

LetterContent

_LetterContent

Word

OLEControl

_OLEControl

Word

ParagraphFormat

_ParagraphFormat

Word

Application

_Application

Excel

Chart

_Chart

Excel

Global

_Global

Excel

OLEObject

_OLEObject

Excel

QueryTable

_QueryTable

Excel

Workbook

_Workbook

Excel

Worksheet

_Worksheet

Excel

Event Interfaces

The classidEventsx interfaces displayed in the Object Browser, such as ApplicationEvents4 in the Word PIA, map directly to interfaces in the original Component Object Model (COM) type libraries and expose a set of corresponding methods. However, these methods are not directly usable.

Similarly, the PIAs implement ClassIdEventsx_SinkHelper classes, such as ApplicationEvents4_SinkHelper, which expose methods and delegates corresponding to each event. These methods and delegates are also for internal use only.

To work with events, use the ClassIdEventsx_Event interfaces, such as ApplicationEvents4_Event, which are also based on the classidEventsx interfaces. In addition, the PIAs implement classidEventsx_eventEventHandler delegates for each event. Use these delegates to create event handlers.

For more information, see .

Dual Interfaces

The Office PIAs implement a dual interface for many interfaces, with each interface having a corresponding Iinterfaceid interface. For example, the AppEvents interface in the Excel PIA has a corresponding IAppEvents interface. The Iinterfaceid interfaces are for internal use only and can be ignored.


Concepts

 

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

1. Interop含义
Interop是互操作的含义。
Microsoft.Office.Interop.Excel.dll 是 Excel COM的.NET封装。
.NET code通过这些被重新封装的COM来操作Excel。

2. 基础环境

在运行环境中必须安装Office,否则即使有Microsoft.Office.Interop.Excel.dll也无法用.NET code来操作Excel。
当然,有些工具可以在没有安装Office的情况下操作Excel,比如GemBox.ExcelLite。但是,这些工具一般都要收费。(这些工具的原理是:使用Office文档的开放规范来直接生成Office文档。)

3. Application和ApplicationClass的联系和区别

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

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

4. 代码实例

命名空间:Microsoft.Office.Interop.Excel

a. 创建Excel实例

Application excel = new Application();

b. 打开已有的一个workbook

Workbook workbook = excel.Workbooks.Open(FilePath, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing);

c. 打开已有的一个worksheet

Worksheet worksheet = (Worksheet)workbook.Worksheets[1]; (打开第一张worksheet。)

d. 选取一整列

Range column = ((Range)worksheet.Cells[1, 1]).EntireColumn;(选取A列;方法:先选取A1单元格,然后选取A1单元格所在的这一整列。)

e. 改变单元格的格式

column.NumberFormat = "@";(将d中选取的一整列的格式设置成General。)

转载地址:http://lmkoo.baihongyu.com/

你可能感兴趣的文章
NSHipster: NSRegularExpression 中文版
查看>>
Android 开发中不得不知道的 Tips 集合 (持续更新 ing)
查看>>
报警系统QuickAlarm之报警规则的设定与加载
查看>>
【CLI】使用 Curl 下载文件实时进度条显示
查看>>
Android 滤镜效果和颜色通道过滤
查看>>
Ruby开发者已可通过Fog管理Microsoft Azure服务
查看>>
Chrome和HTTPS:安全Web的征途
查看>>
软件专家的对话模式(第一部分)
查看>>
脚本填报表的条件查询
查看>>
从一个开发的角度看负载均衡和LVS
查看>>
Spring Boot(12)——使用MongoDB
查看>>
c++基础(上) 听课流水账
查看>>
Observable
查看>>
k8s使用deployment升级
查看>>
ionic3项目实战教程 - 第10讲 ionic3分类菜单设计(类似外卖)
查看>>
深度解析 | K8S API Server之入门须知
查看>>
LeanEngine 中使用 WebSocket
查看>>
浅入分析和Linux内核相关的文件夹/proc和/sys .
查看>>
Java 二分查找
查看>>
刚刚,阿里开源了一项重磅炸弹,终结程序员“中年危机”!
查看>>