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

首頁(yè) > 編程 > C# > 正文

C#創(chuàng)建、讀取和修改Excel的方法

2020-01-24 02:02:17
字體:
來(lái)源:轉(zhuǎn)載
供稿:網(wǎng)友

本文實(shí)例講述了C#創(chuàng)建、讀取和修改Excel的方法。分享給大家供大家參考。具體如下:

windows下我們可以通過(guò) Jet OLE DB訪問(wèn)Excel,就行訪問(wèn)數(shù)據(jù)庫(kù)一樣

復(fù)制代碼 代碼如下:
// Namespaces, Variables, and Constants
using System;
using System.Configuration;
using System.Data;
private OleDbDataAdapter da;
private DataTable dt;
private void Excel_Load(object sender, System.EventArgs e)
{
    // Create the DataAdapter.
    da = new OleDbDataAdapter("SELECT * FROM [Sheet1$]", ConfigurationSettings.AppSettings["ExcelConnectString1"]);
    // Create the insert command.
    String insertSql = "INSERT INTO [Sheet1$] (CategoryID, CategoryName, Description) VALUES (?, ?, ?)";
    da.InsertCommand = new OleDbCommand(insertSql, da.SelectCommand.Connection);
    da.InsertCommand.Parameters.Add("@CategoryID", OleDbType.Integer, 0, "CategoryID");
    da.InsertCommand.Parameters.Add("@CategoryName", OleDbType.Char, 15, "CategoryName");
    da.InsertCommand.Parameters.Add("@Description", OleDbType.VarChar, 100, "Description");
    // Create the update command.
    String updateSql = "UPDATE [Sheet1$] SET CategoryName=?, Description=? " WHERE CategoryID=?";
    da.UpdateCommand = new OleDbCommand(updateSql, da.SelectCommand.Connection);
    da.UpdateCommand.Parameters.Add("@CategoryName", OleDbType.Char, 15, "CategoryName");
    da.UpdateCommand.Parameters.Add("@Description", OleDbType.VarChar, 100, "Description");
    da.UpdateCommand.Parameters.Add("@CategoryID", OleDbType.Integer, 0, "CategoryID");
    // Fill the table from the Excel spreadsheet.
    dt = new DataTable( );
    da.Fill(dt);
    // Define the primary key.
    dt.PrimaryKey = new DataColumn[] {dt.Columns[0]};
    // Records can only be inserted using this technique.
    dt.DefaultView.AllowDelete = false;
    dt.DefaultView.AllowEdit = true;
    dt.DefaultView.AllowNew = true;
    // Bind the default view of the table to the grid.
    dataGrid.DataSource = dt.DefaultView;
}
private void updateButton_Click(object sender, System.EventArgs e)
{
    da.Update(dt);
}

希望本文所述對(duì)大家的C#程序設(shè)計(jì)有所幫助。

發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 虹口区| 化隆| 兴义市| 突泉县| 荔浦县| 铁岭市| 岐山县| 尉犁县| 江华| 嘉禾县| 普陀区| 常德市| 青铜峡市| 邢台县| 宝鸡市| 遵义市| 河南省| 明水县| 富阳市| 白水县| 舞阳县| 荔波县| 赞皇县| 凤台县| 梓潼县| 盐山县| 绥江县| 通海县| 罗平县| 台南市| 乡宁县| 临江市| 瓦房店市| 河南省| 和硕县| 黑河市| 新乡市| 项城市| 阳信县| 肥城市| 龙岩市|