Click here to Skip to main content
14,297,293 members

Loading and reading Microsoft Excel file content using C#

Rate this:
1.71 (41 votes)
Please Sign up or sign in to vote.
1.71 (41 votes)
19 Oct 2005CPOL
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 Pin
Tareq Newaz Shahriar30-Oct-14 20:08
memberTareq 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
Murugu Pandiyan10-Nov-07 18:50
memberMurugu Pandiyan10-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 SharePoint Developer26-Jul-07 23:18
memberkhan SharePoint Developer26-Jul-07 23:18 
QuestionDoes this work online? Pin
Adrian Malone6-Mar-07 9:56
memberAdrian Malone6-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
JakeSays20-Jul-06 17:00
memberJakeSays20-Jul-06 17:00 
GeneralRe: Very Poor Performance Pin
JFMiller21-Aug-06 11:25
memberJFMiller21-Aug-06 11:25 
GeneralRe: Very Poor Performance Pin
FilipKrnjic16-Jun-08 0:15
memberFilipKrnjic16-Jun-08 0:15 
GeneralRe: Very Poor Performance Pin
ronny_2955-Jan-09 19:09
groupronny_2955-Jan-09 19:09 
Generalthe code dosnt read the dateformat Pin
salah_gis3-May-06 20:52
membersalah_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.

Article
Posted 19 Oct 2005

Stats

345K views
50 bookmarked