Click here to Skip to main content
15,881,600 members
Articles / Programming Languages / C#

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 362.1K   50   29
Using the Excel namespace in C#, we can load or open an Excel file and read the cell contents.

Introduction

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#.

Steps

  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:

C#
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, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Russian Federation Russian Federation
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
GeneralMy vote of 1 Pin
Tareq Newaz Shahriar30-Oct-14 20:08
Tareq Newaz Shahriar30-Oct-14 20:08 
GeneralMy vote of 1 Pin
Muthu samy11-Jun-13 1:16
Muthu samy11-Jun-13 1:16 
Generalthx Pin
hamdynassar15-May-11 23:54
hamdynassar15-May-11 23:54 
Generalreading the Microsoft Excel file contents using C# Pin
vishwa1327-Aug-09 6:00
vishwa1327-Aug-09 6:00 
GeneralRe: reading the Microsoft Excel file contents using C# Pin
tripathy.rajendra@gmail.com24-Sep-09 16:02
tripathy.rajendra@gmail.com24-Sep-09 16:02 
GeneralReference to the Excel Object library Pin
AACINC28-Jan-09 4:51
AACINC28-Jan-09 4:51 
GeneralException in while loop Pin
jacky333319-Jan-09 14:51
jacky333319-Jan-09 14:51 
QuestionHow to read excel file using VC++ Pin
Tammanna_19847-May-08 23:33
Tammanna_19847-May-08 23:33 
Hi,
I want to read excel file using VC++. If anybody knows plz share.
Generalthis code does not read the date format in excel file Pin
Murugu Pandiyan10-Nov-07 18:50
Murugu Pandiyan10-Nov-07 18:50 
GeneralRe: this code does not read the date format in excel file Pin
CikaPero18-Apr-10 23:10
CikaPero18-Apr-10 23:10 
GeneralReading Excel (Office 1997) Pin
Star Dreamer25-Sep-07 6:34
Star Dreamer25-Sep-07 6:34 
GeneralRe: Reading Excel (Office 1997) Pin
Star Dreamer25-Sep-07 6:36
Star 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
khan SharePoint Developer26-Jul-07 23:18 
QuestionDoes this work online? Pin
Adrian Malone6-Mar-07 9:56
Adrian Malone6-Mar-07 9:56 
AnswerRe: Does this work online? Pin
kevgriffin27-Mar-07 1:55
kevgriffin27-Mar-07 1:55 
GeneralRe: Does this work online? Pin
shaistaShaikh30-Jun-08 23:29
shaistaShaikh30-Jun-08 23:29 
GeneralRe: Does this work online? Pin
codevigilante26-Feb-09 11:00
codevigilante26-Feb-09 11:00 
GeneralNot all Excel instances are being destroyed Pin
eyasso23-Dec-06 19:37
eyasso23-Dec-06 19:37 
GeneralRe: Not all Excel instances are being destroyed Pin
kevgriffin26-Mar-07 22:30
kevgriffin26-Mar-07 22:30 
GeneralVery Poor Performance Pin
ANIL KUMAR SHARMA (INDIA)3-Jun-06 18:08
ANIL KUMAR SHARMA (INDIA)3-Jun-06 18:08 
GeneralRe: Very Poor Performance Pin
JakeSays20-Jul-06 17:00
JakeSays20-Jul-06 17:00 
GeneralRe: Very Poor Performance Pin
JFMiller21-Aug-06 11:25
JFMiller21-Aug-06 11:25 
GeneralRe: Very Poor Performance Pin
FilipKrnjic16-Jun-08 0:15
FilipKrnjic16-Jun-08 0:15 
GeneralRe: Very Poor Performance Pin
ronny_2955-Jan-09 19:09
ronny_2955-Jan-09 19:09 
Generalthe code dosnt read the dateformat Pin
salah_gis3-May-06 20:52
salah_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.