What reference do I need to use Microsoft.Office.Interop.Excel in .NET?
Codemia Knowledge Hub ·
Microsoft Office Interop for Excel is an indispensable tool for developers aiming to integrate Excel functionality within .NET applications. By using `Microsoft.Office.Interop.Excel`, developers can programmatically manage Excel files such as reading, writing, formatting data, and automating Excel tasks.
Adding Microsoft.Office.Interop.Excel Reference
To leverage `Microsoft.Office.Interop.Excel` within a .NET application, you must first add the appropriate assembly reference to your project.
Step-by-step Guide:
- Check for Microsoft Office Installation: Ensure that Microsoft Office is installed on the system where the application will be executed. This is necessary because the Interop assemblies require Office applications to function correctly.
- Install Interop Assemblies:
- NuGet Package: The easiest way is by using NuGet Package Manager in Visual Studio:
- Open your project in Visual Studio.
- Navigate to Tools > NuGet Package Manager > Manage NuGet Packages for Solution.
- Search for `Microsoft.Office.Interop.Excel`.
- Click Install.
- Visual Studio Assemblies:
- Right-click the project in Solution Explorer.
- Select Add > Reference.
- In the COM tab, find Microsoft Excel XX.0 Object Library, where XX corresponds to the version of Excel installed.
- Check it and click OK.
Example Code:
Here's a simple example demonstrating how to create a new Excel file, write data to a cell, and save it using the Interop library.
- Ensure that the path where you save the file is accessible, and you have permission to write to it.
- Excel will remain in memory if not properly closed. It's essential to release COM objects by calling the `Marshal.ReleaseComObject` for each Excel object you've created if you need to ensure proper shutdown.
- Excel 365/2019: `Microsoft Excel 16.0 Object Library`
- Excel 2016: `Microsoft Excel 16.0 Object Library`
- Excel 2013: `Microsoft Excel 15.0 Object Library`
- Excel 2010: `Microsoft Excel 14.0 Object Library`
- Excel isn't installed.
- Incorrect permissions.
- Invalid file paths.
- Client Environment: The client machine must have the necessary Excel version installed.
- Threading: The Interop model isn't thread-safe. Excel interop operations must be performed on a single thread, typically the main UI thread.
Related reading
- What replaces WCF in .Net Core?
- What so different about Node.js's event-driven? Can't we do that in ASP.Net's HttpAsyncHandler?
- What Sorting Algorithm Is Used By LINQ OrderBy?
- What strategies and tools are useful for finding memory leaks in .NET?
- What type should I use for a 128-bit number in in .NET?
- What value should I pass into TimeZoneInfo.FindSystemTimeZoneByIdString?
- What version of .NET ships with what version of Windows?
- What's a good way of doing string templating in .NET?

OOD Fundamentals
Master object-oriented design from first principles, SOLID, design patterns, and classic interview problems with hands-on coding.
View the courseData Structures & Algorithms practice on Codemia
Step through 300+ coding interview problems with animated visualisers that show the data structure changing as the code runs. The Blind 75 are free.