Initial space in sheet name issue while importing sheet through OleDb objects

1k views Asked by At

I am using OleBb objects to read excel file into data table

System.Data.OleDb.OleDbConnection myCon = new System.Data.OleDb.OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;data source='" + FileName + "';Extended Properties=\"Excel 12.0;HDR=YES;IMEX=1\" ");
System.Data.OleDb.OleDbDataAdapter cImport = new system.Data.OleDb.OleDbDataAdapter("select * from [ Gravity$A3:ZZ]", myCon );
cImport.Fill(dt)

Executing above query shows Error :

Invalid bracketing of name ' Gravity$A3:ZZ'.

I think this error is due to one space at starting of sheet name " Gravity".

If I remove this space in query & write like "select * from [Gravity$A3:ZZ]", then it shows invalid object name.

How to deal with this issue only using OleDb objects?

2

There are 2 answers

0
faby On

try putting the name between quotes

System.Data.OleDb.OleDbDataAdapter cImport = new system.Data.OleDb.OleDbDataAdapter("select * from [\" Gravity$A3:ZZ\"]", myCon );

Second: as @PanagiotisKanavos says

Α3:ZZ doesn't seem like a valid range

0
Mehrad On

Or even try

select * from [' Gravity$A3:ZZ']