1

我知道这个问题已经很老了,但我没有找到任何答案。

如何将值附加到现有的 excel 文件和特定列中。

System.Data.OleDb.OleDbConnection MyConnection;
System.Data.OleDb.OleDbCommand myCommand = new System.Data.OleDb.OleDbCommand();
string sql = null;

MyConnection = new System.Data.OleDb.OleDbConnection("provider=Microsoft.Jet.OLEDB.4.0;Data Source=filelocation;Extended Properties=Excel 8.0;");

MyConnection.Open();
myCommand.Connection = MyConnection;

sql = "Insert into ["+ sheetname +"$] (Result) values ("+ result +")";
myCommand.CommandText = sql;
myCommand.ExecuteNonQuery();
MyConnection.Close();

我什至尝试了不同的方法,例如通过 C# 打开 Excel 并追加,但没有任何效果。

4

3 回答 3

2

如果你不用OLEDB 这里有一篇好文章如何使用早期绑定操作excel文件。

于 2013-02-08T11:46:48.360 回答
0

使用 OLEDB 处理 excel 文件有它的问题和缺点。最好使用第三方库。我推荐CSharpJExcel用于 Excel 97-2003 (xls) 文件或EPPlus用于 Excel 2007 (xlsx) 文件。您甚至不需要安装 Microsoft Excel。

于 2013-02-08T11:54:24.077 回答