Click here to Skip to main content
Licence CPOL
First Posted 2 Oct 2003
Views 877,730
Downloads 7,325
Bookmarked 204 times

Opening and Navigating Excel with C#

By | 17 Apr 2012 | Article
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

using Excel = Microsoft.Office.Interop.Excel; 

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

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.  

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:

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: 

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.

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) method 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 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)

About the Author

Narsters

Software Developer (Senior)

United States United States

Member



Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
AnswerRe: Com Exception 0x80028018 TYPE_E_INVDATAREAD Pinmemberofercinn2:21 17 Sep '08  
GeneralRe: Com Exception 0x80028018 TYPE_E_INVDATAREAD PinmemberMember 119956313:04 12 Jul '10  
GeneralRe: Com Exception 0x80028018 TYPE_E_INVDATAREAD Pinmembernafzali6:05 15 Sep '11  
QuestionHow to move to last row of Active WorkSheet programmatically using C#? PinmemberDinesh Patel19:56 18 Aug '08  
AnswerRe: How to move to last row of Active WorkSheet programmatically using C#? PinmemberJeff Ruys8:26 21 Aug '08  
QuestionHow to insert a row from one excel to another using c#.net 2005? PinmemberVDivyaReddy23:15 13 Aug '08  
AnswerRe: How to insert a row from one excel to another using c#.net 2005? Pinmemberrajkumarsingh0:18 3 Oct '08  
U can use StreamReader and StreamWritter.........
QuestionHelp! C# Excel 'get_range' returning value for cell in the next row PinmemberMember 27187119:44 10 Jul '08  
AnswerRe: Help! C# Excel 'get_range' returning value for cell in the next row PinmemberJeff Ruys17:08 10 Jul '08  
Generalto close the workbook PinmemberMember 151816710:00 8 May '08  
GeneralI DID IT !!!!!!!!!!!!!!!!!!!!!!!! PinmemberMember 15181679:52 10 May '08  
QuestionHi. How to copy a range of column/cells from one excel document to another ? PinmemberMember 151816711:48 7 May '08  
AnswerRe: Hi. How to copy a range of column/cells from one excel document to another ? PinmemberJeff Ruys16:21 7 May '08  
GeneralRe: Hi. How to copy a range of column/cells from one excel document to another ? PinmemberMember 15181674:39 8 May '08  
GeneralRe: Hi. How to copy a range of column/cells from one excel document to another ? PinmemberJeff Ruys6:13 8 May '08  
GeneralRe: Hi. How to copy a range of column/cells from one excel document to another ? PinmemberMember 15181678:23 8 May '08  
QuestionCom exception Pinmembermanu4ever10:22 22 Apr '08  
GeneralRe: Com exception PinmemberJeff Ruys10:54 22 Apr '08  
GeneralRe: Com exception Pinmembermanu4ever11:05 22 Apr '08  
GeneralRe: Com exception Pinmembermanu4ever11:16 22 Apr '08  
GeneralRe: Com exception PinmemberJeff Ruys13:22 22 Apr '08  
GeneralRe: Com exception Pinmembermanu4ever18:17 22 Apr '08  
GeneralRe: Com exception PinmemberJeff Ruys18:46 22 Apr '08  
Generalread an Excel range (cell) of date type into C# string PinmemberMember 271871111:00 20 Mar '08  
AnswerRe: read an Excel range (cell) of date type into C# string PinmemberJeff Ruys14:40 20 Mar '08  

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.

Permalink | Advertise | Privacy | Mobile
Web02 | 2.5.120529.1 | Last Updated 17 Apr 2012
Article Copyright 2003 by Narsters
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid