Ryan McDonnell - Pursuing Web Application Zen

Reading data from an Excel spreadsheet with unknown sheet names

Friday, June 25th, 2004 at 12:00 am

Reading data from an Excel spreadsheet is pretty easy, given you know the name of the sheet in the file you are reading data from. What if you don’t know the name of the sheet? A current project I’m working on needs to give the user the ability to select the sheet name to read data from.  Here’s a small snippet of code that reads the names of the sheets and populates a DropDownList control.

Dim cnExcel As OleDbConnection = New OleDbConnection( _
    “Provider=Microsoft.Jet.OLEDB.4.0;” &_
    “Data Source=” & strWorkbookFileName & “;” & _
    “Extended Properties=”"Excel 8.0;HDR=Yes”"”)
cnExcel.Open()
Dim dtSheets As DataTable = cnExcel.GetOleDbSchemaTable( _
    OleDbSchemaGuid.Tables, _
    New Object() {Nothing, Nothing, Nothing, “Table”})
Dim i As Integer
Dim strWorksheetName As String
For i = 0 To dtSheets.Rows.Count - 1
    strWorksheetName = dtSheets.Rows(i)(2)
    dropWorksheetName.Items.Add(New ListItem(strWorksheetName, strWorksheetName))
Next
cnExcel.Close()

Comments are closed.

About Ryan McDonnell

I am a web application developer living in southern California. Commonly called a “jack of all trades” by collegues, I constantly strive to broaden my knowledge into other fields.
More about Ryan McDonnell »


 Subscribe in a reader

Quick Links & Notes

© 2004-2010 Ryan McDonnell. View my profile on LinkedIn
Some rights reserved under the Creative Commons Attribution-Share Alike 3.0 United States License.