Click here to Skip to main content
6,595,444 members and growing! (21,401 online)
Email Password   helpLost your password?
Languages » C# » General     Advanced

Loading and reading the Microsoft Excel file contents using C#

By _duk_

Using Excel Namespace in C#, we can load or open the Excel file and read the cell contents
C#, VC7, .NET, Win2K, WinXP, Visual Studio, Dev
Posted:19 Oct 2005
Views:164,035
Bookmarked:41 times
Unedited contribution
Announcements
Loading...
 
Search    
Advanced Search
Add to IE Search
printPrint   add Share
      Discuss Discuss   Broken Article?Report  
34 votes for this article.
Popularity: 3.15 Rating: 2.06 out of 5
16 votes, 47.1%
1
3 votes, 8.8%
2
5 votes, 14.7%
3
5 votes, 14.7%
4
5 votes, 14.7%
5

Introduction

This is application is kind of test application which tells how to use Microsoft Excel 10.0 Object Library, by using this library, how to load/read the excel contents.

Note: Here, I have created test.xls file which will be copied to c:\ before run the application.

Application developed using sample console application under VC# projects.

Steps:

 1. Include the following reference into the project :

  Microsoft Excel 10.0 Object Library

  Microsoft Office 10.0 Object Library

 

2. Include the name space i.e. using Excel.

3. Creating the ExcelApplicationClass,WorkBook and Range.

Find the complete code below:

using System;
using Excel;
namespace TestExcel
{
 /// <summary>
 /// Summary description for ExcelApplication.
 /// </summary>
 class ExcelApplication
 {
      /// <summary>
  /// The main entry point for the application.
  /// </summary>
      [STAThread]
      static void Main(string[] args)
      {
        
         string Path = @"c:\test.xls";
         // initialize the Excel Application class
         Excel.ApplicationClass app = new ApplicationClass();
         // create the workbook object by opening the excel file.
         Excel.Workbook workBook = app.Workbooks.Open(Path,
                                                      0,
                                                      true,
                                                      5,
                                                      "",
                                                      "",
                                                      true,
                                                      Excel.XlPlatform.xlWindows,
                                                      "\t",
                                                      false,
                                                      false,
                                                      0,
                                                      true,
                                                      1,
                                                      0);
         // get the active worksheet using sheet name or active sheet
         Excel.Worksheet workSheet = (Excel.Worksheet)workBook.ActiveSheet;
         int index = 0;
         // This row,column index should be changed as per your need.
         // i.e. which cell in the excel you are interesting to read.
         object rowIndex = 2;
         object colIndex1 = 1;
         object colIndex2 = 2;
         try
         {
            while ( ((Excel.Range)workSheet.Cells[rowIndex,colIndex1]).Value2 != null )
            {
               rowIndex = 2+index;
               string firstName = ((Excel.Range)workSheet.Cells[rowIndex,colIndex1]).Value2.ToString();
               string lastName = ((Excel.Range)workSheet.Cells[rowIndex,colIndex2]).Value2.ToString();
               Console.WriteLine("Name : {0},{1} ",firstName,lastName);
               index++;
            }
         }
         catch(Exception ex)
         {
            app.Quit();
            Console.WriteLine(ex.Message);
         }
      }
     
 }
}

 

 

 

 

 

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

_duk_


Member

Location: Russian Federation Russian Federation

Other popular C# articles:

Article Top
You must Sign In to use this message board.
FAQ FAQ 
 
Noise Tolerance  Layout  Per page   
 Msgs 1 to 25 of 25 (Total in Forum: 25) (Refresh)FirstPrevNext
Generalreading the Microsoft Excel file contents using C# Pinmembervishwa137:00 27 Aug '09  
GeneralRe: reading the Microsoft Excel file contents using C# Pinmembertripathy.rajendra@gmail.com17:02 24 Sep '09  
GeneralReference to the Excel Object library PinmemberAACINC5:51 28 Jan '09  
GeneralException in while loop Pinmemberjacky333315:51 19 Jan '09  
GeneralHow to read excel file using VC++ PinmemberTammanna_19840:33 8 May '08  
Generalthis code does not read the date format in excel file Pinmembermurugapandiyan19:50 10 Nov '07  
GeneralReading Excel (Office 1997) PinmemberStar Dreamer7:34 25 Sep '07  
GeneralRe: Reading Excel (Office 1997) PinmemberStar Dreamer7:36 25 Sep '07  
QuestionReading From Excel in C# and showing it and Sharepoint server 2007 Pinmemberkhan_SharePoint0:18 27 Jul '07  
GeneralDoes this work online? Pinmember10:56 6 Mar '07  
GeneralRe: Does this work online? Pinmemberkevgriffin2:55 27 Mar '07  
GeneralRe: Does this work online? PinmembershaistaShaikh0:29 1 Jul '08  
GeneralRe: Does this work online? Pinmembercodevigilante12:00 26 Feb '09  
GeneralNot all Excel instances are being destroyed Pinmembereyasso20:37 23 Dec '06  
GeneralRe: Not all Excel instances are being destroyed Pinmemberkevgriffin23:30 26 Mar '07  
GeneralVery Poor Performance PinmemberANIL KUMAR SHARMA (INDIA)19:08 3 Jun '06  
GeneralRe: Very Poor Performance Pinmemberlaughingandliving18:00 20 Jul '06  
GeneralRe: Very Poor Performance PinmemberJFMiller12:25 21 Aug '06  
GeneralRe: Very Poor Performance PinmemberBarbaMariolino1:15 16 Jun '08  
GeneralRe: Very Poor Performance Pinmemberronny_29520:09 5 Jan '09  
Generalthe code dosnt read the dateformat Pinmembersalah_gis21:52 3 May '06  
GeneralRe: the code dosnt read the dateformat Pinmembermurugapandiyan2:02 11 Nov '07  
QuestionDoesn't exit properly PinmemberNutter4Ever7:07 3 Feb '06  
AnswerRe: Doesn't exit properly Pinmemberjouceyc22:55 6 Sep '06  
GeneralRe: Doesn't exit properly PinmemberNutter4Ever10:54 7 Sep '06  

General General    News News    Question Question    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

PermaLink | Privacy | Terms of Use
Last Updated: 19 Oct 2005
Editor:
Copyright 2005 by _duk_
Everything else Copyright © CodeProject, 1999-2009
Web18 | Advertise on the Code Project