偷偷摘套内射激情视频,久久精品99国产国产精,中文字幕无线乱码人妻,中文在线中文a,性爽19p

幾條C#的Excel編程技巧

開發(fā) 后端
Excel是微軟公司辦公自動(dòng)化套件中的一個(gè)軟件,他主要是用來(lái)處理電子表格。Excel以其功能強(qiáng)大,界面友好等受到了許多用戶的歡迎。這里我們將介紹幾條C#的Excel相關(guān)編程技巧

在辦公的時(shí)候,正是由于Excel的這么多的優(yōu)點(diǎn),許多重要的數(shù)據(jù),往往以Excel電子表格的形式存儲(chǔ)起來(lái)。這樣就給程序員帶來(lái)了一個(gè)問題,雖然Excel功能比較強(qiáng)大,但畢竟不是數(shù)據(jù)庫(kù),在程序中處理數(shù)據(jù)庫(kù)中的數(shù)據(jù)比其處理Excel表格中的數(shù)據(jù)容易許多。那么如何用Visual C#的Excel表格中的數(shù)據(jù)?在以前用Delphi編程的時(shí)候,對(duì)于不同的用戶,他們對(duì)于打印的需求是不一樣的,如果要使得程序中的打印功能適用于每一個(gè)用戶,可以想象程序設(shè)計(jì)是十分復(fù)雜的。這時(shí)想到Excel,由于Excel表格的功能強(qiáng)大,又由于幾乎每一臺(tái)機(jī)器都安裝了它,如果把程序處理的結(jié)果放到Excel表格中,這樣每一個(gè)用戶就可以根據(jù)自己的需要在Excel中定制自己的打印。這樣不僅使得程序設(shè)計(jì)簡(jiǎn)單,而且又滿足了諸多用戶的要求,更加實(shí)用了。那么用Visual C#的Excel,如何又把數(shù)據(jù)存放到Excel表格中?本文就來(lái)探討一下上述問題的解決辦法。

一.C#的Excel程序設(shè)計(jì)及運(yùn)行環(huán)境

(1).微軟視窗2000 服務(wù)器版

(2)..Net Framework SDK Beta 2

(3).Microsoft Data Access Component 2.6以上版本(MDAC2.6)

(4).Office 2000套件

二.Visual C#的Excel表格中的數(shù)據(jù):

本節(jié)將通過一個(gè)程序來(lái)介紹Visual C#讀取Excel表格中的數(shù)據(jù),并把數(shù)據(jù)以DataGrid的形式顯示出來(lái)。

(1).如何讀取數(shù)據(jù):

其實(shí)讀取Excel表格中的數(shù)據(jù)和讀取數(shù)據(jù)庫(kù)中的數(shù)據(jù)是非常類似的,因?yàn)樵谀撤N程度上Excel表格可以看成是一張一張的數(shù)據(jù)表。其二者的主要區(qū)別在于所使用的數(shù)據(jù)引擎不一樣。在本文的程序中,通過下列代碼實(shí)現(xiàn)讀取Excel表格數(shù)據(jù),具體如下:

  1. //創(chuàng)建一個(gè)數(shù)據(jù)鏈接  
  2. string strCon = " Provider = Microsoft.Jet.OLEDB.4.0 ; Data Source = c:\\sample.xls;Extended Properties=Excel 8.0" ;  
  3. OleDbConnection myConn = new OleDbConnection ( strCon ) ;  
  4. string strCom = " SELECT * FROM [Sheet1$] " ;  
  5. myConn.Open ( ) ;  
  6. file://打開數(shù)據(jù)鏈接,得到一個(gè)數(shù)據(jù)集  
  7. OleDbDataAdapter myCommand = new OleDbDataAdapter ( strCom , myConn ) ;  
  8. file://創(chuàng)建一個(gè) DataSet對(duì)象  
  9. myDataSet = new DataSet ( ) ;  
  10. file://得到自己的DataSet對(duì)象  
  11. myCommand.Fill ( myDataSet , "[Sheet1$]" ) ;  
  12. file://關(guān)閉此數(shù)據(jù)鏈接  
  13. myConn.Close ( ) ; 

