Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles / Languages / C#

Loading and reading Microsoft Excel file content using C#

1.71/5 (41 votes)
19 Oct 2005CPOL 1  
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)