C#/.NET 4.0新特性生成Excel文档
2009-12-05 06:10
507 查看
直接上代码:
这个类提供了两个方法:按列数据创建excel文件和按行数据创建excel文件。
注意此时excelApp.Cells[row,column]的返回值已经是.NET 4.0中引入的dynamic类型,
而不再是原来的object类型,所以可以直接在上面调用.Value()方法,由DLR动态解析;
而在以前,必须首先强转其返回值为Range,写成:
此外,由于C# 4.0中命名参数和可选参数的引入,也使得对COM对象的方法调用变得相当
简洁,仅看最后的SaveAs方法,以前必须传递一堆Type.Missing对象以表示不传递此参
数或使用默认值,如:
非常头痛,现在只需用命名参数指定要给出的参数,其他的由于是可选参数,都会自动
给默认值:
意思非常清晰。
最后补上那个简单类ColumnData的定义:
引用Microsoft.Office.Excel 12.0简单测试通过。
using MSExcel = Microsoft.Office.Interop.Excel; public class ExcelHelper { /// <summary> /// Creates the excel file by column. /// </summary> /// <param name="filename">The filename.</param> /// <param name="columns">The columns.</param> public static void CreateExcelFileByColumn (string filename, IEnumerable<ColumnData> columns) { createExcelFile (filename, excelApp => { //Write data into the workbook by column. int columnIndex = 1; foreach (var column in columns) { //Write the header. excelApp.Cells[1, columnIndex].Value = column.Header; //Write the following lines in this column. int rowIndex = 2; foreach (var cell in column.Data) { excelApp.Cells[rowIndex++, columnIndex].Value = cell; } columnIndex++; } }); } /// <summary> /// Creates the excel file by row. /// </summary> /// <param name="filename">The filename.</param> /// <param name="rows">The rows.</param> public static void CreateExcelFileByRow (string filename, IEnumerable<IEnumerable> rows) { createExcelFile (filename, excelApp => { //Write data into the workbook by row. int rowIndex = 1; foreach (var row in rows) { int columnIndex = 1; foreach (var cell in row) { excelApp.Cells[rowIndex, columnIndex++].Value = cell; } rowIndex++; } }); } /// <summary> /// Creates the excel file and perform the specified action. /// </summary> /// <param name="filename">The filename.</param> /// <param name="action">The action.</param> private static void createExcelFile (string filename, Action<MSExcel.Application> action) { //Create the excel application and set it to run in background. var excelApp = new MSExcel.Application (); excelApp.Visible = false; //Add a new workbook. excelApp.Workbooks.Add (); //Perform the action. action (excelApp); //Save the workbook then close the file. excelApp.ActiveWorkbook.SaveAs (Filename: filename, FileFormat: MSExcel.XlFileFormat.xlWorkbookNormal); excelApp.ActiveWorkbook.Close (); //Exit the excel application. excelApp.Quit (); } }
这个类提供了两个方法:按列数据创建excel文件和按行数据创建excel文件。
注意此时excelApp.Cells[row,column]的返回值已经是.NET 4.0中引入的dynamic类型,
而不再是原来的object类型,所以可以直接在上面调用.Value()方法,由DLR动态解析;
而在以前,必须首先强转其返回值为Range,写成:
((MSExcel.Range)excelApp.Cells[row, column]).Value2 = value;
此外,由于C# 4.0中命名参数和可选参数的引入,也使得对COM对象的方法调用变得相当
简洁,仅看最后的SaveAs方法,以前必须传递一堆Type.Missing对象以表示不传递此参
数或使用默认值,如:
excelApp.ActiveWorkbook.SaveAs (filename, MSExcel.XlFileFormat.xlWorkbookNormal, Type.Missing, Type.Missing, Type.Missing, Type.Missing, MSExcel.XlSaveAsAccessMode.xlNoChange, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing);
非常头痛,现在只需用命名参数指定要给出的参数,其他的由于是可选参数,都会自动
给默认值:
excelApp.ActiveWorkbook.SaveAs (Filename: filename, FileFormat: MSExcel.XlFileFormat.xlWorkbookNormal);
意思非常清晰。
最后补上那个简单类ColumnData的定义:
/// <summary> /// Represents the header and data of a column. /// </summary> public class ColumnData { /// <summary> /// Gets or sets the header. /// </summary> /// <value>The header.</value> public string Header { get; set; } /// <summary> /// Gets or sets the data. /// </summary> /// <value>The data.</value> public IEnumerable Data { get; set; } }
引用Microsoft.Office.Excel 12.0简单测试通过。
相关文章推荐
- C#/.NET 4.0新特性生成Excel文档
- C# 4.0 新特性dynamic、可选参数、命名参数等 分类: .NET 2013-07-26 11:15 414人阅读 评论(0) 收藏
- 精进不休 .NET 4.0 (5) - C# 4.0 新特性之并行运算(Parallel)
- C# NPOI生成Excel文档(简单样式)
- 精进不休 .NET 4.0 (4) - C# 4.0 新特性之命名参数和可选参数, 动态绑定(dynamic), 泛型协变和逆变, CountdownEvent, Barrier
- 精进不休 .NET 4.0 (4) - C# 4.0 新特性之命名参数和可选参数, 动态绑定(dynamic), 泛型协变和逆变, CountdownEvent, Barrier
- 一起谈.NET技术,谈谈C# 4.0新特性“缺省参数”的实现
- 用C#实现生成PDF文档-.NET教程,C#语言
- 【C#附源码】数据库文档生成工具支持(Excel+Htm)
- 【C#附源码】数据库文档生成工具支持(Excel+Html)
- 精进不休 .NET 4.0 (4) - C# 4.0 新特性之命名参数和可选参数, 动态绑定(dynamic), 泛型协变和逆变, CountdownEvent, Barrier
- 精进不休 .NET 4.0 (5) - C# 4.0 新特性之并行运算(Parallel)
- 精进不休 .NET 4.0 (5) - C# 4.0 新特性之并行运算(Parallel) (收藏)
- 精进不休 .NET 4.0 (4) - C# 4.0 新特性之命名参数和可选参数
- VS+C#由创建的Excel生成PDF文档
- 精进不休 .NET 4.0 (5) - C# 4.0 新特性之并行运算(Parallel)
- [转]浅析C#编程实现动态生成Word文档_.Net教程
- “.NET研究”谈谈C# 4.0新特性“缺省参数”的实现
- Excel文档生成类(C#版)
- .NET 4.0 (5) - C# 4.0 新特性之并行运算(Parallel)