怎么樣讀取Excel表格中的數(shù)據(jù)其實(shí)和讀取數(shù)據(jù)庫(kù)中的數(shù)據(jù)沒有什么實(shí)質(zhì)上的區(qū)別。

注釋:這里讀取的是C盤根目錄下的"Sample.xls"文件。

(2).用DataGrid來(lái)顯示得到的數(shù)據(jù)集:

在得到DataSet對(duì)象后,只需要通過下列二行代碼,就可以把數(shù)據(jù)集用DataGrid顯示出來(lái)了:

  1. DataGrid1.DataMember"[Sheet1$]" ;  
  2. DataGrid1.DataSource = myDataSet ; 

(3).用Visual C#讀取Excel表格,并用DataGrid顯示出來(lái)的程序代碼(Read.cs)和程序運(yùn)行的界面:

掌握了上面二點(diǎn),水到渠成就可以得到以下代碼:

  1. using System ;  
  2. using System.Drawing ;  
  3. using System.Collections ;  
  4. using System.ComponentModel ;  
  5. using System.Windows.Forms ;  
  6. using System.Data ;  
  7. using System.Data.OleDb ;  
  8. public class Form1 : Form  
  9. {  
  10. private Button button1 ;  
  11. private System.Data.DataSet myDataSet ;  
  12. private DataGrid DataGrid1 ;  
  13. private System.ComponentModel.Container components = null ;  
  14. public Form1 ( )  
  15. {  
  16. file://初始化窗體中的各個(gè)組件  
  17. InitializeComponent ( ) ;  
  18. file://打開數(shù)據(jù)鏈接,得到數(shù)據(jù)集  
  19. GetConnect ( ) ;  
  20. }  
  21. file://清除程序中使用過的資源  
  22. protected override void Dispose ( bool disposing )  
  23. {  
  24. if ( disposing )  
  25. {  
  26. if ( components != null )  
  27. {  
  28. components.Dispose ( ) ;  
  29. }  
  30. }  
  31. base.Dispose ( disposing ) ;  
  32. }  
  33. private void GetConnect ( )  
  34. {  
  35. file://創(chuàng)建一個(gè)數(shù)據(jù)鏈接  
  36. string strCon = " Provider = Microsoft.Jet.OLEDB.4.0 ; Data Source = c:\\sample.xls;Extended Properties=Excel 8.0" ;  
  37. OleDbConnection myConn = new OleDbConnection ( strCon ) ;  
  38. string strCom = " SELECT * FROM [Sheet1$] " ;  
  39. myConn.Open ( ) ;  
  40. file://打開數(shù)據(jù)鏈接,得到一個(gè)數(shù)據(jù)集  
  41. OleDbDataAdapter myCommand = new OleDbDataAdapter ( strCom , myConn ) ;  
  42. file://創(chuàng)建一個(gè) DataSet對(duì)象  
  43. myDataSet = new DataSet ( ) ;  
  44. file://得到自己的DataSet對(duì)象  
  45. myCommand.Fill ( myDataSet , "[Sheet1$]" ) ;  
  46. file://關(guān)閉此數(shù)據(jù)鏈接  
  47. myConn.Close ( ) ;  
  48. }  
  49. private void InitializeComponent ( )  
  50. {  
  51. DataGrid1 = new DataGrid ( ) ;  
  52. button1 = new Button ( ) ;  
  53. SuspendLayout ( ) ;  
  54. DataGrid1.Name = "DataGrid1";  
  55. DataGrid1.Size = new System.Drawing.Size ( 400 , 200 ) ;  
  56. button1.Location = new System.Drawing.Point ( 124 , 240 ) ;  
  57. button1.Name = "button1" ;  
  58. button1.TabIndex = 1 ;  
  59. button1.Text = "讀取數(shù)據(jù)" ;  
  60. button1.Size = new System.Drawing.Size (84 , 24 ) ;  
  61. button1.Click += new System.EventHandler ( this.button1_Click ) ;  
  62. this.AutoScaleBaseSize = new System.Drawing.Size ( 6 , 14 ) ;  
  63. this.ClientSize = new System.Drawing.Size ( 400 , 280 ) ;  
  64. this.Controls.Add ( button1 ) ;  
  65. this.Controls.Add ( DataGrid1 ) ;  
  66. this.Name = "Form1" ;  
  67. this.Text = "讀取Excle表格中的數(shù)據(jù),并用DataGrid顯示出來(lái)!" ;  
  68. this.ResumeLayout ( false ) ;  
  69. }  
  70. private void button1_Click ( object sender , System.EventArgs e )  
  71. {  
  72. DataGrid1.DataMember"[Sheet1$]" ;  
  73. DataGrid1.DataSource = myDataSet ;  
  74. }  
  75. static void Main ( )  
  76. {  
  77. Application.Run ( new Form1 ( ) ) ;  
  78. }  

下圖是程序編譯后,運(yùn)行結(jié)果:

 

用Visual C#讀取Excel

圖01:用Visual C#讀取"c:\sample.xls"的運(yùn)行界面

(4).總結(jié):

以上只是讀取了Excel表格中"Sheet1"中的數(shù)據(jù),對(duì)于其他"Sheet"中的內(nèi)容,可以參照讀取"Sheet1"中的程序,只作一點(diǎn)修改就可以了,譬如要讀取"Sheet2"中的內(nèi)容,只需要把"Read.cs"程序中的"Sheet1$"改成"Sheet2$"就可以了。

三.Visual C#的Excel表格,并在Excel表格中存儲(chǔ)數(shù)據(jù):

在Visual C#中調(diào)用Excel表格,并不像讀取Excel表格中的數(shù)據(jù)那么容易了,因?yàn)樵赩isual C#中調(diào)用Excel表格要使用到Excel的COM組件。如果你安裝Office套件在"C"盤,那么在"C:\Program Files\Microsoft Office\Office"可以找到這個(gè)COM組件"EXCEL9.OLB",在《Visual C#如何使用Active X組件》一文中,這些COM組件都是非受管代碼的,要在Visual C#中使用這些非受管代碼的COM組件,就必須把他們轉(zhuǎn)換成受管代碼的類庫(kù)。所以在用Visual C#調(diào)用Excel表格之前,必須完成從COM組件的非受管代碼到受管代碼的類庫(kù)的轉(zhuǎn)換。

(1).非受管代碼COM組件轉(zhuǎn)換成受管代碼的類庫(kù):

首先把COM組件"EXCEL9.OLB"拷貝到C盤的根目錄下,然后輸入下列命令:

tlbimp excel9.olb

這樣在C盤的根目錄下面就產(chǎn)生了三個(gè)DLL文件:"Excel.dll"、"Office.dll"、"VBIDE.dll"。在產(chǎn)生了上面的三個(gè)文件后,這種轉(zhuǎn)換就成功完成了。在下面的程序中,就可以利用這轉(zhuǎn)換好的三個(gè)類庫(kù)編寫和Excel表格相關(guān)的各種操作了。

(2).Visual C#打開Excel表格:

在"Excel.dll"中定義了一個(gè)命名空間"Excel",在差命名空間中封裝了一個(gè)類"Application",這個(gè)類和啟動(dòng)Excel表格有非常重要的關(guān)系,在Visual C#中,只需要下列三行代碼就可以完成打開Excel表格的工作,具體如下:

  1. Excel.Application excel = new Excel.Application ( ) ;  
  2. excel.Application.Workbooks.Add ( true ) ;  
  3. excel.Visible = true ;  

但此時(shí)的Excel表格是一個(gè)空的表格,沒有任何內(nèi)容,下面就來(lái)介紹如何往Excel表格中輸入數(shù)據(jù)。

(3).往Excel表格中輸入數(shù)據(jù):

在命名空間"Excel"中,還定義了一個(gè)類"Cell",這個(gè)類所代表的就是Excel表格中的一個(gè)下單元。通過給差"Cell"賦值,從而實(shí)現(xiàn)往Excel表格中輸入相應(yīng)的數(shù)據(jù),下列代碼功能是打開Excel表格,并且往表格輸入一些數(shù)據(jù)。

  1. Excel.Application excel = new Excel.Application ( ) ;  
  2. excel.Application.Workbooks.Add ( true ) ;  
  3. excel.Cells[ 1 , 1 ] = "***行***列" ;  
  4. excel.Cells[ 1 , 2 ] = "***行第二列" ;  
  5. excel.Cells[ 2 , 1 ] = "第二行***列" ;  
  6. excel.Cells[ 2 , 2 ] = "第二行第二列" ;  
  7. excel.Cells[ 3 , 1 ] = "第三行***列" ;  
  8. excel.Cells[ 3 , 2 ] = "第三行第二列" ;  
  9. excel.Visible = true ;  

(4). Visual C#調(diào)用Excel表格,并在Excel表格中存儲(chǔ)數(shù)據(jù)的程序代碼(Excel.cs):

了解了上面的這些知識(shí),得到完成上述功能的程序代碼就顯得比較容易了,具體如下:

  1. using System ;  
  2. using System.Drawing ;  
  3. using System.Collections ;  
  4. using System.ComponentModel ;  
  5. using System.Windows.Forms ;  
  6. using System.Data ;  
  7. using System.Data.SqlClient ;  
  8. public class Form1 : Form  
  9. {  
  10. private Button button1 ;  
  11. private System.ComponentModel.Container components = null ;  
  12. public Form1 ( )  
  13. {  
  14. file://初始化窗體中的各個(gè)組件  
  15. InitializeComponent ( ) ;  
  16. }  
  17. file://清除程序中使用的各個(gè)資源  
  18. protected override void Dispose ( bool disposing )  
  19. {  
  20. if ( disposing )  
  21. {  
  22. if ( components != null )  
  23. {  
  24. components.Dispose ( ) ;  
  25. }  
  26. }  
  27. base.Dispose( disposing ) ;  
  28. }  
  29. private void InitializeComponent ( )  
  30. {  
  31. button1 = new Button ( ) ;  
  32. SuspendLayout ( ) ;  
  33. button1.Location = new System.Drawing.Point ( 32 , 72 ) ;  
  34. button1.Name = "button1" ;  
  35. button1.Size = new System.Drawing.Size ( 100 , 30 ) ;  
  36. button1.TabIndex = 0 ;  
  37. button1.Text = "調(diào)用Excel文件!" ;  
  38. button1.Click += new System.EventHandler ( button1_Click ) ;  
  39. AutoScaleBaseSize = new System.Drawing.Size ( 5 , 13 ) ;  
  40. this.ClientSize = new System.Drawing.Size ( 292 , 273 ) ;  
  41. this.Controls.Add ( button1 ) ;  
  42. this.Name = "Form1" ;  
  43. this.Text = "如何用Visual C#調(diào)用Excel表格!" ;  
  44. this.ResumeLayout ( false ) ;  
  45. }  
  46. static void Main ( )  
  47. {  
  48. Application.Run ( new Form1 ( ) ) ;  
  49. }  
  50. private void button1_Click ( object sender , System.EventArgs e )  
  51. {  
  52. Excel.Application excel = new Excel.Application ( ) ;  
  53. excel.Application.Workbooks.Add ( true ) ;  
  54. excel.Cells[ 1 , 1 ] = "***行***列" ;  
  55. excel.Cells[ 1 , 2 ] = "***行第二列" ;  
  56. excel.Cells[ 2 , 1 ] = "第二行***列" ;  
  57. excel.Cells[ 2 , 2 ] = "第二行第二列" ;  
  58. excel.Cells[ 3 , 1 ] = "第三行***列" ;  
  59. excel.Cells[ 3 , 2 ] = "第三行第二列" ;  
  60. excel.Visible = true ;  
  61. }  
  62. }  
  63.  

(5).編譯源程序和程序運(yùn)行界面:

在經(jīng)過了下列命令編譯后:

Csc.exe /r:system.dll /r:system.windows.forms.dll /r:system.drawing.dll /r:excel.dll /r:office.dll /r:vbide.dll excel.cs

就可以得到"Excel.exe",運(yùn)行后界面如下:

運(yùn)行后界面 

圖02:Visual C#調(diào)用Excel表格,并存儲(chǔ)數(shù)據(jù)的程序運(yùn)行界面

四.Visual C#處理Office套件中的其他成員程序:

本文雖然只介紹了Visual C#在處理Excel表格中經(jīng)常遇到的一些問題的解決方法,但其實(shí)對(duì)Office套件的其他成員也有很強(qiáng)的借鑒意義,譬如Visual C#來(lái)處理Word文檔,在調(diào)用Word文檔的時(shí)候,必須先完成COM組件從非受管代碼到受管代碼的轉(zhuǎn)換,Word的COM組件位"MSWORD9.OLB",經(jīng)過轉(zhuǎn)換后也會(huì)產(chǎn)生三個(gè)DLL文件,但分別是"Word.dll"、"Office.dll"、"VBIDE.dll"。其實(shí)在Visual C#中調(diào)用Word,也非常容易。只需要把調(diào)用Excel表格中的代碼換成調(diào)用Word的代碼就可以了,具體如下:

  1. Word.Application word = new Word.Application ( ) ;  
  2. word.Application.Visible = true ; 

不信你試一下,看看是否達(dá)到你的要求。對(duì)于針對(duì)Word的其他的操作,總體來(lái)說和對(duì)Excel表格的操作相類似。由于針對(duì)Word只是一個(gè)文檔,程序?qū)ord進(jìn)行的操作是比較少的,所以就不一一介紹了。

