Vincent
Vincent

Reputation: 524

How to append values to an Excel file?

I know this question is very old but i am not finding any answer to this.

How to append values into existing excel file and to a particular column.

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();

I even tried different method like opening Excel through C# and append, nothing worked.

Upvotes: 1

Views: 2200

Answers (3)

Moha Dehghan
Moha Dehghan

Reputation: 18443

Using OLEDB to work with excel files has it's problems and shortcomings. It is better to use a third-party library. I recomment CSharpJExcel for Excel 97-2003 (xls) files or EPPlus for Excel 2007 (xlsx) files. You don't need to even have Microsoft Excel installed.

Upvotes: 0

Andrey Gordeev
Andrey Gordeev

Reputation: 32459

If you don't have to use OLEDB here is a good article how to operate excel files by using early binding.

Upvotes: 2

Related Questions