How to know the first sheet name of an Excel

A

ad

I use the codes below to import the first sheet of an Excel file into a
DataSet.
But if the name of the first sheet is not "Sheet1", the SelectCommand will
fail.
How can I know the sheet name of the first Sheet in an Excel file?


string sConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;" + "Data
Source=" + savePath +
"; Extended Properties=" +
(char)34 + "Excel 8.0;IMEX=1;" + (char)34;
string sSelect = "SELECT * FROM [Sheet1$]";
OleDbConnection objConn = new OleDbConnection(sConnectionString);
objConn.Open();
OleDbCommand objCmdSelect = new OleDbCommand(sSelect, objConn);
OleDbDataAdapter objAdapter1 = new OleDbDataAdapter();
objAdapter1.SelectCommand = objCmdSelect;
objAdapter1.Fill(ds, "XLData");
objConn.Close();
 
P

Paul Clement

¤ I use the codes below to import the first sheet of an Excel file into a
¤ DataSet.
¤ But if the name of the first sheet is not "Sheet1", the SelectCommand will
¤ fail.
¤ How can I know the sheet name of the first Sheet in an Excel file?
¤
¤
¤ string sConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;" + "Data
¤ Source=" + savePath +
¤ "; Extended Properties=" +
¤ (char)34 + "Excel 8.0;IMEX=1;" + (char)34;
¤ string sSelect = "SELECT * FROM [Sheet1$]";
¤ OleDbConnection objConn = new OleDbConnection(sConnectionString);
¤ objConn.Open();
¤ OleDbCommand objCmdSelect = new OleDbCommand(sSelect, objConn);
¤ OleDbDataAdapter objAdapter1 = new OleDbDataAdapter();
¤ objAdapter1.SelectCommand = objCmdSelect;
¤ objAdapter1.Fill(ds, "XLData");
¤ objConn.Close();
¤

See the following newsgroup thread:

http://tinyurl.com/fz5jq


Paul
~~~~
Microsoft MVP (Visual Basic)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top