Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles / Languages / VB

Excel File Class for Reading Data

3.23/5 (11 votes)
12 Sep 2007CPOL 1   417  
Excel file class for reading data.

Screenshot - ExcelFile.jpg

Introduction

This is a handy little class that I wrote to work with Excel files. I looked around on Google and I could find little snippets to read data and others to read worksheet names, but I couldn't find a reusable class that allowed me to do both. So I wrote one.

Background

Maybe it's not that exciting, but it's reusable, and I really dig OO programming anytime I can use it, I do. For the task I had to do, I really only needed to read and browse an Excel file, but it would be cool if someone else found this class and extended it so that you could load all the worksheets into one dataset, manipulate the data, and finally save the data back to the file. I actually have to get back to real work, or I would :(

Using the code

VB
'http://www.DTS-Global.com
'Author: Allen Crane Sr. Developer
'instanciate the new handy dandy new object I wrote :) 
Dim Excel As New ExcelFile(paths, strFileNameOnly)

'instanciate the new handy dandy new object I wrote :) 
Response.Write("Your workbook contains " worksheets" & _
               Excel.ExcelWorkSheets.Count & ")

'instanciate the new handy dandy new object I wrote :) 
Dim Excel As New ExcelFile(paths, strFileNameOnly)

'''''''''''''''''''''''''''''''''''''''''''''''''''''''
'load all the work sheet names in a radio button list
'''''''''''''''''''''''''''''''''''''''''''''''''''''''
'counter for loop
Dim i As Int16 = Nothing
'loop through all the transactions and add all the items
For i = 0 To Excel.ExcelWorkSheets.Count - 1
  rblMultipleSheets.Items.Add(Excel.ExcelWorkSheets.Item(i).TableName)
Next

'''''''''''''''''''''''''''''''''''''''''''''''''''''
'Bind to the data grid with the first work sheet
'''''''''''''''''''''''''''''''''''''''''''''''''''''
DataGrid1.DataSource = _
    Excel.GetWorkSheetByName(Excel.ExcelWorkSheets.Item(0).TableName)
DataGrid1.DataBind()

History

  • Version 1.0

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)