Click here to Skip to main content
Click here to Skip to main content

Opening and Navigating Excel with C#

, 17 Apr 2012
Rate this:
Please Sign up or sign in to vote.
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)

Share

About the Author

Narsters
Software Developer (Senior)
United States United States
No Biography provided

Comments and Discussions

 
GeneralMy vote of 4 PinmemberArjunPr21-Sep-11 18:04 
GeneralMy vote of 3 PinmemberMember 810932425-Jul-11 1:37 
GeneralMy vote of 4 Pinmemberfyarbasi4-Oct-10 1:07 
QuestionHow to Make Bold in Part of an Excel Cell - Need Eargent help!!!!!!! Pinmembersupuna2u12-Sep-10 17:16 
QuestionOpening an excel workbook from Document Library PinmembersuruKarthik30-Mar-10 23:16 
QuestionHow to make it work with Excel 2007 Pinmemberth3_m0t410-Feb-10 13:48 
AnswerRe: How to make it work with Excel 2007 PinmemberNarsters10-Feb-10 18:08 
GeneralReading Excel Embeded objects PinmemberShrihari Devji7-Jan-10 20:15 
HI
I have an excel file to be read in C#. I used the ODBC to read the data from the excel and it works fine. But one of its sheets has embeded objects e.g Textbox in it. I have to read data from that text box. So how can i read this text box data from C#.
GeneralThank you very much PinmemberBidesh Mukherjee25-Nov-09 0:44 
QuestionWhat exactly are the downloaded source files supposed to do? Pinmemberstapes2-Mar-09 5:35 
AnswerRe: What exactly are the downloaded source files supposed to do? PinmemberNarsters2-Mar-09 6:40 
GeneralThe server threw an exception.(Exception from HRESULT: 0x80010105(RPC_E_SERVERFAULT)) PinmemberMember 13942176-Nov-08 6:14 
GeneralThanks a bunch!!!! PinmemberArcherthegreat28-Oct-08 17:12 
GeneralPopulating an Excel "template" in ASP.NET Pinmemberslapmatt10-Sep-08 23:03 
GeneralRe: Populating an Excel "template" in ASP.NET PinmemberJeff Ruys12-Sep-08 9:59 
GeneralRe: Populating an Excel "template" in ASP.NET PinmemberCikaPero12-Apr-11 22:37 
QuestionCom Exception 0x80028018 TYPE_E_INVDATAREAD Pinmembertho_wa9-Sep-08 1:25 
AnswerRe: Com Exception 0x80028018 TYPE_E_INVDATAREAD PinmemberJeff Ruys12-Sep-08 10:21 
AnswerRe: Com Exception 0x80028018 TYPE_E_INVDATAREAD Pinmemberofercinn17-Sep-08 2:21 
GeneralRe: Com Exception 0x80028018 TYPE_E_INVDATAREAD PinmemberMember 119956312-Jul-10 13:04 
GeneralRe: Com Exception 0x80028018 TYPE_E_INVDATAREAD Pinmembernafzali15-Sep-11 6:05 
QuestionHow to move to last row of Active WorkSheet programmatically using C#? PinmemberDinesh Patel18-Aug-08 19:56 
AnswerRe: How to move to last row of Active WorkSheet programmatically using C#? PinmemberJeff Ruys21-Aug-08 8:26 
QuestionHow to insert a row from one excel to another using c#.net 2005? PinmemberVDivyaReddy13-Aug-08 23:15 
AnswerRe: How to insert a row from one excel to another using c#.net 2005? Pinmemberrajkumarsingh3-Oct-08 0:18 
QuestionHelp! C# Excel 'get_range' returning value for cell in the next row PinmemberMember 271871110-Jul-08 9:44 
AnswerRe: Help! C# Excel 'get_range' returning value for cell in the next row PinmemberJeff Ruys10-Jul-08 17:08 
Generalto close the workbook PinmemberMember 15181678-May-08 10:00 
GeneralI DID IT !!!!!!!!!!!!!!!!!!!!!!!! PinmemberMember 151816710-May-08 9:52 
QuestionHi. How to copy a range of column/cells from one excel document to another ? PinmemberMember 15181677-May-08 11:48 
AnswerRe: Hi. How to copy a range of column/cells from one excel document to another ? PinmemberJeff Ruys7-May-08 16:21 
GeneralRe: Hi. How to copy a range of column/cells from one excel document to another ? PinmemberMember 15181678-May-08 4:39 
GeneralRe: Hi. How to copy a range of column/cells from one excel document to another ? PinmemberJeff Ruys8-May-08 6:13 
GeneralRe: Hi. How to copy a range of column/cells from one excel document to another ? PinmemberMember 15181678-May-08 8:23 
QuestionCom exception Pinmembermanu4ever22-Apr-08 10:22 
GeneralRe: Com exception PinmemberJeff Ruys22-Apr-08 10:54 
GeneralRe: Com exception Pinmembermanu4ever22-Apr-08 11:05 
GeneralRe: Com exception Pinmembermanu4ever22-Apr-08 11:16 
GeneralRe: Com exception PinmemberJeff Ruys22-Apr-08 13:22 
GeneralRe: Com exception Pinmembermanu4ever22-Apr-08 18:17 
GeneralRe: Com exception PinmemberJeff Ruys22-Apr-08 18:46 
Generalread an Excel range (cell) of date type into C# string PinmemberMember 271871120-Mar-08 11:00 
AnswerRe: read an Excel range (cell) of date type into C# string PinmemberJeff Ruys20-Mar-08 14:40 
GeneralRe: read an Excel range (cell) of date type into C# string PinmemberMember 271871121-Mar-08 4:15 
AnswerRe: read an Excel range (cell) of date type into C# string PinmemberfRenato17-Jun-08 2:50 
Questionhow to append data into already existing Excel sheet Pinmembershraddha19046-Mar-08 21:57 
AnswerRe: how to append data into already existing Excel sheet PinmemberJeff Ruys7-Mar-08 4:37 
Questionhow to append data into already existing Excel sheet???? PinmemberKrishnaraj Barvathaya B6-Mar-08 21:54 
QuestionExport to excel ,Help me Out Pinmemberpurushothamanv3-Mar-08 18:25 
QuestionDoes it work for web apps PinmemberMember 322823914-Dec-07 5:05 

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.

| Advertise | Privacy | Mobile
Web01 | 2.8.140827.1 | Last Updated 17 Apr 2012
Article Copyright 2003 by Narsters
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid