Click here to Skip to main content
Licence 
First Posted 8 Dec 2005
Views 107,313
Bookmarked 55 times

Client For Microsoft Excel in C#

By | 8 Dec 2005 | Article
MicrosoftExcelClient is a small assembly coded in C#.NET which is used to interface with, i.e., read from and write into an Excel sheet in the .NET Framework.
 
Part of The SQL Zone sponsored by
See Also

Sample Image - MicrosoftExcelClient.jpg

Introduction

In the following article, a brief description has been provided for a client which is used to interface with Microsoft Excel documents. Interfacing to Excel documents (reading, inserting, updating etc.) is a basic task under ADO.NET, and every developer must have a sense of interfacing to Excel documents. The MicrosoftExcelCient is a simple class any developer can use in their code to interface with Excel documents. It has been created in C# and uses OLEDB.

Inside MicrosoftExcelClient

The MicrosoftExcelClient has been created in C#, and in essence is a class which provides an easy to use simple interface to access Excel workbooks in the .NET Framework. OLEDB components are used within C# to access Excel sheets like databases.

/// <summary>
/// Connects to the source excel workbook
/// </summary>

OleDbConnection m_ConnectionToExcelBook;

/// <summary>
/// Reads the data from the document to a System.Data object
/// </summary>

OleDbDataAdapter m_AdapterForExcelBook;

Shown below are code snippets from some of the basic functions used in the MicrosoftExcelClient assembly...

Open Connection To An Excel Workbook

this.m_ConnectionToExcelBook = 
    new OleDbConnection("Provider=Microsoft.Jet" + 
    ".OLEDB.4.0;Data Source=" + 
    this.m_SourceFileName + 
    ";Extended Properties=Excel 8.0;");

this.m_ConnectionToExcelBook.Open();

Read Data From An Excel Sheet

DataTable returnDataObject = new DataTable();
OleDbCommand selectCommand = 
   new OleDbCommand("select * from [" + iSheetName + "$]");

selectCommand.Connection = this.m_ConnectionToExcelBook;
this.m_AdapterForExcelBook = new OleDbDataAdapter();
this.m_AdapterForExcelBook.SelectCommand = selectCommand;
this.m_AdapterForExcelBook.Fill(returnDataObject);

Insert Or Update Format

OleDbCommand nonQueryCommand = new OleDbCommand(iQuery);
nonQueryCommand.Connection = this.m_ConnectionToExcelBook;
nonQueryCommand.CommandText = iQuery;
nonQueryCommand.ExecuteNonQuery();

How To Use The MicrosoftExcelClient

As mentioned previously, the provided client is quite simple to use for budding developers, and should be a mere cake walk for experienced pros. Shown below is a brief snippet of how simple it is to use this assembly to extract and feed data from / into an Excel sheet…

Sample screenshot

//Declare the sample object & set source data path
MicrosoftExcelClient sampleObject = 
     new MicrosoftExcelClient("SampleData.xls");

//Open connection to the excel work book
sampleObject.openConnection();

//Load the entire excel sheet into Datagrid1 
//( Sheet name is passed as a parameter )
this.dataGrid1.DataSource = sampleObject.readEntireSheet("sheet1");

//Load the result of a specific query on the excel sheet into Datagrid2 
//Query pased as a parameter

this.dataGrid2.DataSource = 
   sampleObject.readForSpecificQuery("select data1 , " + 
   "data2 ,data3 from [sheet1$]");

//******NON RESULT ORIENTED QUERIES*******
//Inserts a new record into the excel sheet
sampleObject.runNonQuery("insert into [sheet1$]" + 
         " values('1','2','3','4','5','6','7') ");

//Update the given excel sheet
sampleObject.runNonQuery("update [sheet1$] set data1 = '9'");

Conclusion

To sum it all up, the MicrosoftExcelClient is very simple to use, and is open source and not copyrighted. You may use it in parts or as a whole without any restrictions. The demo project is bug free, and any and all suggestions are welcome. You may face problems deleteing records from an Excel database when using the NonQuery function, however this is a drawback of OLEDB, and is not a mistake in the code.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

About the Author

Siddhartha Batra

Web Developer

India India

Member

Siddharth Batra is a first year graduate student, pursuing his Master's of Science in Computer Science at Stanford University. His research interests are in the domains of computer vision, digital image processing, visualizations and applied computer graphics.
 
He also posseses a keen interest in the .NET Framework and the C# language
 
You can contact Siddharth Batra at
 
http://www.siddharthbatra.info/

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
GeneralMy vote of 1 Pinmemberdebbie616:09 3 Apr '12  
GeneralMy vote of 5 Pinmembermanoj kumar choubey2:56 7 Feb '12  
QuestionAlternative solution PinmemberJamesHoward9725:57 28 Dec '11  
GeneralERROR: the first row don't display! Pinmemberwadeliu20:26 6 Oct '08  
GeneralRe: ERROR: the first row don't display! PinmemberDeathspike2:22 22 Oct '08  
QuestionExcel Probelm Pinmemberghumman6318:42 8 May '08  
AnswerRe: Excel Probelm Pinmembergg42371:06 13 Nov '09  
QuestionCan I read Opened Excel file continuosly? PinmemberMember 45858047:47 14 Jan '08  
GeneralProblem with create table. PinmemberTomasz_S2:51 14 Jan '08  
QuestionExtended Functions Pinmembernew devy9:25 4 Nov '07  
AnswerRe: Extended Functions PinmemberSiddhartha Batra9:48 4 Nov '07  
QuestionRe: Extended Functions Pinmembernew devy8:23 6 Nov '07  
GeneralLong Strings Pinmemberstephen6662:41 30 Nov '06  
GeneralDates Pinmemberstephen6660:20 24 Nov '06  
GeneralRe: Dates PinmemberSiddhartha Batra0:25 24 Nov '06  
GeneralRe: Dates Pinmemberstephen6660:45 24 Nov '06  
GeneralRe: Dates Pinmemberstephen6662:38 30 Nov '06  
GeneralRe: Dates PinmemberShailendrasinh10:47 14 Nov '07  
QuestionUpdate Fails PinmemberGeorge.Will9:33 30 Oct '06  
AnswerRe: Update Fails Pinmemberricpue10:09 2 Nov '06  
GeneralI CAN NOT download source Pinmemberlbustio3:03 30 Oct '06  
GeneralRe: I CAN NOT download source PinmemberSiddhartha Batra6:34 30 Oct '06  
GeneralThank you for sharing! PinmemberDavid7771:59 6 Sep '06  
QuestionDeleting rows PinmemberCezar Gradinariu2:03 4 Apr '06  
GeneralRe: Deleting rows PinmemberSiddhartha Batra2:06 4 Apr '06  

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.

Permalink | Advertise | Privacy | Mobile
Web04 | 2.5.120517.1 | Last Updated 8 Dec 2005
Article Copyright 2005 by Siddhartha Batra
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid