您的位置:首页 > 其它

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

2010-07-23 10:54 316 查看
索引属性改进了在 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);

转载请注明:

自贡人才网,http://www.rcwqzg.com

金三维网络,http://www.jinsanwei.com

作者:Leek
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