How To Use Microsoft.office.interop.excel In C#
To use Microsoft.Office.Interop.Excel in C#, install desktop Excel on Windows, add an Excel interop reference to your project, and create an Excel.Application instance. Use its workbooks, worksheets and ranges to read or write data, then save your changes and close Excel. Interop controls the installed.
Which licences fit your PC setup?
Prices are an AUD snapshot, not a live quote. Confirm variant and total on the listing.
-

Office 2021 Pro Plus key
$57.00 AUD
-

Office 2019 Pro Plus key
$45.00 AUD
-

Microsoft 365 Pro Plus, 5 devices
$15.00 AUD
$29.00
Check whether Excel Interop fits your application
Interop exposes Excel's COM object model to C#. It suits desktop automation that needs Excel itself, including its calculation engine, formatting and workbook features. Adding the interop assembly supplies the .NET types; it does not install Excel on the destination computer.
For an ASP.NET application, Windows service or unattended file-processing job, consider a library that edits workbook files without launching Office. Microsoft explains the limitations of unattended Office automation, including blocking dialogue boxes and unsupported non-interactive scenarios.
Set up your C# project
- Open Excel manually on the Windows computer that will run the application. Finish any first-run prompts and confirm that you can open and save a workbook.
- Create a C# desktop or console project in Visual Studio. A .NET Framework console project is a straightforward starting point for learning this COM API.
- Open the project's reference manager and locate the installed Microsoft Excel Object Library under COM references. Add it to expose the Microsoft.Office.Interop.Excel namespace. The displayed library name depends on the installed Office release.
- Add the namespace alias shown below at the top of your C# file. This keeps Excel types distinct from similarly named types in other libraries.
- For a console application, place [STAThread] above Main so the entry thread uses the single-threaded apartment model expected by Office automation.
using Excel = Microsoft.Office.Interop.Excel;
If Excel reports an activation problem, resolve it before debugging workbook operations. The store's guide to activating Microsoft Office using CMD covers that separate setup task. Activation still requires a valid licence.
If you are checking an older installation's licence records, the guide to finding a Microsoft Office product key using Command Prompt discusses that task. Product-key retrieval is separate from adding an interop reference.
Create a workbook and write a cell
The main object hierarchy is Application, Workbooks, Workbook, Worksheets, Worksheet and Range. Excel worksheet collections use one-based indexing: Worksheets[1] selects the first worksheet. A workbook's default worksheet count depends on Excel's settings, so do not assume that a new workbook always contains exactly one sheet.
The following statements illustrate the creation sequence. Put them inside your method's try block, with the object variables declared outside that block so the finally block can access them for cleanup.
-
Start a dedicated Excel instance. Make it visible while learning so you can see what your code changes.
app = new Excel.Application();
app.Visible = true;
-
Keep the collection and workbook references in separate variables.
books = app.Workbooks;
book = books.Add();
sheets = book.Worksheets;
sheet = (Excel.Worksheet)sheets[1];
-
Select a cell and assign its value. The corresponding variable types are Excel.Application, Excel.Workbooks, Excel.Workbook, Excel.Sheets, Excel.Worksheet and Excel.Range.
cell = sheet.Range["A1"];
cell.Value2 = "Hello, Excel!";
-
Save to an absolute path in an existing, writable folder. Replace this example path and use a fresh filename to avoid an overwrite prompt.
book.SaveAs(@"C:\Reports\example.xlsx", Excel.XlFileFormat.xlOpenXMLWorkbook);
Use Save() when saving changes to an existing workbook at its current location. Use SaveAs() when choosing a new filename or format. Match the extension to the requested file format.
Open a workbook and read values safely
To read an existing file, replace books.Add() with an Open call. For a read-only task, explicitly disable link updates and request read-only access:
book = books.Open(@"C:\Reports\input.xlsx", UpdateLinks: 0, ReadOnly: true);
Retrieve the worksheet and range as before, then read the cell without assuming it contains text:
object value = cell.Value2;
string text = value == null ? string.Empty : Convert.ToString(value);
A blank cell can return null, so calling ToString() directly on its value can fail. Numbers, text and Excel error values also require different handling. The Range.Value2 documentation explains that this property does not use the Currency or Date data types; dates can arrive as numeric serial values.
For a rectangular range containing multiple cells, Value2 can return a two-dimensional array. Inspect its bounds before iterating. Reading a block in one call reduces repeated communication with Excel compared with reading every cell separately.
Apply formulas and formatting to specific ranges
Use Value2 for data and Formula for a formula expression. For example, after obtaining a range for D2, assign totalCell.Formula = "=SUM(B2:C2)";. Keep formula output outside its input cells to avoid circular references.
Formatting belongs to the relevant range objects. Set a header range's Font.Bold property to true, apply NumberFormat to numeric output, or call AutoFit() on the intended columns. Retain references to Font and column range objects when you need explicit control over their lifetime.
For larger writes, prepare an object[,] array in C# and assign it to an equally sized rectangular range's Value2 property. Keep data entry, formula assignment and formatting as separate operations so each targets the intended cells.
Choose export formats deliberately. CSV stores a single sheet's text data and loses workbook formatting and other sheets. ExportAsFixedFormat publishes PDF or XPS, as described in Microsoft's fixed-format export documentation; it is not a PNG export method.
Close Excel even when an operation fails
Saving, closing and releasing COM references are separate concerns. Excel objects are managed through runtime callable wrappers, and remaining references can keep the Excel process alive after Quit().
- Initialise your object variables to null before entering try, then create and use them inside it.
- In finally, close the workbook if it was created. After an explicit successful save, book.Close(SaveChanges: false) avoids an additional save prompt.
- Use a nested finally to attempt app.Quit() even if closing the workbook throws. Quit only the Excel instance your application owns.
- Remove retained references when finished. If deterministic COM release is necessary, release owned child objects before parent objects and never use them afterwards. Avoid indiscriminately releasing wrappers that other code may still share.
Do not treat killing all Excel processes as routine cleanup: it can discard a user's unrelated work. During development, check that your own automation instance exits, including when opening or saving fails.
For installation-related troubleshooting beyond this workflow, browse the Microsoft Office guides.
Start with a disposable workbook: write A1, save it, close your Excel instance, then reopen the file manually and confirm the value before adding formulas or processing real data.
Other editions for separate workloads
Other editions for separate workloads; prices in AUD, confirm the total on the listing.
-

Windows 11 Pro key
$14.99 AUD
$29.00 -

Office 2024 Pro Plus key
$20.00 AUD
$39.00 -

Windows 11 Home key
$18.88 AUD
$37.00
How to check this worked
- Open Settings, System, Activation and confirm the edition shown.
- Enter the key and run activation; expect a green tick within a minute.
- Reboot and re-check activation survives the restart.
FAQ
What else should you know before buying?
- What do I need before starting?
- Read the requirements and confirm the edition first; the steps assume the machine is compatible.
- How do I know the licence is genuine?
- Activate on Microsoft's servers and check Settings, System, Activation. A working activation is the test.
- Is this a subscription?
- Keys sold here are one-time lifetime licences. Microsoft 365 plans are billed by Microsoft instead.
- What if the key fails?
- Email hi@ms.codes with the order number and a screenshot. Replacement or refund applies within 30 days.
Check who prepared and reviewed this guide
- Written by
- MS.Codes editorial
- Reviewed by
- Support team
Last verified: (source review; see test status above).