Click to See Complete Forum and Search --> : excel & oledb insert


newOne2336
August 26th, 2009, 06:37 AM
I'v write the following code to insert into excel 2007 file, but excel doesn't open *.xlsx file....
after that i change file extension to *.xls ...exel opens file...but warn about "Number stored as text" for all my data....


OleDbConnection baglanti = new OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + path + ";Excel 12.0;HDR=YES;");

baglanti.Open();

string createTableScript = "CREATE TABLE newTable(a1 INT,a2 INT,a3 INT)";

OleDbCommand cmd = new OleDbCommand(createTableScript, baglanti);

cmd.ExecuteNonQuery();
.
.
.

string insertScript = "insert into [newTable$] (a1,a2,a3) values (" + (i + 1).ToString() + "," + (j + 1).ToString() + "," + ((int)p).ToString() + ")";

cmd = new OleDbCommand(insertScript, baglanti);
cmd.ExecuteNonQuery();

newOne2336
August 26th, 2009, 06:58 AM
excume me

i'v resolved problem with xlsx file...with this connection string

baglanti = new OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + path + ";Extended Properties=\"Excel 12.0 xml;HDR=YES;\"");

but warn about "Number stored as text" still persist...

darwen
August 26th, 2009, 03:05 PM
Try using parameterized queries (http://www.4guysfromrolla.com/webtech/092601-1.shtml).

Darwen.

newOne2336
August 26th, 2009, 04:35 PM
I'v tried that but same problem exist...

I found that setting IMEX=1 in connection string will resolved this problem....but this leads to

"file doesnot exist" error...


is there a way to create a new excel file by oledb when setting IMEX=1?

i tried creating excel file using ExcelApplication...but this leads to incompatibility with above code
and doesnt work too...