.NET Excel操作技巧总结

开发 后端
.NET操作Excel是一个很有用的功能。本文介绍一些常用的.NET Excel操作技巧。

通过.NET可以操作Excel,如新建文件,新建Workbook,设置单元格等等。下面就介绍一些常用的.NET Excel操作技巧。

1. 创建一个新Excel的Application: 

  1. Application exc = new Application();   
  2. if (exc == null) {   
  3. Console.WriteLine("ERROR: EXCEL couldn't be started");   
  4. return 0;   
  5. }  

2. 让这个工程可见: 

  1. exc.set_Visible(0, true);   

3. 获取WorkBooks集合: 

  1. Workbooks workbooks = exc.Workbooks;   

4. 加入新的WorkBook: 

  1. _Workbook workbook = workbooks.Add(XlWBATemplate.xlWBATWorksheet, 0);   

5. 获取WorkSheets集合: 

  1. _Worksheet worksheet = (_Worksheet) sheets.get_Item(1);   
  2. if (worksheet == null) {   
  3. Console.WriteLine ("ERROR in worksheet == null");   
  4. }  

6. 给单元格设置变量: 

  1. Range range1 = worksheet.get_Range("C1", Missing.Value);   
  2. if (range1 == null) {   
  3. Console.WriteLine ("ERROR: range == null");   
  4. }   
  5. const int nCells = 1;   
  6. Object[] args1 = new Object[1];   
  7. args1[0] = nCells;   
  8. range1.GetType().InvokeMember("Value", BindingFlags.SetProperty, null, range1, args1);  

.NET Excel操作技巧例程: 

  1. using System;   
  2. using System.Reflection;    
  3. using System.Runtime.InteropServices;    
  4. using Excel;   
  5.  
  6. class Excel {   
  7. public static int Main() {   
  8. Application exc = new Application();   
  9. if (exc == null) {   
  10. Console.WriteLine("ERROR: EXCEL couldn't be started!");   
  11. return 0;   
  12. }   
  13.  
  14. exc.set_Visible(0, true);    
  15. Workbooks workbooks = exc.Workbooks;   
  16. _Workbook workbook = workbooks.Add(XlWBATemplate.xlWBATWorksheet, 0);    
  17. Sheets sheets = workbook.Worksheets;   
  18.  
  19. _Worksheet worksheet = (_Worksheet) sheets.get_Item(1);   
  20. if (worksheet == null) {   
  21. Console.WriteLine ("ERROR: worksheet == null");   
  22. }   
  23.  
  24. Range range1 = worksheet.get_Range("C1", Missing.Value);   
  25. if (range1 == null) {   
  26. Console.WriteLine ("ERROR: range == null");   
  27. }   
  28. const int nCells = 1;   
  29. Object[] args1 = new Object[1];   
  30. args1[0] = nCells;   
  31. range1.GetType().InvokeMember("Value", BindingFlags.SetProperty, null,range1, args1);   
  32. return 100;   
  33. }   
  34. }   

现在我们来看看如何使用数组,他有些类似于设置单元格。仅仅需要的改变只是args2[0] = array2; 

  1. const int nCell = 5;   
  2. Range range2 = worksheet.get_Range("A1""E1");   
  3. int[] array2 = new int [nCell];   
  4. for (int i=0; i < array2.GetLength(0); i++) {   
  5. array2[i] = i+1;   
  6. }   
  7. Object[] args2 = new Object[1];   
  8. args2[0] = array2;   
  9. range2.GetType().InvokeMember("Value", BindingFlags.SetProperty, null, range2, args2);  

以上就介绍了一些常用的.NET Excel操作技巧,希望对大家有所帮助。

【编辑推荐】

  1. 将查询数据通过C#导入Excel表的简单方法
  2. C# Excel COM组件的使用
  3. C# 操作Excel之写入Excel操作浅析
  4. C# 操作Excel基础实例浅析
  5. C# 操作Excel之Excel操作方法调用
责任编辑:yangsai 来源: 中国.NET论坛
相关推荐

2010-01-15 15:51:43

VB.NET类添加方法

2009-11-04 16:55:16

ADO.NET Dat

2009-11-10 13:08:13

VB.NET编程技巧

2009-11-02 10:42:04

VB.NET EXCE

2010-01-05 17:59:54

.NET Framew

2010-01-06 17:02:28

.Net Framew

2009-10-29 11:08:20

ADO.NET Dat

2010-01-20 18:51:16

VB.NET修改系统时

2010-01-12 18:00:50

VB.NET界面

2010-01-15 13:52:42

VB.NET属性设置

2010-03-03 13:12:56

Python图像处理

2010-01-11 14:28:14

VB.NET操作Exc

2012-08-03 10:20:06

ASP.NET

2009-11-02 10:30:37

VB.NET EXCE

2009-12-28 15:46:22

ADO.NET操作

2010-01-15 19:04:09

2009-11-13 09:24:17

2010-02-06 15:15:31

C++ Builder

2009-07-28 10:26:30

ASP.NET操作Ex

2010-01-21 14:49:44

VB.NET操作Wor
点赞
收藏

51CTO技术栈公众号