Link to home
Create AccountLog in
Avatar of JS List
JS ListFlag 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
Avatar of Big Monty
Big Monty
Flag of United States of America image

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
Avatar of 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.
what do you mean by "didnt work"? any errors? results weren't what you expected?
ASKER CERTIFIED SOLUTION
Avatar of JS List
JS List
Flag of United States of America image

Link to home
membership
Create an account to see this answer
Signing up is free. No credit card required.
Create Account
Avatar of JS List

ASKER

The responses didn't work.