日韩无码专区无码一级三级片|91人人爱网站中日韩无码电影|厨房大战丰满熟妇|AV高清无码在线免费观看|另类AV日韩少妇熟女|中文日本大黄一级黄色片|色情在线视频免费|亚洲成人特黄a片|黄片wwwav色图欧美|欧亚乱色一区二区三区

RELATEED CONSULTING
相關咨詢
選擇下列產(chǎn)品馬上在線溝通
服務時間:8:30-17:00
你可能遇到了下面的問題
關閉右側工具欄

新聞中心

這里有您想知道的互聯(lián)網(wǎng)營銷解決方案
幾條C#的Excel編程技巧

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

創(chuàng)新互聯(lián)建站服務項目包括志丹網(wǎng)站建設、志丹網(wǎng)站制作、志丹網(wǎng)頁制作以及志丹網(wǎng)絡營銷策劃等。多年來,我們專注于互聯(lián)網(wǎng)行業(yè),利用自身積累的技術優(yōu)勢、行業(yè)經(jīng)驗、深度合作伙伴關系等,向廣大中小型企業(yè)、政府機構等提供互聯(lián)網(wǎng)行業(yè)的解決方案,志丹網(wǎng)站推廣取得了明顯的社會效益與經(jīng)濟效益。目前,我們服務的客戶以成都為中心已經(jīng)輻射到志丹省份的部分城市,未來相信會繼續(xù)擴大服務區(qū)域并繼續(xù)獲得客戶的支持與信任!

一.C#的Excel程序設計及運行環(huán)境

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

(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ù),具體如下:

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

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

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

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

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

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

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

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

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

下圖是程序編譯后,運行結果:

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

(4).總結:

以上只是讀取了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組件,就必須把他們轉換成受管代碼的類庫。所以在用Visual C#調(diào)用Excel表格之前,必須完成從COM組件的非受管代碼到受管代碼的類庫的轉換。

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

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

tlbimp excel9.olb

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

五.C#的Excel編程技巧總結:

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


分享文章:幾條C#的Excel編程技巧
本文來源:http://www.5511xx.com/article/djgehsc.html