Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting the first sheet from an Excel document regardless of sheet name with OleDb

Tags:

.net

oledb

I have users that name their sheets all sorts of crazy things, but I want to be able to get the first sheet of the Excel document regardless of what it is named.

I currently use:

OleDbDataAdapter adapter = new OleDbDataAdapter( "SELECT * FROM [sheetName$]", connString); 

How would I go about getting the first sheet no matter what it is named?

Thank you.

like image 644
naspinski Avatar asked Sep 17 '09 10:09

naspinski


People also ask

How do I automatically extract data from one Excel sheet to another?

Using the + symbol in Excel Start by selecting the target cell (in our case B1 of Sheet 2) and typing in the + symbol. Next, right-click on the Sheet 1 label button to go back to your data. Select cell A1 and then press Enter. Your data will be automatically copied into cell B1.


1 Answers

ended up using this:

using (OleDbConnection conn = new OleDbConnection(connString)) {     conn.Open();     dtSchema = conn.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, new object[] { null, null, null, "TABLE" });     Sheet1= dtSchema.Rows[0].Field<string>("TABLE_NAME"); } 
like image 171
naspinski Avatar answered Sep 22 '22 14:09

naspinski