Click here to Skip to main content
15,881,139 members
Articles / Programming Languages / C#

Opening and Navigating Excel with C#

Rate me:
Please Sign up or sign in to vote.
4.57/5 (139 votes)
17 Apr 2012CPOL3 min read 2M   28.5K   246   291
Introduction to manipulating Excel with C#.

Introduction 

Note: This article has been updated to interact with Office 2007. I left the old .NET 1.0 solution for Office XP available for download for reference (in case you are using an old version of Office).

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 Microsoft.Office.Interop.Excel library by going to the Project -> Add Reference  menu item. Go to the .NET tab of the dialog box that pops up and scroll down the Microsoft.Office.Interop.Excel list item. Double click on it and press OK. This adds the reference to your project. In the "using" section of your code, type

C#
using Excel = Microsoft.Office.Interop.Excel; 

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

C#
Excel.Application excelApp = new Excel.Application();

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

C#
excelApp.Visible = 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:

C#
Excel.Workbook newWorkbook = excelApp.Workbooks.Add();

The preceding code opens a blank workbook with one sheet. The .Add method optionally takes a template object.  If the parameter is left blank, the default template will be used (like when you open Excel manually).

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

C#
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, and if using .NET 4.0 and the Office 2007 library (Microsoft.Office.Interop.Excel library), it is the only parameter required. The rest of the excelApp.Workbooks.open parameters are 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 to Microsoft’s website for further explanation of the function at: http://msdn.microsoft.com/en-us/library/bb179167%28v=office.12%29.aspx

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.

C#
Excel.Sheets excelSheets = excelWorkbook.Worksheets;

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

C#
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) method because it returns an object. Now that you have the sheet you are ready to access individual cells with the following code:

C#
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 method. 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.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer (Senior)
United States United States
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
GeneralRe: Error "Access Denied" - PLZZZ HELP! Pin
cdavis7-Jun-06 10:00
cdavis7-Jun-06 10:00 
GeneralRe: Error "Access Denied" - PLZZZ HELP! Pin
Yukta7-Jun-06 21:20
Yukta7-Jun-06 21:20 
GeneralRe: Error "Access Denied" - PLZZZ HELP! Pin
Yukta7-Jun-06 21:23
Yukta7-Jun-06 21:23 
GeneralRe: Error "Access Denied" - PLZZZ HELP! Pin
ian_orozco7-Jun-06 17:06
ian_orozco7-Jun-06 17:06 
GeneralRe: Error "Access Denied" - PLZZZ HELP! Pin
Yukta7-Jun-06 21:18
Yukta7-Jun-06 21:18 
GeneralRe: Error "Access Denied" - PLZZZ HELP! Pin
Yukta10-Jun-06 4:17
Yukta10-Jun-06 4:17 
Generalcells with 0 values Pin
RB@Emphasys27-Feb-06 7:50
RB@Emphasys27-Feb-06 7:50 
Questionhow to make range of cells readonly??? Pin
deepakbadki30-Jan-06 1:45
deepakbadki30-Jan-06 1:45 
AnswerRe: how to make range of cells readonly??? Pin
abhiram_nayan29-Jan-09 22:50
abhiram_nayan29-Jan-09 22:50 
GeneralError when opening Excel 2003 Pin
sunil_s17-Jan-06 23:18
sunil_s17-Jan-06 23:18 
GeneralRe: Error when opening Excel 2003 Pin
skystar200117-Jan-06 23:34
skystar200117-Jan-06 23:34 
GeneralRe: Error when opening Excel 2003 Pin
Narsters18-Jan-06 12:31
Narsters18-Jan-06 12:31 
GeneralWeb Service Printing Pin
Richard Zey17-Jan-06 10:13
Richard Zey17-Jan-06 10:13 
QuestionReading Tab separated files in Excel Pin
sunil_s5-Jan-06 23:52
sunil_s5-Jan-06 23:52 
AnswerRe: Reading Tab separated files in Excel Pin
Narsters6-Jan-06 11:56
Narsters6-Jan-06 11:56 
QuestionComprehensive guide for Excel Object ? Pin
Big Cheese1-Dec-05 2:44
Big Cheese1-Dec-05 2:44 
AnswerRe: Comprehensive guide for Excel Object ? Pin
Narsters1-Dec-05 12:28
Narsters1-Dec-05 12:28 
GeneralSpecialCells Pin
Anonymous11-Oct-05 5:11
Anonymous11-Oct-05 5:11 
QuestionProblem with the count of rows and columns Pin
Anonymous11-Oct-05 1:24
Anonymous11-Oct-05 1:24 
Generalexcel page Pin
alexby7-Sep-05 8:04
alexby7-Sep-05 8:04 
GeneralRe: excel page Pin
Narsters7-Sep-05 13:09
Narsters7-Sep-05 13:09 
GeneralRe: excel page Pin
MPSharp9-Sep-05 5:39
MPSharp9-Sep-05 5:39 
QuestionHow to read only selected range from excel sheet Pin
atodmal20-Jul-05 1:40
atodmal20-Jul-05 1:40 
AnswerRe: How to read only selected range from excel sheet Pin
Narsters20-Jul-05 13:23
Narsters20-Jul-05 13:23 
GeneralRe: How to read only selected range from excel sheet Pin
atodmal20-Jul-05 20:17
atodmal20-Jul-05 20:17 

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.