Click here to Skip to main content
12,453,183 members (60,490 online)
Click here to Skip to main content
Add your own
alternative version

Stats

313.8K views
50 bookmarked
Posted

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

You may also be interested in...

Pro
Pro

Comments and Discussions

 
GeneralMy vote of 1 Pin
Tariq Newaz Shahriar30-Oct-14 20:08
memberTariq 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
murugapandiyan10-Nov-07 18:50
membermurugapandiyan10-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_SharePoint26-Jul-07 23:18
memberkhan_SharePoint26-Jul-07 23:18 
QuestionDoes this work online? Pin
Member #38635796-Mar-07 9:56
memberMember #38635796-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
ANIL KUMAR SHARMA (INDIA)3-Jun-06 18:08
memberANIL KUMAR SHARMA (INDIA)3-Jun-06 18:08 
GeneralRe: Very Poor Performance Pin
laughingandliving20-Jul-06 17:00
memberlaughingandliving20-Jul-06 17:00 
GeneralRe: Very Poor Performance Pin
JFMiller21-Aug-06 11:25
memberJFMiller21-Aug-06 11:25 
GeneralRe: Very Poor Performance Pin
BarbaMariolino16-Jun-08 0:15
memberBarbaMariolino16-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 
GeneralRe: the code dosnt read the dateformat Pin
murugapandiyan11-Nov-07 1:02
membermurugapandiyan11-Nov-07 1:02 
QuestionDoesn't exit properly Pin
Nutter4Ever3-Feb-06 6:07
memberNutter4Ever3-Feb-06 6:07 
AnswerRe: Doesn't exit properly Pin
jouceyc6-Sep-06 21:55
memberjouceyc6-Sep-06 21:55 
GeneralRe: Doesn't exit properly Pin
Nutter4Ever7-Sep-06 9:54
memberNutter4Ever7-Sep-06 9:54 

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.

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