国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > C# > 正文

C# 通過 oledb 操作Excel實例代碼

2019-10-29 21:10:03
字體:
供稿:網(wǎng)友

整理文檔,搜刮出一個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('=');        sb.Append(prop.Value);        sb.Append(';');      }       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,'AAAA','2014-01-01');";        cmd.ExecuteNonQuery();         cmd.CommandText = "INSERT INTO [table1](id,name,datecol) VALUES(2, 'BBBB','2014-01-03');";        cmd.ExecuteNonQuery();         cmd.CommandText = "INSERT INTO [table1](id,name,datecol) VALUES(3, 'CCCC','2014-01-03');";        cmd.ExecuteNonQuery();         cmd.CommandText = "UPDATE [table1] SET name = 'DDDD' 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;    }

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持VEVB武林網(wǎng)。


注:相關(guān)教程知識閱讀請移步到c#教程頻道。
發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表
主站蜘蛛池模板: 安庆市| 棋牌| 克什克腾旗| 罗定市| 瑞丽市| 鄄城县| 健康| 乌兰察布市| 科尔| 博客| 秦皇岛市| 辰溪县| 灵武市| 大石桥市| 灵武市| 盐池县| 黔江区| 茌平县| 河北区| 邵阳县| 凌海市| 奉化市| 习水县| 油尖旺区| 阳新县| 西充县| 桂东县| 大同县| 泰顺县| 安乡县| 宜黄县| 永州市| 内丘县| 敦煌市| 雅江县| 邳州市| 徐闻县| 册亨县| 鄂伦春自治旗| 开封市| 津市市|