自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

.NET Excel操作技巧總結(jié)

開發(fā) 后端
.NET操作Excel是一個很有用的功能。本文介紹一些常用的.NET Excel操作技巧。

通過.NET可以操作Excel,如新建文件,新建Workbook,設(shè)置單元格等等。下面就介紹一些常用的.NET Excel操作技巧。

1. 創(chuàng)建一個新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. 給單元格設(shè)置變量: 

  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. }   

現(xiàn)在我們來看看如何使用數(shù)組,他有些類似于設(shè)置單元格。僅僅需要的改變只是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. 將查詢數(shù)據(jù)通過C#導入Excel表的簡單方法
  2. C# Excel COM組件的使用
  3. C# 操作Excel之寫入Excel操作淺析
  4. C# 操作Excel基礎(chǔ)實例淺析
  5. C# 操作Excel之Excel操作方法調(diào)用
責任編輯:yangsai 來源: 中國.NET論壇
相關(guān)推薦

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-10-29 11:08:20

ADO.NET Dat

2010-01-12 18:00:50

VB.NET界面

2010-01-20 18:51:16

VB.NET修改系統(tǒng)時

2010-01-15 13:52:42

VB.NET屬性設(shè)置

2010-01-06 17:02:28

.Net Framew

2009-11-02 10:42:04

VB.NET EXCE

2010-03-03 13:12:56

Python圖像處理

2010-01-05 17:59:54

.NET Framew

2009-12-28 15:46:22

ADO.NET操作

2010-01-15 19:04:09

2012-08-03 10:20:06

ASP.NET

2009-11-02 10:30:37

VB.NET EXCE

2010-01-11 14:28:14

VB.NET操作Exc

2009-11-13 09:24:17

2010-02-06 15:15:31

C++ Builder

2010-01-21 14:49:44

VB.NET操作Wor

2009-07-28 10:26:30

ASP.NET操作Ex
點贊
收藏

51CTO技術(shù)棧公眾號