Home  >  Article  >  Backend Development  >  C# operates Excel example code through oledb

C# operates Excel example code through oledb

黄舟
黄舟Original
2017-06-04 09:35:181915browse

本篇文章主要介绍了C# 通过 oledb 操作Excel实例代码,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧

整理文档,搜刮出一个C# 通过 oledb 操作Excel实例代码,稍微整理精简一下做下分享。

public string GetConnectionString()
    {
      Dictionary<string, string> props = new Dictionary<string, string>();
 
      // XLSX - Excel 2007, 2010, 2012, 2013
      props["Provider"] = "Microsoft.ACE.OLEDB.12.0;";
      props["Extended Properties"] = "Excel 12.0 XML";
      props["Data Source"] = @"C:\tools\MyExcel.xlsx";
 
      // XLS - Excel 2003 and Older
      //props["Provider"] = "Microsoft.Jet.OLEDB.4.0";
      //props["Extended Properties"] = "Excel 8.0";
      //props["Data Source"] = "C:\\MyExcel.xls";
 
      var sb = new StringBuilder();
 
      foreach (KeyValuePair<string, string> prop in props)
      {
        sb.Append(prop.Key);
        sb.Append(&#39;=&#39;);
        sb.Append(prop.Value);
        sb.Append(&#39;;&#39;);
      }
 
      return sb.ToString();
    }
 
    public void WriteExcelFile()
    {
      string connectionString = GetConnectionString();
 
      using (OleDbConnection conn = new OleDbConnection(connectionString))
      {
        conn.Open();
        OleDbCommand cmd = new OleDbCommand();
        cmd.Connection = conn;
 
        cmd.CommandText = "CREATE TABLE [table1] (id INT, name VARCHAR, datecol DATE );";
        cmd.ExecuteNonQuery();
 
        cmd.CommandText = "INSERT INTO [table1](id,name,datecol) VALUES(1,&#39;AAAA&#39;,&#39;2014-01-01&#39;);";
        cmd.ExecuteNonQuery();
 
        cmd.CommandText = "INSERT INTO [table1](id,name,datecol) VALUES(2, &#39;BBBB&#39;,&#39;2014-01-03&#39;);";
        cmd.ExecuteNonQuery();
 
        cmd.CommandText = "INSERT INTO [table1](id,name,datecol) VALUES(3, &#39;CCCC&#39;,&#39;2014-01-03&#39;);";
        cmd.ExecuteNonQuery();
 
        cmd.CommandText = "UPDATE [table1] SET name = &#39;DDDD&#39; WHERE id = 3;";
        cmd.ExecuteNonQuery();
 
        conn.Close();
      }
    }
 
    public DataSet ReadExcelFile()
    {
      DataSet ds = new DataSet();
 
      string connectionString = GetConnectionString();
 
      using (OleDbConnection conn = new OleDbConnection(connectionString))
      {
        conn.Open();
        OleDbCommand cmd = new OleDbCommand();
        cmd.Connection = conn;
 
        // Get all Sheets in Excel File
        DataTable dtSheet = conn.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, null);
 
        // Loop through all Sheets to get data
        foreach (DataRow dr in dtSheet.Rows)
        {
          string sheetName = dr["TABLE_NAME"].ToString();
 
          if (!sheetName.EndsWith("$"))
            continue;
 
          // Get all rows from the Sheet
          cmd.CommandText = "SELECT * FROM [" + sheetName + "]";
 
          DataTable dt = new DataTable();
          dt.TableName = sheetName;
 
          OleDbDataAdapter da = new OleDbDataAdapter(cmd);
          da.Fill(dt);
 
          ds.Tables.Add(dt);
        }
 
        cmd = null;
        conn.Close();
      }
 
      return ds;
    }

The above is the detailed content of C# operates Excel example code through oledb. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn