Avatar of JS List
JS List
Flag for United States of America asked on

Import Excel Data Using Generic Sheet Name

Hello,
I'm importing data from an excel spreadsheet into an ASP.NET web page.  The problem is the sheet name keeps changing.  It's always the first sheet in the excel book - but the users change the name.

Is there a generic name for the first sheet in Excel.   So if the user names the sheet to "employees" - when I make a connection can I use something like [Sheet(0)]?

Using ASP.NET 4.0 and xlsx file.

Here's my typical connection:  (Source name not complete path here.)
In Web.config
 <add name="csXLMembers" connectionString="Provider=Microsoft.ACE.OLEDB.12.0;Data Source=...\Members.xlsx;Extended Properties=Excel 12.0"/>

Code Behind aspx Page
 Dim conString As String = ConfigurationManager.ConnectionStrings("csXLMembers").ConnectionString

****  The following line is where I want to use a generic name instead of Sheet1$
 Dim cmd As OleDbCommand = New OleDbCommand("SELECT * FROM [Sheet1$]", con)

JS
Web Applications

Avatar of undefined
Last Comment
JS List

8/22/2022 - Mon
Big Monty

you could use the following code to obtain the sheet name:

string sheetName = dtExcelSchema.Rows[0]["TABLE_NAME"];

source: http://aspsnippets.com/Articles/Read-and-Import-Excel-Sheet-using-ADO.Net-and-C.aspx
JS List

ASKER
This didn't work.  I see many references on the web to do it this way.  But I couldn't get it to work.
Big Monty

what do you mean by "didnt work"? any errors? results weren't what you expected?
Experts Exchange is like having an extremely knowledgeable team sitting and waiting for your call. Couldn't do my job half as well as I do without it!
James Murphy
ASKER CERTIFIED SOLUTION
JS List

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
JS List

ASKER
The responses didn't work.