Click here to Skip to main content
11,412,833 members (72,404 online)
Click here to Skip to main content

Opening and Navigating Excel with C#

, 17 Apr 2012 CPOL
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

 
QuestionThis might be interesting here: Pin
Dietmar Schoder at 29-Jul-14 11:34
professionalDietmar Schoder29-Jul-14 11:34 
AnswerRe: This might be interesting here: Pin
Member 11165672 at 19-Oct-14 20:40
memberMember 1116567219-Oct-14 20:40 
GeneralRe: This might be interesting here: Pin
Dietmar Schoder at 19-Oct-14 21:26
professionalDietmar Schoder19-Oct-14 21:26 
QuestionGet Error in this code Pin
DilipSutariya at 5-May-14 1:00
memberDilipSutariya5-May-14 1:00 
QuestionGreat Pin
TopDawg Dawg at 25-Mar-14 20:05
memberTopDawg Dawg25-Mar-14 20:05 
QuestionGracias !! Pin
Member 10679569 at 18-Mar-14 4:51
memberMember 1067956918-Mar-14 4:51 
QuestionHow I can keep lead zeros when get_value? Pin
Paul_ll at 14-Dec-13 10:36
memberPaul_ll14-Dec-13 10:36 
QuestionSpreadSheetLight Pin
TL Wallace at 25-Nov-13 12:45
memberTL Wallace25-Nov-13 12:45 
GeneralMy vote of 4 Pin
jonthan34 at 17-May-13 12:39
memberjonthan3417-May-13 12:39 
Questionyou awsome Pin
Dark_traveller at 9-May-13 1:32
memberDark_traveller9-May-13 1:32 
QuestionVisible, not Visable Pin
wizardzz at 17-Apr-12 9:11
memberwizardzz17-Apr-12 9:11 
AnswerRe: Visible, not Visable Pin
Narsters at 17-Apr-12 10:46
memberNarsters17-Apr-12 10:46 
GeneralMy vote of 4 Pin
ArjunPr at 21-Sep-11 19:04
memberArjunPr21-Sep-11 19:04 
GeneralMy vote of 3 Pin
Member 8109324 at 25-Jul-11 2:37
memberMember 810932425-Jul-11 2:37 
GeneralMy vote of 4 Pin
fyarbasi at 4-Oct-10 2:07
memberfyarbasi4-Oct-10 2:07 
QuestionHow to Make Bold in Part of an Excel Cell - Need Eargent help!!!!!!! Pin
supuna2u at 12-Sep-10 18:16
membersupuna2u12-Sep-10 18:16 
QuestionOpening an excel workbook from Document Library Pin
suruKarthik at 31-Mar-10 0:16
membersuruKarthik31-Mar-10 0:16 
QuestionHow to make it work with Excel 2007 Pin
th3_m0t4 at 10-Feb-10 14:48
memberth3_m0t410-Feb-10 14:48 
I got the following error:
COMException was unhandled
Old format or type of library not valid.
Exception HRESULT: 0x80028018 (TYPE_E_INVDATAREAD)

I assume that this error is because I'm using Excel 2007

Thxs for the help

