Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations biv343 on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Excel writing to one cell.

Status
Not open for further replies.
Jun 5, 2006
28
US
I wrote the following code (full source code at the bottom) which writes to cells A7 and B7 in Book1.xls. I am wondering how can I modify the following code so it only write to one cell.

I attempted the following
comOleDBCommand.CommandText = "UPDATE [Sheet1$A7:B7] SET F1 = 5, F2 = 'Cell G3'";
TO
comOleDBCommand.CommandText = "UPDATE [Sheet1$A7] SET F1 = 5";

but get an error. Any ideas?




OleDbConnection conOleDBConnection = new OleDbConnection();
OleDbCommand comOleDBCommand = new OleDbCommand();
conOleDBConnection.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;" + "Data Source=Book1.xls;Extended Properties=\"Excel 8.0;HDR=NO;\"";
comOleDBCommand.Connection = conOleDBConnection;
conOleDBConnection.Open();
comOleDBCommand.CommandText = "UPDATE [Sheet1$A7:B7] SET F1 = 5, F2 = 'Cell G3'";
comOleDBCommand.ExecuteNonQuery();
conOleDBConnection.Close();

PS if you are trying this code dont forget to add at the top: using System.Data.OleDb;
 
I think i figured it out it should be:
comOleDBCommand.CommandText = "UPDATE [Sheet1$A7:A7] SET F1 = 5";

the interesting part is that it doesnt work for the first row cells. Any ideas?

code:
comOleDBCommand.CommandText = "UPDATE [Sheet1$A1:A1] SET F1 = 5";
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top