如何将值附加到Excel文件?

我知道这个问题很老,但我没有find任何答案。

如何将值追加到现有的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和追加,没有任何工作。

如果你不必使用OLEDB 这里是一个很好的文章如何使用早期绑定操作excel文件。

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