Peter

Peter

  • NA
  • 5
  • 0

Using OLEDB to write to excel file.. problems

Mar 7 2011 4:57 PM
This is really frustrating..

I have an blank excel workbook (97-2003 version) ie .xls which just has some formatting in it and two worksheets Results and Other

I am trying to write something into the cells of the worksheet Results using two methods :-

1)
string sql = null;
System.Data.OleDb.OleDbConnection MyConnection;

MyConnection = new System.Data.OleDb.OleDbConnection(@"provider=Microsoft.Jet.OLEDB.4.0;Data Source='c:\\test\\ResultsTest.xls'; Extended Properties='Excel 8.0;HDR=No;'");

MyConnection.Open();

System.Data.OleDb.OleDbDataAdapter MyAdapter = new System.Data.OleDb.OleDbDataAdapter("select * from [Results$]", MyConnection); 

System.Data.DataSet DS = new DataSet(); 
MyAdapter.Fill(DS); 
MyConnection.Close(); 

MyAdapter.InsertCommand = new System.Data.OleDb.OleDbCommand("insert into [Results$] (F1,F2) Values ('test','peter'))", MyConnection); 
MyAdapter.Update(DS);


and 

2)
            sql = " insert into [Results$] (F1,F2) Values ('test','peter') ";
            System.Data.OleDb.OleDbCommand cmd = new System.Data.OleDb.OleDbCommand(sql, MyConnection);
            cmd.ExecuteNonQuery();
            cmd.Dispose();
            MyConnection.Close();


Both these do not error but when I open up the spreadsheet nothing has been saved..


What am I doing wrong !!

thanks


Answers (1)