Click here to Skip to main content
12,452,344 members (54,148 online)
Click here to Skip to main content

Stats

1.3M views
24.6K downloads
245 bookmarked
Posted

Opening and Navigating Excel with C#

, 17 Apr 2012 CPOL
Introduction to manipulating Excel with C#.
ExcelExample
App.ico
bin
Release
ExcelExample.exe
Interop.Excel.dll
Interop.Office.dll
Interop.VBIDE.dll
ExcelExample.csproj.user
obj
Release
temp
TempPE
App.ico
Debug
ExcelExample.exe
ExcelExample.pdb
ExcelExample.vshost.exe
Interop.Microsoft.Office.Core.dll
Interop.VBIDE.dll
ExcelExample.exe
ExcelExample.vshost.exe
ExcelExample.vshost.exe.manifest
Interop.Excel.dll
Interop.Microsoft.Office.Core.dll
Interop.Office.dll
Interop.VBIDE.dll
ExcelExample.csproj.user
ExcelExample.suo
Debug
DesignTimeResolveAssemblyReferencesInput.cache
ExcelExample.csproj.ResolveComReference.cache
ExcelExample.exe
ExcelExample.pdb
Interop.Microsoft.Office.Core.dll
Interop.VBIDE.dll
TempPE
DesignTimeResolveAssemblyReferencesInput.cache
ExcelExample.csproj.ResolveComReference.cache
ExcelExample.exe
Interop.Microsoft.Office.Core.dll
Interop.VBIDE.dll
using System;
using Excel = Microsoft.Office.Interop.Excel;

namespace ExcelExample
{
	/// <summary>
	/// Summary description for Class1.
	/// </summary>
	class ExcelClass
	{
		/// <summary>
		/// The main entry point for the application.
		/// </summary>
		[STAThread]
		static void Main(string[] args)
		{
			Excel.Application excelApp = new Excel.Application();  // Creates a new Excel Application
			excelApp.Visible = true;  // Makes Excel visible to the user.

			// The following line if uncommented adds a new workbook
			//Excel.Workbook newWorkbook = excelApp.Workbooks.Add();
			
			// The following code opens an existing workbook
			string workbookPath = "c:/SomeWorkBook.xlsx";  // Add your own path here

            Excel.Workbook excelWorkbook = null;

            try
            {
                excelWorkbook = excelApp.Workbooks.Open(workbookPath, 0,
                    false, 5, "", "", false, Excel.XlPlatform.xlWindows, "", true,
                    false, 0, true, false, false);
            }
            catch
            {
                //Create a new workbook if the existing workbook failed to open.
                excelWorkbook = excelApp.Workbooks.Add();
            }
			
			// The following gets the Worksheets collection
			Excel.Sheets excelSheets = excelWorkbook.Worksheets;

			// The following gets Sheet1 for editing
			string currentSheet = "Sheet1";
			Excel.Worksheet excelWorksheet = (Excel.Worksheet)excelSheets.get_Item(currentSheet);
			
			// The following gets cell A1 for editing
			Excel.Range excelCell = (Excel.Range)excelWorksheet.get_Range("A1", "A1");

			// The following sets cell A1's value to "Hi There"
			excelCell.Value2 = "Hi There";
		}
	}
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

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

You may also be interested in...

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