All Topics, C#, .NET >> C# Programming >> General

Opening and Navigating Excel with C#
By Jeff Ruys

Introduction to manipulating Excel with C# 
 Beginner
 C#
Windows (WinXP, Win2K, Win2003, Win95, Win98, WinME), .NET
Win32, VS
Dev
 Posted 3 Oct 2003
  
 

Introduction

This is a short introduction to opening an existing Microsoft Excel spreadsheet using Visual C# .NET. The computer must have Excel installed on the system for this code to run properly. The Excel assembly is used to open and manipulate Excel spreadsheets.

Code Explanation

First, the Excel assembly must be added to the project. To do this you must add a reference to the Excel 10 Object Library (Excel XP) by going to the Project -> Add Reference  menu item. Go to the COM tab of the dialog box that pops up and scroll down the Excel 10 Object Library. Double click on it and press OK. This adds the reference to your project. In the "using" section of your code, type

    using Excel; 

Once the assembly is added to the project, a new application needs to be created:

    Excel.Application excelApp = new Excel.ApplicationClass();

If you want to make Excel visible to the user you have to set the Visible property to true, the default is false.

    excelApp.Visable = true;

The code above opens the Excel application, in order to use the application, you have to open a workbook by creating a Workbook object. You can open a new blank workbook by using the following code:

    Excel.Workbook newWorkbook = 
        excelApp.Workbooks.Add(XlWBATemplate.xlWBATWorksheet);

The preceding code opens a blank workbook with one sheet. The .Add method takes a template object, the XlWBATemplate.xlWBATWorksheet object is a built in template for a blank workbook.

If you want to open an existing document for editing instead of creating a new one, you can use the following code to create a Workbook object:

    string workbookPath = "c:/SomeWorkBook.xls";
    Excel.Workbook excelWorkbook = excelApp.Workbooks.Open(workbookPath,
        0, false, 5, "", "", false, Excel.XlPlatform.xlWindows, "",
        true, false, 0, true, false, false);

The preceding code may need a little explanation. The workbookPath is of course the path to the existing spreadsheet that you want to open. The excelApp.Workbooks.open is a little less obvious. The following is the list of parameters that are passed to the function:

  • WorkBooks.open(string Filename, object UpdateLinks, object ReadOnly, object Format, object Password, object WriteResPassword, object ReadOnlyRecommend, object Origin, object Delimiter, object Editable, object Notify, object Converter, object AddToMru, object Local, object CorruptLoad )

To view the documentation on this function, follow the link Microsoft’s website for further explanation of the function at:

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vbaxl10/html/xlmthOpen.asp

Once the workbook is either created or opened, you must create a Sheets object that holds the Worksheets within the workbook. The following code will get all of the sheets in the workbook you previously opened.

    Excel.Sheets excelSheets = excelWorkbook.Worksheets;

Now that you have the collection of Worksheets, you must get an individual sheet edit data within.

    string currentSheet = "Sheet1";
    Excel.Worksheet excelWorksheet = 
        (Excel.Worksheet)excelSheets.get_Item(currentSheet);

In the preceding code, you have to type cast the excelSheets.get_Item(string) function because it returns an object. Now that you have the sheet you are ready to access individual cells with the following code:

    Excel.Range excelCell = 
        (Excel.Range)excelWorksheet.get_Range("A1", "A1");

The get_Range function must take two parameters. If the two parameters are equal, a single cell is selected; otherwise a range of cells will be selected. Again you have to type cast the return value of the function. Once you have a cell object, your can set its value using the .Value2 property, or use any of the other properties to manipulate ranges of cells.

Jeff Ruys


   
 FAQ Noise level     
 View   Per page  
 New threadMsgs 1 to 25 of 115 (Total: 115) ()First Prev Next     
Subject 
Author 
Date 
  check Excel97 language setting
 Uffel3 9:56 1 Jun '05 
  Security policy
 ro_angel_bv 3:22 30 May '05 
  Excel 10 Object Library not available
 seeker@seekerspalace.com 10:19 28 May '05 
  Re: Excel 10 Object Library not available
 seeker@seekerspalace.com 10:23 28 May '05 
  How to read data from Excel using C#
 Anonymous 19:20 5 May '05 
  Re: How to read data from Excel using C#
Unconfirmed/Anonymous posting yevaud_us 20:58 6 May '05 
  Re: How to read data from Excel using C#
Unconfirmed/Anonymous posting yevaud_us 21:11 6 May '05 
  Re: How to read data from Excel using C#
 Jeff Ruys 23:28 8 May '05 
  Re: How to read data from Excel using C#
 Yevaud_us2 14:44 9 May '05 
  Re: How to read data from Excel using C#
 Yevaud_us2 16:34 9 May '05 
  Re: How to read data from Excel using C#
 Jeff Ruys 18:37 9 May '05 
  Problem with Excel Range Names in C#
 Uffel3 9:11 26 Apr '05 
  Re: Problem with Excel Range Names in C#
 Jeff Ruys 18:44 26 Apr '05 
  Re: Problem with Excel Range Names in C#
 Uffel3 4:16 27 Apr '05 
  Re: Problem with Excel Range Names in C#
 Jeff Ruys 0:00 28 Apr '05 
  Re: Problem with Excel Range Names in C#
 Uffel3 3:37 28 Apr '05 
  Re: Problem with Excel Range Names in C#
 Uffel3 16:49 15 May '05 
  Re: Problem with Excel Range Names in C#
 Jeff Ruys 0:19 16 May '05 
  Create Excel file form aspx.cs
Unconfirmed/Anonymous posting amyat 4:09 14 Feb '05 
  using text values to access cell
Unconfirmed/Anonymous posting Anonymous 11:41 10 Feb '05 
  Re: using text values to access cell
 Flora PL 6:36 12 Apr '05 
  C#, set an Excel Column to Text?
Unconfirmed/Anonymous posting Anonymous 12:30 26 Jan '05 
  Re: C#, set an Excel Column to Text?
 Jeff Ruys 18:45 26 Jan '05 
  Can write code C++ in C#?
 Chau Duong 22:42 9 Jan '05 
  Re: Can write code C++ in C#?
 Jeff Ruys 22:56 9 Jan '05 
Last Visit: 6:08 Thursday 16th June, 2005First Prev Next     

All Topics, C#, .NET >> C# Programming >> General
Updated: 3 Oct 2003
Article content copyright Jeff Ruys, 2003
everything else Copyright � CodeProject, 1999-2005.
 | Privacy