Click here to Skip to main content
Click here to Skip to main content

Loading and reading Microsoft Excel file content using C#

, 19 Oct 2005 CPOL
Rate this:
Please Sign up or sign in to vote.
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:

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)

Share

About the Author

_duk_

Russian Federation Russian Federation
No Biography provided

Comments and Discussions

 
GeneralMy vote of 1 PinmemberTariq Newaz Shahriar30-Oct-14 21:08 
GeneralMy vote of 1 PinmemberMuthu samy11-Jun-13 2:16 
Generalthx Pinmemberhamdynassar16-May-11 0:54 
Generalreading the Microsoft Excel file contents using C# Pinmembervishwa1327-Aug-09 7:00 
GeneralRe: reading the Microsoft Excel file contents using C# Pinmembertripathy.rajendra@gmail.com24-Sep-09 17:02 
GeneralReference to the Excel Object library PinmemberAACINC28-Jan-09 5:51 
GeneralException in while loop Pinmemberjacky333319-Jan-09 15:51 
QuestionHow to read excel file using VC++ PinmemberTammanna_19848-May-08 0:33 
Generalthis code does not read the date format in excel file Pinmembermurugapandiyan10-Nov-07 19:50 
GeneralRe: this code does not read the date format in excel file PinmemberCikaPero19-Apr-10 0:10 
GeneralReading Excel (Office 1997) PinmemberStar Dreamer25-Sep-07 7:34 
GeneralRe: Reading Excel (Office 1997) PinmemberStar Dreamer25-Sep-07 7:36 
QuestionReading From Excel in C# and showing it and Sharepoint server 2007 Pinmemberkhan_SharePoint27-Jul-07 0:18 
QuestionDoes this work online? PinmemberMember #38635796-Mar-07 10:56 
AnswerRe: Does this work online? Pinmemberkevgriffin27-Mar-07 2:55 
GeneralRe: Does this work online? PinmembershaistaShaikh1-Jul-08 0:29 
GeneralRe: Does this work online? Pinmembercodevigilante26-Feb-09 12:00 
GeneralNot all Excel instances are being destroyed Pinmembereyasso23-Dec-06 20:37 
GeneralRe: Not all Excel instances are being destroyed Pinmemberkevgriffin26-Mar-07 23:30 
GeneralVery Poor Performance PinmemberANIL KUMAR SHARMA (INDIA)3-Jun-06 19:08 
GeneralRe: Very Poor Performance Pinmemberlaughingandliving20-Jul-06 18:00 
GeneralRe: Very Poor Performance PinmemberJFMiller21-Aug-06 12:25 
GeneralRe: Very Poor Performance PinmemberBarbaMariolino16-Jun-08 1:15 
GeneralRe: Very Poor Performance Pinmemberronny_2955-Jan-09 20:09 
Generalthe code dosnt read the dateformat Pinmembersalah_gis3-May-06 21:52 
GeneralRe: the code dosnt read the dateformat Pinmembermurugapandiyan11-Nov-07 2:02 
QuestionDoesn't exit properly PinmemberNutter4Ever3-Feb-06 7:07 
AnswerRe: Doesn't exit properly Pinmemberjouceyc6-Sep-06 22:55 
GeneralRe: Doesn't exit properly PinmemberNutter4Ever7-Sep-06 10:54 

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

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

| Advertise | Privacy | Terms of Use | Mobile
Web03 | 2.8.1411019.1 | Last Updated 19 Oct 2005
Article Copyright 2005 by _duk_
Everything else Copyright © CodeProject, 1999-2014
Layout: fixed | fluid