Click here to Skip to main content
12,559,868 members (48,424 online)
Click here to Skip to main content
Add your own
alternative version


245 bookmarked

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


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 parameters are a little less obvious. The following is the list of parameters that are passed to the function:

  • 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:

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.


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


About the Author

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

You may also be interested in...

Comments and Discussions

QuestionGood one,really helpful Pin
venubattini21-Jul-16 7:09
membervenubattini21-Jul-16 7:09 
QuestionVery Well Expained Pin
@nkit Bajpai14-Apr-16 23:41
professional@nkit Bajpai14-Apr-16 23:41 
QuestionIssue with Microsoft.Office.Interop.Excel assembly on windows 2008 R2 server. Pin
Member 123024812-Feb-16 1:07
memberMember 123024812-Feb-16 1:07 
Questioncan the excel sheet be formatted before generating? Pin
sarthakm23-May-15 7:15
membersarthakm23-May-15 7:15 
QuestionThis might be interesting here: Pin
Dietmar Schoder29-Jul-14 10:34
professionalDietmar Schoder29-Jul-14 10:34 
AnswerRe: This might be interesting here: Pin
Member 1116567219-Oct-14 19:40
memberMember 1116567219-Oct-14 19:40 
GeneralRe: This might be interesting here: Pin
Dietmar Schoder19-Oct-14 20:26
professionalDietmar Schoder19-Oct-14 20:26 
QuestionGet Error in this code Pin
DilipSutariya5-May-14 0:00
memberDilipSutariya5-May-14 0:00 
QuestionGreat Pin
TopDawg Dawg25-Mar-14 19:05
memberTopDawg Dawg25-Mar-14 19:05 
QuestionGracias !! Pin
Member 1067956918-Mar-14 3:51
memberMember 1067956918-Mar-14 3:51 
QuestionHow I can keep lead zeros when get_value? Pin
Paul_ll14-Dec-13 9:36
memberPaul_ll14-Dec-13 9:36 
QuestionSpreadSheetLight Pin
TL Wallace25-Nov-13 11:45
memberTL Wallace25-Nov-13 11:45 
GeneralMy vote of 4 Pin
jonthan3417-May-13 11:39
memberjonthan3417-May-13 11:39 
Questionyou awsome Pin
Dark_traveller9-May-13 0:32
memberDark_traveller9-May-13 0:32 
QuestionVisible, not Visable Pin
wizardzz17-Apr-12 8:11
memberwizardzz17-Apr-12 8:11 
AnswerRe: Visible, not Visable Pin
Narsters17-Apr-12 9:46
memberNarsters17-Apr-12 9:46 
GeneralMy vote of 4 Pin
ArjunPr21-Sep-11 18:04
memberArjunPr21-Sep-11 18:04 
GeneralMy vote of 3 Pin
Member 810932425-Jul-11 1:37
memberMember 810932425-Jul-11 1:37 
GeneralMy vote of 4 Pin
fyarbasi4-Oct-10 1:07
memberfyarbasi4-Oct-10 1:07 
QuestionHow to Make Bold in Part of an Excel Cell - Need Eargent help!!!!!!! Pin
supuna2u12-Sep-10 17:16
membersupuna2u12-Sep-10 17:16 
QuestionOpening an excel workbook from Document Library Pin
suruKarthik30-Mar-10 23:16
membersuruKarthik30-Mar-10 23:16 
QuestionHow to make it work with Excel 2007 Pin
th3_m0t410-Feb-10 13:48
memberth3_m0t410-Feb-10 13:48 
AnswerRe: How to make it work with Excel 2007 Pin
Narsters10-Feb-10 18:08
memberNarsters10-Feb-10 18:08 
GeneralReading Excel Embeded objects Pin
Shrihari Devji7-Jan-10 20:15
memberShrihari Devji7-Jan-10 20:15 
GeneralThank you very much Pin
Bidesh Mukherjee25-Nov-09 0:44
memberBidesh Mukherjee25-Nov-09 0:44 
QuestionWhat exactly are the downloaded source files supposed to do? Pin
stapes2-Mar-09 5:35
memberstapes2-Mar-09 5:35 
AnswerRe: What exactly are the downloaded source files supposed to do? Pin
Narsters2-Mar-09 6:40
memberNarsters2-Mar-09 6:40 
GeneralThe server threw an exception.(Exception from HRESULT: 0x80010105(RPC_E_SERVERFAULT)) Pin
Member 13942176-Nov-08 6:14
memberMember 13942176-Nov-08 6:14 
GeneralThanks a bunch!!!! Pin
Archerthegreat28-Oct-08 17:12
memberArcherthegreat28-Oct-08 17:12 
GeneralPopulating an Excel "template" in ASP.NET Pin
slapmatt10-Sep-08 23:03
memberslapmatt10-Sep-08 23:03 
GeneralRe: Populating an Excel "template" in ASP.NET Pin
Jeff Ruys12-Sep-08 9:59
memberJeff Ruys12-Sep-08 9:59 
GeneralRe: Populating an Excel "template" in ASP.NET Pin
CikaPero12-Apr-11 22:37
memberCikaPero12-Apr-11 22:37 
QuestionCom Exception 0x80028018 TYPE_E_INVDATAREAD Pin
tho_wa9-Sep-08 1:25
membertho_wa9-Sep-08 1:25 

