C# 4.0中对Office编程的预览和对可选参数的体验

      最近随着VS2010 beta2的发布,有机会体验了一下C# 4.0中的很多新特性,这里对官方提供的一个简单的实例进行了一下分析,因为我的2010跑在win7上新建所有的Windows项目都会无响应,悲剧,所以我只得把官方的例子通过Web方式来运行了一下

//Accounts类

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

//VS2010中集成的.NET office开发资源包
using Microsoft.Office.Interop;
using Excel = Microsoft.Office.Interop.Excel;
using Word = Microsoft.Office.Interop.Word;

namespace Accounts
{

    //业务实体类
    public class Account
    {
        public int ID { get; set; }
        public double Balance { get; set; }
        public string AccountHolder { get; set; }
    }

    public static class AccountExtention
    {
        //把Excel内容嵌入Word文档中
        public static void EmbedInWordDocument()
        {
            //创建一个新的word对象
            var word = new Word.Application();
            //word 可见
            word.Visible = true;
            //新添加一个word文档
            word.Documents.Add();
            //C# 4.0中可选参数的用法,把剪贴板的内容嵌入到word文档里,设置是否以图标的形式来现实
            word.Selection.PasteSpecial(Link: true, DisplayAsIcon: false);
        }

        // accounts的扩展方法
        public static void DisplayInExcel(this IEnumerable<Accounts.Account> accounts,
                      Action<Accounts.Account, Excel.Range> DisplayFunc)
        {
            //创建一个新的excel对象
            var x1 = new Excel.Application();
            //添加Excel标签页
            x1.Workbooks.Add();
            //excel可见
            x1.Visible = true;
            //设置标题
            x1.get_Range("A1").Value2 = "ID";
            x1.get_Range("B1").Value2 = "Balance";
            x1.get_Range("C1").Value2 = "Account Holder";
            //把业务实体中的数据导入Excel表中
            x1.get_Range("A2").Select();
            foreach (var ac in accounts)   //定义ac为var,可以适用于ac的不同数据类型
            {
                DisplayFunc(ac, x1.ActiveCell);
                //添加完后往下移动一行
                x1.ActiveCell.get_Offset(1, 0).Select();
            }
            //设置Excel列的宽度为自适应
            ((Excel.Range)x1.Columns[1]).AutoFit();
            ((Excel.Range)x1.Columns[2]).AutoFit();
            ((Excel.Range)x1.Columns[3]).AutoFit();
            //把Excel表中的内容复制到剪贴板上
            x1.get_Range("A1:C4").Copy();
        }
    }
}

 

//aspx.cs类

 

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;


namespace WebApplication2
{
    public partial class _Default : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        //定义业务实体
        private static List<Accounts.Account> CreateAccountList()
        {
            //C# 3.0中的泛式定义
            var checkAccounts = new List<Accounts.Account> {
                new Accounts.Account{
                    ID = 1,
                    Balance = 285.93,
                    AccountHolder = "John Doe"
                },
                new Accounts.Account {
                    ID = 2,
                    Balance = 2349.23,
                    AccountHolder = "Richard Roe"
                },
                new Accounts.Account {
                    ID = 3,
                    Balance = -39.46,
                    AccountHolder = "I Dunoe"
                }
            };
            return checkAccounts;
        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            var checkAccounts = CreateAccountList();
            //第一个参数是上面定义的业务实体,第二个参数是一个Lambda表达式,account是定义的业务试题类Account,cell是Excel.Range对象
            Accounts.AccountExtention.DisplayInExcel(checkAccounts, (account, cell) =>
            {
                //Lamdba表达式的内部处理过程
                //把Account的每个属性对应到Excel表的列中
                cell.Value2 = account.ID;
                cell.get_Offset(0, 1).Value2 = account.Balance;
                cell.get_Offset(0, 2).Value2 = account.AccountHolder;
                //如果Balance<0,这行数据用红色的显示
                if (account.Balance < 0)
                {
                    cell.Interior.Color = 255;
                    cell.get_Offset(0, 1).Interior.Color = 255;
                    cell.get_Offset(0, 2).Interior.Color = 255;
                }
            });
           
            Accounts.AccountExtention.EmbedInWordDocument();
        }

      
    }
}

        在这里我们注意到,在上面的嵌入Word方法里,我们只通过word.Selection.PasteSpecial(Link: true, DisplayAsIcon: false);就实现了把剪贴板的内容嵌入word中的操作,这就是C# 4.0中的一个新的概念,可选参数的动态函数,大家知道,在C#以前版本中我们要实现相同的功能,需要定义大量的参数

object iconIndex = System.Reflection.Missing.Value;
object link = true;
object placement = System.Reflection.Missing.Value;
object displayAsIcon = false;
object dataType = System.Reflection.Missing.Value;
object iconFileName = System.Reflection.Missing.Value;
object iconLabel = System.Reflection.Missing.Value;
word.Selection.PasteSpecial(ref iconIndex,
      ref link,
      ref placement,
      ref displayAsIcon,
      ref dataType,
      ref iconFileName,
      ref iconLabel);

相比这下,C# 4.0的这个新特性应该说是相当不错的,大大的降低了程序的复杂度。下面是运行的结果

C# 4.0中对Office编程的预览和对可选参数的体验

C# 4.0中对Office编程的预览和对可选参数的体验

 

转载于:https://www.cnblogs.com/sleeplessC/articles/1618049.html