C#操作Excel(匯入匯出)

來源:互聯網
上載者:User
view plaincopy to clipboardprint?
  1. class ExampleOfLegalsoft   
  2. {   
  3.     /// <summary>    
  4.     /// 讀取Excel文檔    
  5.     /// </summary>    
  6.     /// <param name="Path">檔案名稱</param>    
  7.     /// <returns>返回一個資料集</returns>    
  8.     public DataSet ExcelToDS(string Path)   
  9.     {   
  10.         string strConn = "Provider=Microsoft.Jet.OLEDB.4.0;" + "Data Source=" + Path + ";" + "Extended Properties=Excel 8.0;";   
  11.         OleDbConnection conn = new OleDbConnection(strConn);   
  12.         conn.Open();   
  13.         string strExcel = "";   
  14.         OleDbDataAdapter myCommand = null;   
  15.         DataSet ds = null;   
  16.         strExcel = "select * from [sheet1$]";   
  17.         myCommand = new OleDbDataAdapter(strExcel, strConn);   
  18.         ds = new DataSet();   
  19.         myCommand.Fill(ds, "table1");   
  20.         return ds;   
  21.     }   
  22.     /// <summary>    
  23.     /// 寫入Excel文檔    
  24.     /// </summary>    
  25.     /// <param name="Path">檔案名稱</param>    
  26.     public bool SaveFP2toExcel(string Path)   
  27.     {   
  28.         try  
  29.         {   
  30.             string strConn = "Provider=Microsoft.Jet.OLEDB.4.0;" + "Data Source=" + Path + ";" + "Extended Properties=Excel 8.0;";   
  31.             OleDbConnection conn = new OleDbConnection(strConn);   
  32.             conn.Open();   
  33.             System.Data.OleDb.OleDbCommand cmd = new OleDbCommand();   
  34.             cmd.Connection = conn;   
  35.             //cmd.CommandText ="UPDATE [sheet1$] SET 姓名="2005-01-01" WHERE 工號="日期"";    
  36.             //cmd.ExecuteNonQuery ();    
  37.             for (int i = 0; i < fp2.Sheets[0].RowCount - 1; i++)   
  38.             {   
  39.                 if (fp2.Sheets[0].Cells[i, 0].Text != "")   
  40.                 {   
  41.                     cmd.CommandText = "INSERT INTO [sheet1$] (工號,姓名,部門,職務,日期,時間) VALUES(" + fp2.Sheets[0].Cells[i, 0].Text + "," +   
  42.                     fp2.Sheets[0].Cells[i, 1].Text + "," + fp2.Sheets[0].Cells[i, 2].Text + "," + fp2.Sheets[0].Cells[i, 3].Text +   
  43.                     "," + fp2.Sheets[0].Cells[i, 4].Text + "," + fp2.Sheets[0].Cells[i, 5].Text + ")";   
  44.                     cmd.ExecuteNonQuery();   
  45.                 }   
  46.             }   
  47.             conn.Close();   
  48.             return true;   
  49.         }   
  50.         catch (System.Data.OleDb.OleDbException ex)   
  51.         {   
  52.             System.Diagnostics.Debug.WriteLine("寫入Excel發生錯誤:" + ex.Message);   
  53.         }   
  54.         return false;   
  55.     }   
相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.