C#4.0新Feature官方文档和Sample Code下载 [本文提供Office编程的一段Sample Code作为预览]
先把重点写在前头:
http://code.msdn.microsoft.com/Release/ProjectReleases.aspx?ProjectName=csharpfuture&ReleaseId=1686
以上这个地址是下载官方C#4.0的最新文档和Sample Code,而以下正是我看到第一个例子,关于Office的操作。可见它的例子质量还是蛮高的,值得学习,所以分享给大家,也许有些人已经知道这个链接,如果不能下载,留言告诉我,我到时下载到cnblogs里。
前言:
从C#1.0,2.0,3.0,4.0一路走来,我们可能不是去掌握它的技术,而是掌握它为什么会这样变化,从2.0的泛型,到3.0的Lamda,Linq,集合对象初始化等,再到现在4.0的动态编程,可选参数等,微软的种种更新让我们都跟着很累,但是,如果我们深入进去多看看“为什么”,也许就不是很累,而是会发现这些更新都是必然。
下面代码在.NET4.0环境下编译,是关于将一些数据写入Excel并操作Word的一小段代码,代码很简单,但需要注意到是下面的一些C#3.0/4.0里的知识,如Lamda, Named Parameter等。
源代码
using System;
using System.Collections.Generic;
using Excel = Microsoft.Office.Interop.Excel;
using Word = Microsoft.Office.Interop.Word;
namespace Zivsoft.OfficeDemo
{
/// <summary>
/// 域名信息
/// </summary>
public class DomainInfo
{
public string DomainName { get; set; }
public string Owner { get; set; }
}
public class Program
{
/// <summary>
/// 入口
/// </summary>
/// <param name="args"></param>
static void Main(string[] args)
{
//注意定义方式(C#3.0)
var checkAccounts = new List<DomainInfo> {
new DomainInfo {
DomainName = "www.zivsoft.com",
Owner = "zivsoft"
},
new DomainInfo {
DomainName = "lihua.club.officelive.com",
Owner = "microsoft"
}
};
DisplayInExcel(checkAccounts, (domain, cell) =>//注意参数2, Lambda, domain是自定义类DomainInfo,cell是Excel.Range
{
// Lambda内部业务
cell.Value2 = domain.DomainName;
cell.get_Offset(0, 1).Value2 = domain.Owner;
if (domain.Owner == "zivsoft")//如果owner是zivsoft,改变颜色
{
cell.Interior.Color = 255;
cell.get_Offset(0, 1).Interior.Color = 255;
}
});
//操作Word对象
var word = new Word.Application();
word.Visible = true;
word.Documents.Add();
word.Selection.PasteSpecial(Link: true, DisplayAsIcon: true);//注意这是C#4.0中可选参数用法
}
public static void DisplayInExcel(IEnumerable<DomainInfo> accounts,
Action<DomainInfo, Excel.Range> DisplayFunc)//参数2中的Action是一个泛型delegate
{
//操作Excel对象
var xl = new Excel.Application();
xl.Workbooks.Add();
xl.Visible = true;
xl.Cells[1, 1] = "域名";
xl.Cells[1, 2] = "拥有者";
xl.Cells[2, 1].Select();
foreach (var ac in accounts)
{
DisplayFunc(ac, xl.ActiveCell);
xl.ActiveCell.get_Offset(1, 0).Select();
}
xl.get_Range("A1:B3").Copy();
xl.Columns[1].AutoFit();
xl.Columns[2].AutoFit();
}
}
}
using System.Collections.Generic;
using Excel = Microsoft.Office.Interop.Excel;
using Word = Microsoft.Office.Interop.Word;
namespace Zivsoft.OfficeDemo
{
/// <summary>
/// 域名信息
/// </summary>
public class DomainInfo
{
public string DomainName { get; set; }
public string Owner { get; set; }
}
public class Program
{
/// <summary>
/// 入口
/// </summary>
/// <param name="args"></param>
static void Main(string[] args)
{
//注意定义方式(C#3.0)
var checkAccounts = new List<DomainInfo> {
new DomainInfo {
DomainName = "www.zivsoft.com",
Owner = "zivsoft"
},
new DomainInfo {
DomainName = "lihua.club.officelive.com",
Owner = "microsoft"
}
};
DisplayInExcel(checkAccounts, (domain, cell) =>//注意参数2, Lambda, domain是自定义类DomainInfo,cell是Excel.Range
{
// Lambda内部业务
cell.Value2 = domain.DomainName;
cell.get_Offset(0, 1).Value2 = domain.Owner;
if (domain.Owner == "zivsoft")//如果owner是zivsoft,改变颜色
{
cell.Interior.Color = 255;
cell.get_Offset(0, 1).Interior.Color = 255;
}
});
//操作Word对象
var word = new Word.Application();
word.Visible = true;
word.Documents.Add();
word.Selection.PasteSpecial(Link: true, DisplayAsIcon: true);//注意这是C#4.0中可选参数用法
}
public static void DisplayInExcel(IEnumerable<DomainInfo> accounts,
Action<DomainInfo, Excel.Range> DisplayFunc)//参数2中的Action是一个泛型delegate
{
//操作Excel对象
var xl = new Excel.Application();
xl.Workbooks.Add();
xl.Visible = true;
xl.Cells[1, 1] = "域名";
xl.Cells[1, 2] = "拥有者";
xl.Cells[2, 1].Select();
foreach (var ac in accounts)
{
DisplayFunc(ac, xl.ActiveCell);
xl.ActiveCell.get_Offset(1, 0).Select();
}
xl.get_Range("A1:B3").Copy();
xl.Columns[1].AutoFit();
xl.Columns[2].AutoFit();
}
}
}
程序运行结果