Click here to Skip to main content
Click here to Skip to main content

Excel File Class for Reading Data

, 12 Sep 2007 CPOL
Rate this:
Please Sign up or sign in to vote.
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

'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)

Share

About the Author

geekstarexe@yahoo.com

United States United States
No Biography provided

Comments and Discussions

 
Generalappreciated [modified] Pinmemberjamescollett5-Oct-07 6:01 
GeneralRe: appreciated Pinmembergeekstarexe@yahoo.com22-Oct-07 10:16 
GeneralRe: appreciated PinmemberJoshua Billingsley19-Jun-08 10:51 
GeneralRe: appreciated PinmemberJoshua Billingsley19-Jun-08 10:55 
GeneralRe: appreciated Pinmembersongmao30-Oct-08 14:09 
James,
 
You get to create you own project and make it work. Try harder.
Example:
 
//get data from worksheet
public DataTable Get_WorkSheet(string worksheetname)
{
OleDbConnection objCon = (OleDbConnection)null;
DataTable objDT = new DataTable();
OleDbCommand objDCommand = (OleDbCommand)null;
OleDbDataAdapter objAD = new OleDbDataAdapter();
objAD.TableMappings.Add("Class", worksheetname);
String sQuery = "Select * from [" + worksheetname + "]";
try
{
//clear
if (objDT != null)
objDT.Clear();
//get the connection string
_connectionstring = this.Get_ConnectionPath();
objCon = new OleDbConnection(_connectionstring);
objCon.Open();
 
objDCommand = new OleDbCommand(sQuery, objCon);
objAD.SelectCommand = objDCommand;
objAD.Fill(objDT);
 
return objDT;
}
catch (Exception ex)
{
//Console.WriteLine("Message: " + ex.Message);
throw (ex);
}
finally
{
objCon.Close();
objDCommand.Dispose();
objAD.Dispose();
}
}
 
//update data into worksheet
public void Update_Data_WorkSheet(string worksheetname, string _Set_Query, string _Where_Query)
{
OleDbConnection objCon = (OleDbConnection)null;
DataTable objDT = new DataTable();
OleDbCommand objDCommand = (OleDbCommand)null;
OleDbDataReader objDR;
//form the query string to do the updates
String sQuery = "Update [" + worksheetname + "] SET" + _Set_Query + "WHERE" + _Where_Query;
try
{
//clear
if (objDT != null)
objDT.Clear();
 
//get the connection string
_connectionstring = this.Get_ConnectionPath();
objCon = new OleDbConnection(_connectionstring);
objCon.Open();
objDCommand = new OleDbCommand(sQuery, objCon);
objDR = objDCommand.ExecuteReader();
}
catch (Exception ex)
{
//Console.WriteLine("Message: " + ex.Message);
throw (ex);
}
finally
{
objCon.Close();
objDCommand.Dispose();
}
}
 
Keith Dead | X|
www.fortunecollection.com
GeneralSo useful Pinmemberrajantawate1(http//www.jhatak.com)12-Sep-07 7:55 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.141030.1 | Last Updated 12 Sep 2007
Article Copyright 2007 by geekstarexe@yahoo.com
Everything else Copyright © CodeProject, 1999-2014
Layout: fixed | fluid