VS2010的新特性:4.简化了对 Office API 对象的访问

索引属性改进了在 C# 编程中使用具有参数的 COM 属性的方式,下面我们来看看Microsoft Office Excel的编程实例

在VS2008中访问 Range 属性,必须使用get_Range方法

var excelApp = new Excel.Application();

Excel.Range targetRange = excelApp.get_Range("A1", Type.Missing);

在VS2010中我们,使用索引属性

var excelApp = new Excel.Application();

Excel.Range targetRange = excelApp.Range["A1"]

这里还使用到了前面文章提到的可选参数,忽略了Type.Missing

我们在来看一下在VS2008中对AutoFormat的调用

excelApp.get_Range("A1", "B4").AutoFormat(Excel.XlRangeAutoFormat.xlRangeAutoFormatTable3,

    Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing,

Type.Missing);

该方法具有七个可选参数,调用起来非常繁琐。

VS2010中通过命名话参数和可选参数,使得调用变得非常简单。

workSheet.Range["A1", "B3"].AutoFormat(Format:

    Excel.XlRangeAutoFormat.xlRangeAutoFormatClassic2);

原文地址:https://www.cnblogs.com/letnet/p/1783554.html