Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

Loading and reading the Microsoft Excel file contents using C#

0.00/5 (No votes)
19 Oct 2005 1  
Using Excel Namespace in C#, we can load or open the Excel file and read the cell contents

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:

<address>using System;
using Excel; </address>


<address>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(); </address>


<address>         // 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; </address>


<address>         int index = 0; </address>


<address>         // 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; </address>


<address>         try
         { </address>


<address>            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); </address>


<address>               index++; </address>


<address>            }
         }
         catch(Exception ex)
         {
            app.Quit();
            Console.WriteLine(ex.Message);
         }
      }
      
 }
} </address>

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