C#使用oledb操做excel文件的方法

本文實例講述了C#使用oledb操做excel文件的方法。分享給你們供你們參考。具體分析以下:web

無論什麼編程語言都會提供操做Excel文件的方式,C#操做Excel主要有如下幾種方式:編程

1.Excel編程語言

說明:利用Office 的Excel組件來操做excel文件
優勢:可以徹底操做Excel文件,生成豐富文件內容
缺點:須要電腦安裝Excel,會啓動Excel進程這在web上很不方便ide

2.OpenXML學習

說明:一個操做字處理文檔的組件包括Excel
優勢:可以操做操做Excel2007版本文件
缺點:只可以操做Excel2007文件ui

3.NPOIspa

說明:一個開源的Excel讀寫庫
優勢:不須要安裝Excel
缺點:只可以操做Excel2003文檔,對文檔內容控制不徹底.net

4.OleDb設計

說明:使用Microsoft Jet 提供程序用於鏈接到 Excel 工做簿,將Excel文件做爲數據源來讀寫
優勢:簡單快速,可以操做高版本Excel
缺點:只可以進行有限的操做(讀、寫)excel

今天學習使用OleDb操做Excel文件

鏈接字符串:Provider=Microsoft.Jet.OLEDB.4.0;Data Source=d:\test.xls;Extended Properties='Excel 8.0;HDR=Yes;IMEX=1;'
provider:表示提供程序名稱
Data Source:這裏填寫Excel文件的路徑
Extended Properties:設置Excel的特殊屬性
Extended Properties 取值:
Excel 8.0 針對Excel2000及以上版本,Excel5.0 針對Excel97。
HDR=Yes 表示第一行包含列名,在計算行數時就不包含第一行
IMEX 0:導入模式,1:導出模式:2混合模式

1.讀取excel文件

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
if (openFileDialog1.ShowDialog() == DialogResult.OK)
{
  String sConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;" +
  "Data Source=" +openFileDialog1.FileName+ ";" +
  "Extended Properties='Excel 8.0;HDR=Yes;IMEX=2'" ;
  //實例化一個Oledbconnection類(實現了IDisposable,要using)
  using (OleDbConnection ole_conn = new OleDbConnection(sConnectionString))
  {
   ole_conn.Open();
   using (OleDbCommand ole_cmd = ole_conn.CreateCommand())
   {
   //相似SQL的查詢語句這個[Sheet1$對應Excel文件中的一個工做表]
   ole_cmd.CommandText = "select * from [Sheet1$]" ;
   OleDbDataAdapter adapter = new OleDbDataAdapter(ole_cmd);
   DataSet ds = new DataSet();
   adapter.Fill(ds, "Sheet1" );
     for ( int i = 0; i < ds.Tables[0].Rows.Count; i++)
   {
    MessageBox.Show(ds.Tables[0].Rows[i][ "商家名稱" ].ToString());
   }
   }
  }
}

2.獲取工做簿中全部的工做表

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
if (openFileDialog1.ShowDialog() == DialogResult.OK)
{
  String sConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;" +
  "Data Source=" +openFileDialog1.FileName+ ";" +
  "Extended Properties='Excel 8.0;HDR=Yes;IMEX=2'" ;
  //實例化一個Oledbconnection類(實現了IDisposable,要using)
  using (OleDbConnection ole_conn = new OleDbConnection(sConnectionString))
  {
   ole_conn.Open();
   using (OleDbCommand ole_cmd = ole_conn.CreateCommand())
   {
   DataTable tb = ole_conn.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, null );
   foreach (DataRow row in tb.Rows)
   {
    MessageBox.Show(row[ "TABLE_NAME" ].ToString());
   }
   }
  }
}

3.寫入數據到Excel表

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
if (openFileDialog1.ShowDialog() == DialogResult.OK)
{
  String sConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;" +
  "Data Source=" +openFileDialog1.FileName+ ";" +
  "Extended Properties=Excel 8.0;" ;
  //實例化一個Oledbconnection類(實現了IDisposable,要using)
  using (OleDbConnection ole_conn = new OleDbConnection(sConnectionString))
  {
   ole_conn.Open();
   using (OleDbCommand ole_cmd = ole_conn.CreateCommand())
   {
   ole_cmd.CommandText = "insert into [Sheet1$](商戶ID,商家名稱)values('DJ001','點擊科技')" ;
   ole_cmd.ExecuteNonQuery();
   MessageBox.Show( "數據插入成功......" );
   }
  }
}

4.建立Excel文件並寫入數據

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
String sConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;" +
   "Data Source=d:\\excel1.xls;" +
   "Extended Properties=Excel 8.0;" ;
  //實例化一個Oledbconnection類(實現了IDisposable,要using)
  using (OleDbConnection ole_conn = new OleDbConnection(sConnectionString))
  {
   ole_conn.Open();
   using (OleDbCommand ole_cmd = ole_conn.CreateCommand())
   {
   ole_cmd.CommandText = "CREATE TABLE CustomerInfo ([CustomerID] VarChar,[Customer] VarChar)" ;
   ole_cmd.ExecuteNonQuery();
   ole_cmd.CommandText = "insert into CustomerInfo(CustomerID,Customer)values('DJ001','點擊科技')" ;
   ole_cmd.ExecuteNonQuery();
   MessageBox.Show( "生成Excel文件成功並寫入一條數據......" );
   }
}

但願本文所述對你們的C#程序設計有所幫助。

相關文章
相關標籤/搜索