I have this command in C#:
OleDbCommand cmd = new OleDbCommand("select * from[Sheet1$]", con);
And its working fine when sheet is called Sheet1, but in my case sheet has different name. How can I accomplish that ?
I already tried something like this:
string sheetName = "First sheet";
OleDbCommand cmd = new OleDbCommand("select * from["+sheetName+"$]", con);
You have to erase whitespace from sheet name, in example change:
string sheetName = "First sheet";
to
string sheetName = "FirstSheet";
You are not allowed to do whitespaces in SQL-based table's name.
If you are having issues with getting the sheet names right, you can try GetOleDbSchemaTable()
to get the schema and pass this value to your OleDbCommand
. Note that this works only if you now the positioning of your worksheet in the excel.
Hope this helps!
objConn = new OleDbConnection(constring);
objConn.Open();
// Get the data table containg the schema guid.
var dt = objConn.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, null);
var query = "select * from [" + dt.Rows[0]["TABLE_NAME"].ToString() + "]";
DataSet ds = new DataSet();
OleDbConnection con = new OleDbConnection(constring + "");
OleDbDataAdapter da = new OleDbDataAdapter(sqlquery, con);
da.Fill(ds);
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.