Click here to Skip to main content
15,391,859 members
Articles / Programming Languages / C#
Posted 19 Oct 2005


50 bookmarked

Loading and reading Microsoft Excel file content using C#

Rate me:
Please Sign up or sign in to vote.
1.71/5 (41 votes)
19 Oct 2005CPOL
Using the Excel namespace in C#, we can load or open an Excel file and read the cell contents.


This is a test application which tells how to use the Microsoft Excel 10.0 Object Library to load/read Excel content.

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

The application is a Console Application developed using VC#.


  1. Include the following references to the project:
    • Microsoft Excel 10.0 Object Library
    • Microsoft Office 10.0 Object Library
  2. Include the required namespace: using Excel;.
  3. Create the objects ExcelApplicationClass, WorkBook, and Range.

Here is the complete code:

using System;
using Excel; 

namespace TestExcel
 /// <summary>
 /// Summary description for ExcelApplication.
 /// </summary>
 class ExcelApplication
      /// <summary>
      /// The main entry point for the application.
      /// </summary>
      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, 
         // 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; 
            while ( ((Excel.Range)workSheet.Cells[rowIndex,colIndex1]).Value2 != null )
               rowIndex = 2+index;
               string firstName = 
               string lastName = 
               Console.WriteLine("Name : {0},{1} ",firstName,lastName);
         catch(Exception ex)


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


About the Author

Russian Federation Russian Federation
No Biography provided

Comments and Discussions

GeneralMy vote of 1 Pin
Tareq Newaz Shahriar30-Oct-14 20:08
MemberTareq Newaz Shahriar30-Oct-14 20:08 
GeneralMy vote of 1 Pin
Muthu samy11-Jun-13 1:16
MemberMuthu samy11-Jun-13 1:16 
Generalthx Pin
hamdynassar15-May-11 23:54
Memberhamdynassar15-May-11 23:54 
Generalreading the Microsoft Excel file contents using C# Pin
vishwa1327-Aug-09 6:00
Membervishwa1327-Aug-09 6:00 
GeneralRe: reading the Microsoft Excel file contents using C# Pin
tripathy.rajendra@gmail.com24-Sep-09 16:02
Membertripathy.rajendra@gmail.com24-Sep-09 16:02 
GeneralReference to the Excel Object library Pin
AACINC28-Jan-09 4:51
MemberAACINC28-Jan-09 4:51 
GeneralException in while loop Pin
jacky333319-Jan-09 14:51
Memberjacky333319-Jan-09 14:51 
QuestionHow to read excel file using VC++ Pin
Tammanna_19847-May-08 23:33
MemberTammanna_19847-May-08 23:33 
Generalthis code does not read the date format in excel file Pin
Murugu Pandiyan10-Nov-07 18:50
MemberMurugu Pandiyan10-Nov-07 18:50 
GeneralRe: this code does not read the date format in excel file Pin
CikaPero18-Apr-10 23:10
MemberCikaPero18-Apr-10 23:10 
GeneralReading Excel (Office 1997) Pin
Star Dreamer25-Sep-07 6:34
MemberStar Dreamer25-Sep-07 6:34 
GeneralRe: Reading Excel (Office 1997) Pin
Star Dreamer25-Sep-07 6:36
MemberStar Dreamer25-Sep-07 6:36 
QuestionReading From Excel in C# and showing it and Sharepoint server 2007 Pin
khan SharePoint Developer26-Jul-07 23:18
Memberkhan SharePoint Developer26-Jul-07 23:18 
QuestionDoes this work online? Pin
Adrian Malone6-Mar-07 9:56
MemberAdrian Malone6-Mar-07 9:56 
AnswerRe: Does this work online? Pin
kevgriffin27-Mar-07 1:55
Memberkevgriffin27-Mar-07 1:55 
GeneralRe: Does this work online? Pin
shaistaShaikh30-Jun-08 23:29
MembershaistaShaikh30-Jun-08 23:29 
GeneralRe: Does this work online? Pin
codevigilante26-Feb-09 11:00
Membercodevigilante26-Feb-09 11:00 
GeneralNot all Excel instances are being destroyed Pin
eyasso23-Dec-06 19:37
Membereyasso23-Dec-06 19:37 
GeneralRe: Not all Excel instances are being destroyed Pin
kevgriffin26-Mar-07 22:30
Memberkevgriffin26-Mar-07 22:30 
GeneralVery Poor Performance Pin
MemberANIL KUMAR SHARMA (INDIA)3-Jun-06 18:08 
GeneralRe: Very Poor Performance Pin
JakeSays20-Jul-06 17:00
MemberJakeSays20-Jul-06 17:00 
GeneralRe: Very Poor Performance Pin
JFMiller21-Aug-06 11:25
MemberJFMiller21-Aug-06 11:25 
GeneralRe: Very Poor Performance Pin
FilipKrnjic16-Jun-08 0:15
MemberFilipKrnjic16-Jun-08 0:15 
GeneralRe: Very Poor Performance Pin
ronny_2955-Jan-09 19:09
Memberronny_2955-Jan-09 19:09 
Generalthe code dosnt read the dateformat Pin
salah_gis3-May-06 20:52
Membersalah_gis3-May-06 20:52 

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

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