在C#操作Excel中的数据.其实和操作数据库的方法.差不多.下面介绍我使用的方法:
1、连接Excel的字符串和连接access数据库的字符有一定的区别:
    string strCon = " Provider = Microsoft.Jet.OLEDB.4.0 ; Data Source = Book1.xls;Extended Properties=Excel 8.0";
2、连接其中的一张sheet;
       string strCom = " SELECT * FROM [Sheet1$] ";
3、要给工作薄添加新数据。
       string ind = "insert into [Sheet1$](工龄)values('11')";
其中在Excel中的字段名是以A1、B1、C1等开头的。
4 、修改数据和添加新数据类似,这里就不多说了。
5、删除数据好像不行。我操作时,系统总提示“INAM数据链表不能删除数据”,不知是我的方法不对还是不支持望大家在这点上自已实验。

以下是源代码:读出数据放到DataSet中并修改和插入数据。
string strCon = " Provider = Microsoft.Jet.OLEDB.4.0 ; Data Source = Book1.xls;Extended Properties=Excel 8.0";
   OleDbConnection myConn = new OleDbConnection(strCon);
 string strCom = " SELECT * FROM [Sheet1$] ";
myConn.Open();
OleDbDataAdapter myCommand = new OleDbDataAdapter(strCom, myConn);
DataSet myDataSet = new DataSet();myCommand.Fill(myDataSet, "[Sheet1$]");
  //string ind = "insert into [Sheet1$](工龄)values('11')";
string ind = "update [Sheet1$] set 工龄 = 12 where 工龄 = 2";
  OleDbCommand cmd = new OleDbCommand(ind, myConn);
cmd.ExecuteNonQuery();
myConn.Close();

相关文章:

  • 2021-11-03
  • 2021-11-05
  • 2022-12-23
  • 2022-02-13
  • 2022-12-23
  • 2021-10-28
  • 2021-12-27
猜你喜欢
  • 2022-12-23
  • 2021-12-27
  • 2021-09-25
  • 2022-12-23
  • 2022-01-28
  • 2021-12-27
相关资源
相似解决方案