五.C#的Excel編程技巧總結(jié):

本文介紹Visual C#來(lái)處理Excel表格的幾種最常遇到的情況,雖然針對(duì)的只是Excel表格,但對(duì)其他Office套件中的成員也具有十分的借鑒意義。

【編輯推薦】

  1. C#運(yùn)算符重載實(shí)現(xiàn)復(fù)數(shù)運(yùn)算
  2. C#運(yùn)算符重載實(shí)例解析
  3. C#運(yùn)算符重載的一些總結(jié)
  4. C#運(yùn)算符重載“>”的操作淺析
  5. C#運(yùn)算符優(yōu)先級(jí)介紹
責(zé)任編輯:彭凡 來(lái)源: 編程入門
相關(guān)推薦

2009-08-12 14:13:51

C#讀寫Excel文件

2009-08-03 16:22:58

C#編程技巧

2010-01-11 10:28:51

C++編程

2010-09-28 08:52:00

C#Visual Stud

2009-08-11 14:36:41

C#讀取Excel

2011-05-30 15:29:32

C++

2009-08-11 15:44:05

C#基本技巧

2009-08-27 16:54:59

C#開發(fā)技巧

2021-06-22 09:18:13

Python代碼技巧

2010-11-10 09:01:50

Visual Stud

2020-07-15 14:51:39

代碼C+開發(fā)

2009-08-13 10:15:50

C#讀取Excel

2009-08-21 09:14:47

C# Excel CO

2009-09-02 16:43:55

C#調(diào)用Excel的C

2009-08-24 11:02:52

C#接口映射

2009-08-26 10:34:15

C#類型C#變量

2009-08-24 09:55:26

C#接口轉(zhuǎn)換

2010-01-26 17:11:13

C++編程

2009-09-02 16:36:37

C#調(diào)用Excel對(duì)象

2009-08-18 13:49:21

C# 操作Excel
點(diǎn)贊
收藏

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