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);
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】博客园社区专享云产品让利特惠,阿里云新客6.5折上折
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步