r p

r p

  • NA
  • 343
  • 0

How to Insert numbers into Excel using OLEDB and C#

Oct 20 2011 5:30 AM
I am inserting values into excel document using OLEDB in my website written in ASP.NET(C#).
When I insert numbers, they are inserted as strings instead of numbers. 
How do I convert string to numbers programmically and insert them into excel as number NOT strings?
My code:-
========

string connString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + MYExcelFile + ";Extended Properties=Excel 8.0";
OleDbConnection MyConnection;
OleDbCommand myCommand = new System.Data.OleDb.OleDbCommand();
string sql = null;
MyConnection = new OleDbConnection(connString);
MyConnection.Open();
myCommand.Connection = MyConnection;

string tempTitle1 = "Title1";
string tempTitleField1 = "[" + tempTitle1 + "]";
string tempTitle2 = "Title2";
string tempTitleField2 = "[" + tempTitle2 + "]";
string tempTitle3 = "Title3";
string tempTitleField3 = "[" + tempTitle3 + "]";

sql = "Insert into " + MyRangeName + " (" + tempTitleField1 + "," +
tempTitleField2 + "," + tempTitleField3 + ") values('" + Value1 +
"','" + Value2 + "','" + Value3 + "')";
myCommand.CommandText = sql;
myCommand.ExecuteNonQuery();
MyConnection.Close();

Thanks so much,

Answers (5)