自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

C#讀取Excel中數(shù)據(jù)并插入到數(shù)據(jù)庫中

開發(fā) 后端
這里將介紹如何利用C#讀取Excel中數(shù)據(jù)并插入到數(shù)據(jù)庫中,具體步驟是上傳選擇的xls文件,根據(jù)得到路徑,讀取文件信息,插入到數(shù)據(jù)庫中。

C#讀取Excel中數(shù)據(jù),首先上傳選擇的xls文件,根據(jù)得到路徑,讀取文件信息,插入到數(shù)據(jù)庫中。 

  1. using System.Data;     
  2. using System.Data.OleDb;     
  3. using System.IO;     
  4. using System.Text; protected void Page_Load(object sender, EventArgs e)     
  5.  {               
  6.  }     
  7.  public void ExcelToDS(string path)     
  8.     {      
  9.         string strSheetName = "sheet1";     
  10.         string strConn = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" +path + ";Extended Properties='Excel 8.0;HDR=Yes;IMEX=1;'";     
  11.         //Sql語句      
  12.         string strExcel = "select * from  [" + strSheetName + "$] ";               
  13.         DataSet ds = new DataSet();     
  14.        //連接數(shù)據(jù)源      
  15.         OleDbConnection conn = new OleDbConnection(strConn);      
  16.         conn.Open();     
  17.     
  18.         //適配到數(shù)據(jù)源      
  19.         OleDbDataAdapter adapter = new OleDbDataAdapter(strExcel, conn);     
  20.         adapter.Fill(ds, "data");     
  21.     
  22.         conn.Close();     
  23.     
  24.         GridView1.DataSource = ds.Tables["data"];     
  25.         GridView1.DataBind();       
  26.         Label1.Text = ds.Tables[0].Rows.Count.ToString();     
  27.         SqlAccess.Sqldatabase sql = new SqlAccess.Sqldatabase();     
  28.         StringBuilder sb = new StringBuilder();     
  29.         for (int i = 0; i < ds.Tables[0].Rows.Count; i++)     
  30.         {     
  31.     
  32.             sb.Append("insert into data values('" + ds.Tables[0].Rows[i]["companyname"] + "','" + ds.Tables[0].Rows[i]["telname"] + "','" + ds.Tables[0].Rows[i]["qytel"] + "','" + ds.Tables[0].Rows[i]["fax"] + "');");     
  33.     
  34.         }     
  35.         sql.ExecuteNonQuery(CommandType.Text, sb.ToString());     
  36.     
  37.     }     
  38.     protected void Button1_Click(object sender, EventArgs e)     
  39.     {     
  40.         string fileName = null;     
  41.         try    
  42.         {     
  43.             Boolean fileOK = false;     
  44.             String path = Server.MapPath("./doc/");     
  45.             if (FileUpload2.HasFile)     
  46.             {     
  47.                 String fileExtension =     
  48.                     System.IO.Path.GetExtension(FileUpload2.FileName).ToLower();     
  49.                 String[] allowedExtensions =      
  50.                 { ".xls" };     //C#讀取Excel中數(shù)據(jù)
  51.                 for (int i = 0; i < allowedExtensions.Length; i++)     
  52.                 {     
  53.                     if (fileExtension == allowedExtensions[i])     
  54.                     {     
  55.                         fileOK = true;     
  56.                     }     
  57.                 }     
  58.             }     
  59.     
  60.             if (fileOK)     
  61.             {     
  62.                fileName = "r_" + DateTime.Now.ToString("yyyy_MM_dd_hh_mm_ss") + "_" + DateTime.Now.Millisecond +     
  63.                     System.IO.Path.GetExtension(FileUpload2.FileName).ToLower();     
  64.                 if (File.Exists(path + fileName))     
  65.                 {     
  66.                     Random rnd = new Random(10000);     
  67.                     fileName = fileName + rnd.Next();     
  68.                 }     
  69.     
  70.                 FileUpload2.PostedFile.SaveAs(path     
  71.                     + fileName);     
  72.                    
  73.     
  74.             }     
  75.             else    
  76.             {     
  77.                      
  78.             }     
  79.         }     
  80.         catch (Exception exp)     
  81.         {     
  82.         }     
  83.         ExcelToDS(Server.MapPath(".") + "\\doc\\"+fileName);     
  84.     
  85.     }   

C#讀取Excel中數(shù)據(jù)并插入到數(shù)據(jù)庫中就介紹到這里。

【編輯推薦】

  1. C#數(shù)據(jù)庫查詢和操作大全
  2. 淺析C#擴(kuò)展方法
  3. C# Singleton的使用及優(yōu)缺點(diǎn)探討
  4. 詳細(xì)分析C#數(shù)據(jù)庫連接池
  5. C#數(shù)據(jù)庫備份及還原的實(shí)現(xiàn)代碼
責(zé)任編輯:彭凡 來源: javaeye
相關(guān)推薦

2009-08-12 16:39:50

C#向Excel插入數(shù)

2011-03-17 15:59:37

c#數(shù)據(jù)庫

2024-09-20 18:02:42

C#數(shù)據(jù)庫SQLite

2010-08-26 16:34:12

DB2Excel讀取

2009-08-13 10:27:28

C#讀取Excel數(shù)據(jù)

2009-08-11 15:22:57

C#讀Excel

2011-08-01 16:10:11

XCode Excel 數(shù)據(jù)庫

2009-08-07 15:33:11

C#數(shù)據(jù)庫編程

2009-08-13 16:44:45

C#數(shù)據(jù)轉(zhuǎn)換

2009-03-19 10:08:09

C#數(shù)據(jù)庫查詢

2009-08-11 13:35:13

C# Berkeley

2024-04-18 09:56:16

2009-08-11 14:32:06

C#讀取Excel數(shù)據(jù)

2010-10-26 15:21:11

連接Oracle數(shù)據(jù)庫

2009-09-10 10:59:47

C# form

2009-07-20 17:03:55

批量插入數(shù)據(jù)ASP.NET

2009-09-04 17:53:23

C#增加數(shù)據(jù)庫數(shù)據(jù)

2009-08-25 12:50:32

數(shù)據(jù)庫常用C#代碼

2009-08-07 16:19:00

C#下數(shù)據(jù)庫編程

2009-08-12 14:27:36

訪問MySQL數(shù)據(jù)庫C# ODBC
點(diǎn)贊
收藏

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