Hugo Mota
AnswerRe: How to make it work with Excel 2007 Pin
Narsters at 10-Feb-10 19:08
memberNarsters10-Feb-10 19:08 
GeneralReading Excel Embeded objects Pin
Shrihari Devji at 7-Jan-10 21:15
memberShrihari Devji7-Jan-10 21:15 
GeneralThank you very much Pin
Bidesh Mukherjee at 25-Nov-09 1:44
memberBidesh Mukherjee25-Nov-09 1:44 
QuestionWhat exactly are the downloaded source files supposed to do? Pin
stapes at 2-Mar-09 6:35
memberstapes2-Mar-09 6:35 
AnswerRe: What exactly are the downloaded source files supposed to do? Pin
Narsters at 2-Mar-09 7:40
memberNarsters2-Mar-09 7:40 
GeneralThe server threw an exception.(Exception from HRESULT: 0x80010105(RPC_E_SERVERFAULT)) Pin
Member 1394217 at 6-Nov-08 7:14
memberMember 13942176-Nov-08 7:14 
GeneralThanks a bunch!!!! Pin
Archerthegreat at 28-Oct-08 18:12
memberArcherthegreat28-Oct-08 18:12 
GeneralPopulating an Excel "template" in ASP.NET Pin
slapmatt at 11-Sep-08 0:03
memberslapmatt11-Sep-08 0:03 
GeneralRe: Populating an Excel "template" in ASP.NET Pin
Jeff Ruys at 12-Sep-08 10:59
memberJeff Ruys12-Sep-08 10:59 
GeneralRe: Populating an Excel "template" in ASP.NET Pin
CikaPero at 12-Apr-11 23:37
memberCikaPero12-Apr-11 23:37 
QuestionCom Exception 0x80028018 TYPE_E_INVDATAREAD Pin
tho_wa at 9-Sep-08 2:25
membertho_wa9-Sep-08 2:25 
AnswerRe: Com Exception 0x80028018 TYPE_E_INVDATAREAD Pin
Jeff Ruys at 12-Sep-08 11:21
memberJeff Ruys12-Sep-08 11:21 
AnswerRe: Com Exception 0x80028018 TYPE_E_INVDATAREAD Pin
ofercinn at 17-Sep-08 3:21
memberofercinn17-Sep-08 3:21 
GeneralRe: Com Exception 0x80028018 TYPE_E_INVDATAREAD Pin
Member 1199563 at 12-Jul-10 14:04
memberMember 119956312-Jul-10 14:04 
GeneralRe: Com Exception 0x80028018 TYPE_E_INVDATAREAD Pin
nafzali at 15-Sep-11 7:05
membernafzali15-Sep-11 7:05 
QuestionHow to move to last row of Active WorkSheet programmatically using C#? Pin
Dinesh Patel at 18-Aug-08 20:56
memberDinesh Patel18-Aug-08 20:56 
AnswerRe: How to move to last row of Active WorkSheet programmatically using C#? Pin
Jeff Ruys at 21-Aug-08 9:26
memberJeff Ruys21-Aug-08 9:26 
QuestionHow to insert a row from one excel to another using c#.net 2005? Pin
VDivyaReddy at 14-Aug-08 0:15
memberVDivyaReddy14-Aug-08 0:15 
AnswerRe: How to insert a row from one excel to another using c#.net 2005? Pin
rajkumarsingh at 3-Oct-08 1:18
memberrajkumarsingh3-Oct-08 1:18 
QuestionHelp! C# Excel 'get_range' returning value for cell in the next row Pin
Member 2718711 at 10-Jul-08 10:44
memberMember 271871110-Jul-08 10:44 
AnswerRe: Help! C# Excel 'get_range' returning value for cell in the next row Pin
Jeff Ruys at 10-Jul-08 18:08
memberJeff Ruys10-Jul-08 18:08 
Generalto close the workbook Pin
Member 1518167 at 8-May-08 11:00
memberMember 15181678-May-08 11:00 
GeneralI DID IT !!!!!!!!!!!!!!!!!!!!!!!! Pin
Member 1518167 at 10-May-08 10:52
memberMember 151816710-May-08 10:52 
QuestionHi. How to copy a range of column/cells from one excel document to another ? Pin
Member 1518167 at 7-May-08 12:48
memberMember 15181677-May-08 12:48 
AnswerRe: Hi. How to copy a range of column/cells from one excel document to another ? Pin
Jeff Ruys at 7-May-08 17:21
memberJeff Ruys7-May-08 17:21 
GeneralRe: Hi. How to copy a range of column/cells from one excel document to another ? Pin
Member 1518167 at 8-May-08 5:39
memberMember 15181678-May-08 5:39 
GeneralRe: Hi. How to copy a range of column/cells from one excel document to another ? Pin
Jeff Ruys at 8-May-08 7:13
memberJeff Ruys8-May-08 7:13 
GeneralRe: Hi. How to copy a range of column/cells from one excel document to another ? Pin
Member 1518167 at 8-May-08 9:23
memberMember 15181678-May-08 9:23 
QuestionCom exception Pin
manu4ever at 22-Apr-08 11:22
membermanu4ever22-Apr-08 11:22 
GeneralRe: Com exception Pin
Jeff Ruys at 22-Apr-08 11:54
memberJeff Ruys22-Apr-08 11:54 
GeneralRe: Com exception Pin
manu4ever at 22-Apr-08 12:05
membermanu4ever22-Apr-08 12:05 
GeneralRe: Com exception Pin
manu4ever at 22-Apr-08 12:16
membermanu4ever22-Apr-08 12:16 

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 | Terms of Use | Mobile
Web03 | 2.8.150427.1 | Last Updated 17 Apr 2012
Article Copyright 2003 by Narsters
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid