C#创建、读取和修改Excel的方法
程序员文章站
2023-11-24 18:40:04
本文实例讲述了c#创建、读取和修改excel的方法。分享给大家供大家参考。具体如下:
windows下我们可以通过 jet ole db访问excel,就行访问数据库一样...
本文实例讲述了c#创建、读取和修改excel的方法。分享给大家供大家参考。具体如下:
windows下我们可以通过 jet ole db访问excel,就行访问数据库一样
复制代码 代码如下:
// 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);
}
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);
}
希望本文所述对大家的c#程序设计有所帮助。
上一篇: C#解码base64编码二进制数据的方法
下一篇: C#查找列表中所有重复出现元素的方法