幾條C#的Excel編程技巧
在辦公的時(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ù),具體如下:
- //創(chuàng)建一個(gè)數(shù)據(jù)鏈接
 - string strCon = " Provider = Microsoft.Jet.OLEDB.4.0 ; Data Source = c:\\sample.xls;Extended Properties=Excel 8.0" ;
 - OleDbConnection myConn = new OleDbConnection ( strCon ) ;
 - string strCom = " SELECT * FROM [Sheet1$] " ;
 - myConn.Open ( ) ;
 - file://打開數(shù)據(jù)鏈接,得到一個(gè)數(shù)據(jù)集
 - OleDbDataAdapter myCommand = new OleDbDataAdapter ( strCom , myConn ) ;
 - file://創(chuàng)建一個(gè) DataSet對(duì)象
 - myDataSet = new DataSet ( ) ;
 - file://得到自己的DataSet對(duì)象
 - myCommand.Fill ( myDataSet , "[Sheet1$]" ) ;
 - file://關(guān)閉此數(shù)據(jù)鏈接
 - 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)了:
- DataGrid1.DataMember= "[Sheet1$]" ;
 - DataGrid1.DataSource = myDataSet ;
 
(3).用Visual C#讀取Excel表格,并用DataGrid顯示出來(lái)的程序代碼(Read.cs)和程序運(yùn)行的界面:
掌握了上面二點(diǎn),水到渠成就可以得到以下代碼:
- using System ;
 - using System.Drawing ;
 - using System.Collections ;
 - using System.ComponentModel ;
 - using System.Windows.Forms ;
 - using System.Data ;
 - using System.Data.OleDb ;
 - public class Form1 : Form
 - {
 - private Button button1 ;
 - private System.Data.DataSet myDataSet ;
 - private DataGrid DataGrid1 ;
 - private System.ComponentModel.Container components = null ;
 - public Form1 ( )
 - {
 - file://初始化窗體中的各個(gè)組件
 - InitializeComponent ( ) ;
 - file://打開數(shù)據(jù)鏈接,得到數(shù)據(jù)集
 - GetConnect ( ) ;
 - }
 - file://清除程序中使用過的資源
 - protected override void Dispose ( bool disposing )
 - {
 - if ( disposing )
 - {
 - if ( components != null )
 - {
 - components.Dispose ( ) ;
 - }
 - }
 - base.Dispose ( disposing ) ;
 - }
 - private void GetConnect ( )
 - {
 - file://創(chuàng)建一個(gè)數(shù)據(jù)鏈接
 - string strCon = " Provider = Microsoft.Jet.OLEDB.4.0 ; Data Source = c:\\sample.xls;Extended Properties=Excel 8.0" ;
 - OleDbConnection myConn = new OleDbConnection ( strCon ) ;
 - string strCom = " SELECT * FROM [Sheet1$] " ;
 - myConn.Open ( ) ;
 - file://打開數(shù)據(jù)鏈接,得到一個(gè)數(shù)據(jù)集
 - OleDbDataAdapter myCommand = new OleDbDataAdapter ( strCom , myConn ) ;
 - file://創(chuàng)建一個(gè) DataSet對(duì)象
 - myDataSet = new DataSet ( ) ;
 - file://得到自己的DataSet對(duì)象
 - myCommand.Fill ( myDataSet , "[Sheet1$]" ) ;
 - file://關(guān)閉此數(shù)據(jù)鏈接
 - myConn.Close ( ) ;
 - }
 - private void InitializeComponent ( )
 - {
 - DataGrid1 = new DataGrid ( ) ;
 - button1 = new Button ( ) ;
 - SuspendLayout ( ) ;
 - DataGrid1.Name = "DataGrid1";
 - DataGrid1.Size = new System.Drawing.Size ( 400 , 200 ) ;
 - button1.Location = new System.Drawing.Point ( 124 , 240 ) ;
 - button1.Name = "button1" ;
 - button1.TabIndex = 1 ;
 - button1.Text = "讀取數(shù)據(jù)" ;
 - button1.Size = new System.Drawing.Size (84 , 24 ) ;
 - button1.Click += new System.EventHandler ( this.button1_Click ) ;
 - this.AutoScaleBaseSize = new System.Drawing.Size ( 6 , 14 ) ;
 - this.ClientSize = new System.Drawing.Size ( 400 , 280 ) ;
 - this.Controls.Add ( button1 ) ;
 - this.Controls.Add ( DataGrid1 ) ;
 - this.Name = "Form1" ;
 - this.Text = "讀取Excle表格中的數(shù)據(jù),并用DataGrid顯示出來(lái)!" ;
 - this.ResumeLayout ( false ) ;
 - }
 - private void button1_Click ( object sender , System.EventArgs e )
 - {
 - DataGrid1.DataMember= "[Sheet1$]" ;
 - DataGrid1.DataSource = myDataSet ;
 - }
 - static void Main ( )
 - {
 - Application.Run ( new Form1 ( ) ) ;
 - }
 - }
 
