溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

C#的Excel編程技巧有哪些

發(fā)布時間:2021-12-01 14:12:06 來源:億速云 閱讀:120 作者:小新 欄目:編程語言

小編給大家分享一下C#的Excel編程技巧有哪些,相信大部分人都還不怎么了解,因此分享這篇文章給大家參考一下,希望大家閱讀完這篇文章后大有收獲,下面讓我們一起去了解一下吧!

一.C#的Excel程序設(shè)計及運行環(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é)將通過一個程序來介紹Visual C#讀取Excel表格中的數(shù)據(jù),并把數(shù)據(jù)以DataGrid的形式顯示出來。

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

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

//創(chuàng)建一個數(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ù)鏈接,得到一個數(shù)據(jù)集  OleDbDataAdapter myCommand = new OleDbDataAdapter ( strCom , myConn ) ;  file://創(chuàng)建一個 DataSet對象  myDataSet = new DataSet ( ) ;  file://得到自己的DataSet對象  myCommand.Fill ( myDataSet , "[Sheet1$]" ) ;  file://關(guān)閉此數(shù)據(jù)鏈接  myConn.Close ( ) ;

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

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

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

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

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

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

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

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://初始化窗體中的各個組件  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)建一個數(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ù)鏈接,得到一個數(shù)據(jù)集  OleDbDataAdapter myCommand = new OleDbDataAdapter ( strCom , myConn ) ;  file://創(chuàng)建一個 DataSet對象  myDataSet = new DataSet ( ) ;  file://得到自己的DataSet對象  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顯示出來!" ;  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 ( ) ) ;  }  }

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

C#的Excel編程技巧有哪些

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

(4).總結(jié):

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

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

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

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

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

tlbimp excel9.olb

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

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

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

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

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

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

在命名空間"Excel"中,還定義了一個類"Cell",這個類所代表的就是Excel表格中的一個下單元。通過給差"Cell"賦值,從而實現(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表格中存儲數(shù)據(jù)的程序代碼(Excel.cs):

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

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://初始化窗體中的各個組件  InitializeComponent ( ) ;  }  file://清除程序中使用的各個資源  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).編譯源程序和程序運行界面:

在經(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",運行后界面如下:

C#的Excel編程技巧有哪些

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

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

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

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

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

以上是“C#的Excel編程技巧有哪些”這篇文章的所有內(nèi)容,感謝各位的閱讀!相信大家都有了一定的了解,希望分享的內(nèi)容對大家有所幫助,如果還想學(xué)習(xí)更多知識,歡迎關(guān)注億速云行業(yè)資訊頻道!

向AI問一下細節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI