1个回答
展开全部
通过Ado.net组件用OLEDB来操作数据源就行了。
给你个操作例子,增删改查,除了删空逗仔除操作无法进行其他的和操作数据库差不多。删除功能可以考将单元格内容置空来实现。
//Select
string path = @"D:\excel\test.xls";
String sConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data
Source=" + path + ";Extended Properties='Excel 8.0;HDR=Yes;'";
OleDbConnection objConn = new
OleDbConnection(sConnectionString);
objConn.Open();
OleDbCommand objCmdSelect = new OleDbCommand("SELECT ae,be FROM
[abc$]", objConn);
OleDbDataReader dr = objCmdSelect.ExecuteReader();
while (dr.Read())
{
Console.WriteLine(dr.GetString(0)+ dr.GetString(1));
}
dr.Close();
objConn.Close();
//使用DataAdapter
DataSet ds = new DataSet();
OleDbDataAdapter da = new OleDbDataAdapter(objCmdSelect);
objConn.Open();
da.Fill(ds, "table1");
objConn.Close();
da.Dispose();
DataTable dt = new DataTable();
dt = ds.Tables[0];
DataRowCollection dtr = dt.Rows;
foreach (DataRow item in dtr)
{
Console.WriteLine(item["ae"]);
}
/指兄/Insert
OleDbCommand cmd = new OleDbCommand("insert into [abc$](ae,be)
values('ah','bh')", objConn);
objConn.Open();
cmd.ExecuteNonQuery();
objConn.Close();
//Update
OleDbCommand cmd2 = new OleDbCommand("斗汪UPDATE [abc$] set ae='amm'
where be='bm'", objConn);
objConn.Open();
cmd2.ExecuteNonQuery();
objConn.Close();
//Create Table
OleDbCommand cmd3 = new OleDbCommand("CREATE TABLE xyz (F1
char(255), F2 char(255))", objConn);
objConn.Open();
cmd3.ExecuteNonQuery();
objConn.Close();
给你个操作例子,增删改查,除了删空逗仔除操作无法进行其他的和操作数据库差不多。删除功能可以考将单元格内容置空来实现。
//Select
string path = @"D:\excel\test.xls";
String sConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data
Source=" + path + ";Extended Properties='Excel 8.0;HDR=Yes;'";
OleDbConnection objConn = new
OleDbConnection(sConnectionString);
objConn.Open();
OleDbCommand objCmdSelect = new OleDbCommand("SELECT ae,be FROM
[abc$]", objConn);
OleDbDataReader dr = objCmdSelect.ExecuteReader();
while (dr.Read())
{
Console.WriteLine(dr.GetString(0)+ dr.GetString(1));
}
dr.Close();
objConn.Close();
//使用DataAdapter
DataSet ds = new DataSet();
OleDbDataAdapter da = new OleDbDataAdapter(objCmdSelect);
objConn.Open();
da.Fill(ds, "table1");
objConn.Close();
da.Dispose();
DataTable dt = new DataTable();
dt = ds.Tables[0];
DataRowCollection dtr = dt.Rows;
foreach (DataRow item in dtr)
{
Console.WriteLine(item["ae"]);
}
/指兄/Insert
OleDbCommand cmd = new OleDbCommand("insert into [abc$](ae,be)
values('ah','bh')", objConn);
objConn.Open();
cmd.ExecuteNonQuery();
objConn.Close();
//Update
OleDbCommand cmd2 = new OleDbCommand("斗汪UPDATE [abc$] set ae='amm'
where be='bm'", objConn);
objConn.Open();
cmd2.ExecuteNonQuery();
objConn.Close();
//Create Table
OleDbCommand cmd3 = new OleDbCommand("CREATE TABLE xyz (F1
char(255), F2 char(255))", objConn);
objConn.Open();
cmd3.ExecuteNonQuery();
objConn.Close();
追问
不懂啊,不知道代码往哪里加。我就是要把textbox1-11每次产生的数据添加到excel中
追答
你可以把这个保存为一个工具类。把上面的每一段操作构建成成员函数。
然后在你项目中的Button Click事件里创建该类对象然后调用insert或者updata函数就行了啊。
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询