下圖是程序編譯后,運(yùn)行結(jié)果:

圖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表格的工作,具體如下:
- Excel.Application excel = new Excel.Application ( ) ;
 - excel.Application.Workbooks.Add ( true ) ;
 - 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ù)。
- Excel.Application excel = new Excel.Application ( ) ;
 - excel.Application.Workbooks.Add ( true ) ;
 - excel.Cells[ 1 , 1 ] = "***行***列" ;
 - excel.Cells[ 1 , 2 ] = "***行第二列" ;
 - excel.Cells[ 2 , 1 ] = "第二行***列" ;
 - excel.Cells[ 2 , 2 ] = "第二行第二列" ;
 - excel.Cells[ 3 , 1 ] = "第三行***列" ;
 - excel.Cells[ 3 , 2 ] = "第三行第二列" ;
 - excel.Visible = true ;
 
(4). Visual C#調(diào)用Excel表格,并在Excel表格中存儲(chǔ)數(shù)據(jù)的程序代碼(Excel.cs):
了解了上面的這些知識(shí),得到完成上述功能的程序代碼就顯得比較容易了,具體如下:
- using System ;
 - using System.Drawing ;
 - using System.Collections ;
 - using System.ComponentModel ;
 - using System.Windows.Forms ;
 - using System.Data ;
 - using System.Data.SqlClient ;
 - public class Form1 : Form
 - {
 - private Button button1 ;
 - private System.ComponentModel.Container components = null ;
 - public Form1 ( )
 - {
 - file://初始化窗體中的各個(gè)組件
 - InitializeComponent ( ) ;
 - }
 - file://清除程序中使用的各個(gè)資源
 - protected override void Dispose ( bool disposing )
 - {
 - if ( disposing )
 - {
 - if ( components != null )
 - {
 - components.Dispose ( ) ;
 - }
 - }
 - base.Dispose( disposing ) ;
 - }
 - private void InitializeComponent ( )
 - {
 - button1 = new Button ( ) ;
 - SuspendLayout ( ) ;
 - button1.Location = new System.Drawing.Point ( 32 , 72 ) ;
 - button1.Name = "button1" ;
 - button1.Size = new System.Drawing.Size ( 100 , 30 ) ;
 - button1.TabIndex = 0 ;
 - button1.Text = "調(diào)用Excel文件!" ;
 - button1.Click += new System.EventHandler ( button1_Click ) ;
 - AutoScaleBaseSize = new System.Drawing.Size ( 5 , 13 ) ;
 - this.ClientSize = new System.Drawing.Size ( 292 , 273 ) ;
 - this.Controls.Add ( button1 ) ;
 - this.Name = "Form1" ;
 - this.Text = "如何用Visual C#調(diào)用Excel表格!" ;
 - this.ResumeLayout ( false ) ;
 - }
 - static void Main ( )
 - {
 - Application.Run ( new Form1 ( ) ) ;
 - }
 - private void button1_Click ( object sender , System.EventArgs e )
 - {
 - Excel.Application excel = new Excel.Application ( ) ;
 - excel.Application.Workbooks.Add ( true ) ;
 - excel.Cells[ 1 , 1 ] = "***行***列" ;
 - excel.Cells[ 1 , 2 ] = "***行第二列" ;
 - excel.Cells[ 2 , 1 ] = "第二行***列" ;
 - excel.Cells[ 2 , 2 ] = "第二行第二列" ;
 - excel.Cells[ 3 , 1 ] = "第三行***列" ;
 - excel.Cells[ 3 , 2 ] = "第三行第二列" ;
 - excel.Visible = true ;
 - }
 - }
 
(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)行后界面如下:
 
圖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的代碼就可以了,具體如下:
- Word.Application word = new Word.Application ( ) ;
 - 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套件中的成員也具有十分的借鑒意義。
【編輯推薦】















 
 
 

 
 
 
 