excel read file

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

I am trying to read an excel file. Here is my code
string Conn ="Provider=Microsoft.Jet.OLEDB.4.0;Data Source =
G:\\\\NA\\Stamford\\Technology\\Information
Security\\DATA_ENTITLEMENTS!\\G_Drive_Entitlements_STM_2005.xls;" +
"Exteneded Properties=Excel 8.0;";
OleDbConnection dbConn = new OleDbConnection(Conn);
dbConn.Open ();
OleDbCommand Cmd = new OleDbCommand("Select * From [Sheet1$]", dbConn);
OleDbDataAdapter Da = new OleDbDataAdapter();
Da.SelectCommand = Cmd;
DataSet Ds = new DataSet();
Da.Fill(Ds);
dg.DataSource = Ds.Tables[0];
dbConn.Close ();

I get the error here: OleDbCommand Cmd = new OleDbCommand("Select * From
[Sheet1$]", dbConn);
Please help
 
Freddy,

I believe that the only thing wrong with your code is you have misspelled
the word extended. If you replace

"Exteneded Properties=Excel 8.0;";

with

"Extended Properties=Excel 8.0;";

It should work fine. One other thing...To avoid all of those escape
sequences you can use verbatim strings instead. Try @"C:\temp\test.xls"
instead of "C:\\temp\\test.xls".

Hope this helps.
 
Back
Top