I have tried the code (and also other examples found on the internet) but all are resulting in a COM Exception 0x80028018 (TYPE_E_INVDATAREAD) when trying to open the excel sheet.

Here is the complete error message:
System.Runtime.InteropServices.COMException was unhandled
Message="Old format or invalid type library. (Exception from HRESULT: 0x80028018 (TYPE_E_INVDATAREAD))"
at Excel.Workbooks.Add(Object Template)
at ExcelExample.ExcelClass.Main(String[] args)
at System.AppDomain._nExecuteAssembly(Assembly assembly, String[] args)
at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
at System.Threading.ThreadHelper.ThreadStart_Context(Object state)
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
at System.Threading.ThreadHelper.ThreadStart()

I'm using Excel 2003 SP3 (11.8169.8172) with Visual Studio 2005 (8.0.50727.762 - SP.050727-7600) and .NET Framework V2.0.50727 SP1

Any suggestions?

Thanks a lot
AnswerRe: Com Exception 0x80028018 TYPE_E_INVDATAREAD Pin
Jeff Ruys12-Sep-08 10:21
memberJeff Ruys12-Sep-08 10:21 
AnswerRe: Com Exception 0x80028018 TYPE_E_INVDATAREAD Pin
ofercinn17-Sep-08 2:21
memberofercinn17-Sep-08 2:21 
GeneralRe: Com Exception 0x80028018 TYPE_E_INVDATAREAD Pin
Member 119956312-Jul-10 13:04
memberMember 119956312-Jul-10 13:04 
GeneralRe: Com Exception 0x80028018 TYPE_E_INVDATAREAD Pin
nafzali15-Sep-11 6:05
membernafzali15-Sep-11 6:05 
QuestionHow to move to last row of Active WorkSheet programmatically using C#? Pin
Dinesh Patel18-Aug-08 19:56
memberDinesh Patel18-Aug-08 19:56 
AnswerRe: How to move to last row of Active WorkSheet programmatically using C#? Pin
Jeff Ruys21-Aug-08 8:26
memberJeff Ruys21-Aug-08 8:26 
QuestionHow to insert a row from one excel to another using 2005? Pin
VDivyaReddy13-Aug-08 23:15
memberVDivyaReddy13-Aug-08 23:15 
AnswerRe: How to insert a row from one excel to another using 2005? Pin
rajkumarsingh3-Oct-08 0:18
memberrajkumarsingh3-Oct-08 0:18 
QuestionHelp! C# Excel 'get_range' returning value for cell in the next row Pin
Member 271871110-Jul-08 9:44
memberMember 271871110-Jul-08 9:44 
AnswerRe: Help! C# Excel 'get_range' returning value for cell in the next row Pin
Jeff Ruys10-Jul-08 17:08
memberJeff Ruys10-Jul-08 17:08 
Generalto close the workbook Pin
Member 15181678-May-08 10:00
memberMember 15181678-May-08 10:00 
GeneralI DID IT !!!!!!!!!!!!!!!!!!!!!!!! Pin
Member 151816710-May-08 9:52
memberMember 151816710-May-08 9:52 
QuestionHi. How to copy a range of column/cells from one excel document to another ? Pin
Member 15181677-May-08 11:48
memberMember 15181677-May-08 11:48 
AnswerRe: Hi. How to copy a range of column/cells from one excel document to another ? Pin
Jeff Ruys7-May-08 16:21
memberJeff Ruys7-May-08 16:21 
GeneralRe: Hi. How to copy a range of column/cells from one excel document to another ? Pin
Member 15181678-May-08 4:39
memberMember 15181678-May-08 4:39 
GeneralRe: Hi. How to copy a range of column/cells from one excel document to another ? Pin
Jeff Ruys8-May-08 6:13
memberJeff Ruys8-May-08 6:13 
GeneralRe: Hi. How to copy a range of column/cells from one excel document to another ? Pin
Member 15181678-May-08 8:23
memberMember 15181678-May-08 8:23 

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.

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