title | description | ms.custom | ms.date | ms.topic | dev_langs | helpviewer_keywords | author | ms.author | manager | ms.technology | ms.workload | |||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
Office primary interop assemblies |
Learn how to use the primary interop assembly (PIA) to gain access the features of a Microsoft Office application from an Office project. |
devdivchpfy22 |
12/23/2021 |
conceptual |
|
|
John-Hart |
johnhart |
jmartens |
office-development |
office |
Office primary interop assemblies
[!INCLUDE Visual Studio]
To use the features of a Microsoft Office application from an Office project, you must use the primary interop assembly (PIA) for the application. The PIA enables managed code to interact with a Microsoft Office application’s COM-based object model.
[!includeAdd-ins note]
When you create a new Office project, Visual Studio adds references to the PIAs that are required to build the project. In some scenarios, you might need to add references to other PIAs (for example, you can use a feature of Microsoft Office Word in a project for Microsoft Office Excel).
This article describes the following aspects of using the Microsoft Office PIAs in Office projects:
-
Separate primary interop assemblies to build and run projects
-
Use features of multiple Microsoft Office applications in a single project
-
Full list of primary interop assemblies for Microsoft Office applications
For more information about primary interop assemblies, see Primary interop assemblies.
Separate primary interop assemblies to build and run projects
Visual Studio uses different sets of the PIAs on the development computer. These different sets of assemblies are in the following locations:
-
A folder in the program files directory
This set of the assemblies is used when you write code and build projects. Visual Studio installs these assemblies automatically.
-
The global assembly cache
This set of the assemblies is used during some development tasks, such as when you run or debug projects. Visual Studio doesn’t install and register these assemblies; you need to do it yourself.
Primary interop assemblies in the program files directory
The PIAs are automatically added to a location in the file system, outside of the global assembly cache, while you install Visual Studio. When you create a new project, Visual Studio automatically adds references to these copies of the PIAs to your project. Visual Studio uses these copies of the PIAs, instead of the assemblies in the global assembly cache, to resolve type references when you develop and build your project.
When different versions of the PIAs are registered in the global assembly cache, you can face several development issues. The added copies of PIAs will help you to avoid such issues.
For Visual Studio 2017 and later, these copies of the PIAs are installed to following shared locations on the development computer:
-
%ProgramFiles%Microsoft Visual StudioSharedVisual Studio Tools for OfficePIA
-
(or
%ProgramFiles(x86)%Microsoft Visual StudioSharedVisual Studio Tools for OfficePIA
on 64-bit operating systems)
[!NOTE]
For older versions of Visual Studio, these PIAs will be installed to the Visual Studio Tools for OfficePIA folder under the%ProgramFiles%
folder for that version of Visual Studio.
For Example:%ProgramFiles(x86)%Microsoft Visual Studio 14.0Visual Studio Tools for OfficePIA
Primary interop assemblies in the global assembly cache
To perform certain development tasks, the PIAs must be installed and registered in the global assembly cache on the development computer. Typically, the PIAs are installed automatically when you install Office on the development computer. For more information, see Configure a computer to develop Office solutions.
The Office PIAs aren’t required on end-user computers to run Office solutions. For more information, see Design and create Office solutions.
Use features of multiple Microsoft Office applications in a single project
Every Office project template in Visual Studio is designed to work with a single Microsoft Office application. To use features in multiple Microsoft Office applications, or to use features in an application or component that doesn’t have a project in Visual Studio, you must add a reference to the required PIAs.
In most cases, you should add references to the PIAs that are installed by Visual Studio under the %ProgramFiles(x86)%Microsoft Visual StudioSharedVisual Studio Tools for OfficePIA
directory. These versions of the assemblies appear on the Framework tab of the Reference Manager dialog box. For more information, see How to: Target Office applications through primary interop assemblies.
If you’ve installed and registered the PIAs in the global assembly cache, these versions of the assemblies appear on the COM tab of the Reference Manager dialog box. Avoid adding references to these versions of the assemblies, because there are some development issues that can occur when you use them. For example, if you’ve registered different versions of the PIAs in the global assembly cache, your project will automatically bind to the version of the assembly that was registered last, even if you specify a different version of the assembly on the COM tab of the Reference Manager dialog box.
[!NOTE]
Some assemblies are added to a project automatically when an assembly that references them is added. For example, references to theOffice.dll
andMicrosoft.Vbe.Interop.dll
assemblies are added automatically when you add a reference to the Word, Excel, Outlook, Microsoft Forms, or Graph assemblies.
Primary interop assemblies for Microsoft Office applications
The following table lists the primary interop assemblies that are available for [!INCLUDEOffice_16_short], [!INCLUDEOffice_15_short] and [!INCLUDEoffice14_long].
Office application or component | Primary interop assembly name |
---|---|
Microsoft Access 14.0 Object Library
Microsoft Access 15.0 Object Library |
Microsoft.Office.Interop.Access.dll |
Microsoft Office 14.0 Access Database Engine Object Library
Microsoft Office 15.0 Access Database Engine Object Library |
Microsoft.Office.Interop.Access.Dao.dll |
Microsoft Excel 14.0 Object Library
Microsoft Excel 15.0 Object Library |
Microsoft.Office.Interop.Excel.dll |
Microsoft Graph 14.0 Object Library (used by PowerPoint, Access, and Word for graphs)
Microsoft Graph 15.0 Object Library |
Microsoft.Office.Interop.Graph.dll |
Microsoft InfoPath 2.0 Type Library (for InfoPath 2007 only) | Microsoft.Office.Interop.InfoPath.dll |
Microsoft InfoPath XML Interop Assembly (for InfoPath 2007 only) | Microsoft.Office.Interop.InfoPath.Xml.dll |
Microsoft Office 14.0 Object Library (Office shared functionality)
Microsoft Office 15.0 Object Library (Office shared functionality) |
office.dll |
Microsoft Office Outlook View Control (can be used in Web pages and applications to access your Inbox) | Microsoft.Office.Interop.OutlookViewCtl.dll |
Microsoft Outlook 14.0 Object Library
Microsoft Outlook 15.0 Object Library |
Microsoft.Office.Interop.Outlook.dll |
Microsoft PowerPoint 14.0 Object Library
Microsoft PowerPoint 15.0 Object Library |
Microsoft.Office.Interop.PowerPoint.dll |
Microsoft Project 14.0 Object Library
Microsoft Project 15.0 Object Library |
Microsoft.Office.Interop.MSProject.dll |
Microsoft Publisher 14.0 Object Library
Microsoft Publisher 15.0 Object Library |
Microsoft.Office.Interop.Publisher.dll |
Microsoft SharePoint Designer 14.0 Web Object Reference Library | Microsoft.Office.Interop.SharePointDesigner.dll |
Microsoft SharePoint Designer 14.0 Page Object Reference Library | Microsoft.Office.Interop.SharePointDesignerPage.dll |
Microsoft Smart Tags 2.0 Type Library Note: Smart tags are deprecated in [!INCLUDEExcel_14_short] and [!INCLUDEWord_14_short]. | Microsoft.Office.Interop.SmartTag.dll |
Microsoft Visio 14.0 Type Library
Microsoft Visio 15.0 Type Library |
Microsoft.Office.Interop.Visio.dll |
Microsoft Visio 14.0 Save As Web Type Library
Microsoft Visio 15.0 Save As Web Type Library |
Microsoft.Office.Interop.Visio.SaveAsWeb.dll |
Microsoft Visio 14.0 Drawing Control Type Library
Microsoft Visio 15.0 Drawing Control Type Library |
Microsoft.Office.Interop.VisOcx.dll |
Microsoft Word 14.0 Object Library
Microsoft Word 15.0 Object Library |
Microsoft.Office.Interop.Word.dll |
Microsoft Visual Basic for Applications Extensibility 5.3 | Microsoft.Vbe.Interop.dll |
Binding redirect assemblies
When you install and register the Office PIAs in the global assembly cache (either with Office or by installing the redistributable package for the PIAs), the binding redirect assemblies are also installed only in the global assembly cache. These assemblies ensure that the correct version of the primary interop assemblies is loaded at run time.
For example, when a solution that references a [!INCLUDEoffice14_long] assembly runs on a computer that has the [!INCLUDEOffice_15_short] version of the same primary interop assembly, the binding redirect assembly instructs the [!INCLUDEdnprdnshort] runtime to load the [!INCLUDEOffice_15_short] version of the primary interop assembly.
For more information, see How to: Enable and disable automatic binding redirection.
See also
- How to: Target Office applications through primary interop assemblies
- Excel object model overview
- InfoPath solutions
- Outlook object model overview
- PowerPoint solutions
- Project solutions
- Visio object model overview
- Word object model overview
- General reference (Office development in Visual Studio)
-
README
-
Frameworks
-
Dependencies
-
Used By
-
Versions
This an assembly you can use for Excel 2013/2016/2019 COM interop, generated and signed by Microsoft. This is entirely unsupported and there is no license since it is a repackaging of Office assemblies.
Product | Versions |
---|---|
.NET |
net5.0 net5.0-windows net6.0 net6.0-android net6.0-ios net6.0-maccatalyst net6.0-macos net6.0-tvos net6.0-windows net7.0 net7.0-android net7.0-ios net7.0-maccatalyst net7.0-macos net7.0-tvos net7.0-windows |
.NET Core |
netcoreapp2.0 netcoreapp2.1 netcoreapp2.2 netcoreapp3.0 netcoreapp3.1 |
.NET Standard |
netstandard2.0 netstandard2.1 |
.NET Framework |
net20 net35 net40 net403 net45 net451 net452 net46 net461 net462 net463 net47 net471 net472 net48 net481 |
MonoAndroid | monoandroid |
MonoMac | monomac |
MonoTouch | monotouch |
Tizen |
tizen40 tizen60 |
Xamarin.iOS | xamarinios |
Xamarin.Mac | xamarinmac |
Xamarin.TVOS | xamarintvos |
Xamarin.WatchOS | xamarinwatchos |
-
.NETFramework 4.0
- No dependencies.
-
.NETStandard 2.0
- No dependencies.
NuGet packages (68)
Showing the top 5 NuGet packages that depend on Microsoft.Office.Interop.Excel:
Package | Downloads |
---|---|
FileHelpers.ExcelStorage Extension for FileHelpers to support Excel read/write operations, using Interop Assemblies (deprecated) www.filehelpers.net |
143.1K |
fion.modelerp.core ModelErp |
111.9K |
BalaReva.Excel.Activities Tha package contains more activities .It make more the excel automation. |
95.0K |
MepApps.MepControls Mep Apps Inc. Base Code Module |
59.6K |
Horseshoe.NET.Excel An Excel file import utility for .NET (uses NPOI) |
29.1K |
GitHub repositories (10)
Showing the top 5 popular GitHub repositories that depend on Microsoft.Office.Interop.Excel:
Repository | Stars |
---|---|
greenshot/greenshot Greenshot for Windows — Report bugs & features go here: https://greenshot.atlassian.net or look for information on: |
3.3K |
Tichau/FileConverter File Converter is a very simple tool which allows you to convert and compress one or several file(s) using the context menu in windows explorer. |
2.6K |
DynamoDS/Dynamo Open Source Graphical Programming for Design |
1.4K |
Excel-DNA/ExcelDna Excel-DNA — Free and easy .NET for Excel. This repository contains the core Excel-DNA library. |
1.0K |
saucepleez/taskt taskt (pronounced ‘tasked’ and formely sharpRPA) is free and open-source robotic process automation (rpa) built in C# powered by the .NET Framework |
802 |
I am interested in using C# to manipulate/Automate Excel files.
After browsing the web I have found VSTO but it seems you can not use that in Visual Studio Express Edition so I can not use that.
Just few minutes ago I noticed a question in this site that used this namespace in its code:
Microsoft.Office.Interop.Excel
So I am wondering if all I need to use that is add necessary reference and if so which reference to add?
UPDATE
I installed «Primary Interop Assemblies» as the accepted answer suggested but for some reason they are still missing in the Add Reference dialog under «.NET» but they are present in GAC.
So I just added the reference using «Browse» and located Microsoft.Office.Interop.Excel.dll
in GAC folder.
But looking for similar questions on this site regarding adding reference from GAC it seems this is not recommended.
asked May 7, 2012 at 7:00
user850010user850010
6,29112 gold badges38 silver badges60 bronze badges
Update (thanks user2347528)
These assemblies are available as NuGet packages, which is much easier than my original answer.
You can install by either right clicking on References in your project and selecting Manage NuGet packages… and searching for one of the packages listed below, or install using the Package Manager Console:
PM> Install-Package Microsoft.Office.Interop.Excel
- Microsoft.Office.Interop.Excel
- Microsoft.Office.Interop.Word
- Microsoft.Office.Interop.Outlook
- Microsoft.Office.Interop.PowerPoint
- Microsoft.Office.Interop.Graph
These are available as «Primary Interop Assemblies», which can be installed with Office, or downloaded and installed separately. How to: Install Office Primary Interop Assemblies.
Once those are installed, you can reference them in your project in the Add Reference dialog, under .NET. If you do not see those Microsoft.Office.Interop assemblies listed, then they have not been installed yet. Install them from your setup, or download and install them separately (see my link above for the downloads).
answered May 7, 2012 at 7:03
moribvndvsmoribvndvs
42.1k11 gold badges137 silver badges149 bronze badges
8
Just had this problem myself and accepted answer didn’t help me but I solved it with:
Add reference > Browse > C: > Windows > assembly > GAC > Microsoft.Office.Interop.Excel > 12.0.0.0_etc > Microsoft.Office.Interop.Excel.dll
answered Mar 14, 2013 at 14:25
RobertRobert
5,27843 gold badges65 silver badges115 bronze badges
5
Answers didn’t help me to solve my problem, I couldn’t find (and browse) the assemblies although I installed them using Microsoft’s msi installer. For me, the excel assembly is located under C:WindowsassemblyGAC_MSILMicrosoft.Office.Interop.Excel14.0.0.0__71e9bce111e9429cMicrosoft.Office.Interop.Excel.dll
answered Jul 24, 2014 at 6:37
0x8BADF00D0x8BADF00D
9621 gold badge7 silver badges18 bronze badges
2
Just add reference of Microsoft.Office.Interop.Excel
.
It includes MicroSoft Excel related classes, no need to add more references.
bluish
25.9k27 gold badges120 silver badges179 bronze badges
answered May 7, 2012 at 7:06
RAKESH HOLKARRAKESH HOLKAR
2,1175 gold badges24 silver badges42 bronze badges
2
I guess what you are trying to do is add Microsoft.Office.Interop.Excel
with using
statement with out adding its reference in your application, in that case it wont be found. Before calling it with using
statement you need to add a reference to ur application. Right click on References
and add the Excel Interop
reference.
answered May 7, 2012 at 8:24
MisamMisam
4,3002 gold badges25 silver badges43 bronze badges
Add reference > Browse > C: > Windows > assembly > GAC > Microsoft.Office.Interop.Excel > 12.0.0.0_wasd.. > Microsoft.Office.Interop.Excel.dll
answered May 5, 2013 at 11:38
I found it located under Assemblies->Extensions in VS2013.
answered Jan 5, 2015 at 14:46
bluebunny72bluebunny72
5264 silver badges7 bronze badges
If you have Microsoft Office installed, then you should be able to add a reference to Interop.Excel.
For example, the PC I’m typing this on has MSVS 2010 C# Express and Office 2010. I can add a reference to Microsoft.Office.Interop.Excel 11.0.0.0.
‘Hope that helps
answered May 7, 2012 at 7:05
paulsm4paulsm4
113k16 gold badges135 silver badges188 bronze badges
its in the com component, named:
«Microsoft Office 14 Object Library»
answered Apr 7, 2014 at 16:40
James TanJames Tan
1,3161 gold badge14 silver badges32 bronze badges
1
I just had the same problem, but none of these answers helped me. I did find the dll on my pc in the location Mostey noted: (C:WindowsassemblyGAC_MSILMicrosoft.Office.Interop.Excel14.0.0.0__71e9bce111e9429cMicrosoft.Office.Interop.Excel.dll
), but this is not the one that was referenced in the project I was trying to get building.
The reference in our project in Visual Studio 2012 was pointing to C:Program Files (x86)Microsoft Visual Studio 11.0Visual Studio Tools for Office
. This location was empty for me, but it worked fine for everyone else. It took a number of tries, but I finally tracked down a working installer. I hope this saves others the same hassle!
—> Office Tools Bundle installer for VS2012 <—
This was located on the Office Documentation and Download page. Scroll down to Tools Downloads. There’s also currently one for VS2013.
answered Nov 13, 2014 at 21:27
thehelixthehelix
5781 gold badge6 silver badges10 bronze badges
The best option since office 2007 is using Open XML SDK for it.
We used Word.Interop but it halt sometimes, and it is not recommend for Microsoft, to use it as a server side document formatting, so Open XML SDK lets you creates word documents on DOCX and Open XML formats very easily.
It lets you going well with scability, confidence ( the files, if it is corrupted can be rebuild ), and another very fine characteristics.
answered Feb 5, 2016 at 8:19
Here is super solid solution, you just need have excell.dll in your Debug/Release folder
Mine is 77,824 bytes, I downloaded it as a file, this also explain why some people have Debug compiled but Release not or vice versa.
Trento
answered Oct 30, 2012 at 21:39
Mario TrentoMario Trento
5051 gold badge4 silver badges14 bronze badges
1.Download and install: Microsoft Office Developer Tools
2.Add references from:
C:Program Files (x86)Microsoft Visual Studio 11.0Visual Studio Tools for OfficePIAOffice15
answered Jul 24, 2015 at 15:25
I had the same issue and the Microsoft.Office.Interop was not appearing in «Add Reference» option once I upgraded VS2012 to VS2015. I basically repaired the installation (Control Panel > Programs & Features > VS 2012 > Right click Change > Repair) and added the Microsoft Office component. After that the same solution started working.
answered Nov 3, 2015 at 0:01
sk1900sk1900
2873 silver badges14 bronze badges
Make sure your project is 32 bit.
I had this problem, as soon as I ticked «Prefer 32 bit and rebuilt» all the Office Interop assemblies where available in Reference->Assemblies->Search «Office».
answered Nov 1, 2019 at 10:28
This C# tutorial handles Microsoft Excel files. It applies Microsoft.Office.Interop.Excel to read XLS files.
Excel creates XLS and XLSX files. These files are hard to read in C# programs.
They are handled with the Microsoft.Office.Interop.Excel assembly. This assembly sometimes creates performance issues. Step-by-step instructions are helpful.
Tip: Add the Microsoft.Office.Interop.Excel assembly by going to Project -> Add Reference.
Visual Studio
Interop. You must include a namespace to use Excel in your C# program. You have to use Visual Studio’s GUI to add an assembly to your program to use Excel interop. Use the Add Reference command for this.
Namespace containing Excel interop methods using Microsoft.Office.Interop.Excel;
First, we make a new C# class file in Visual Studio and you can call it something like ExcelInterop. We use that name in the examples, but you are free to use something more familiar or standard.
Tip: Add the using line to your class file—this will make the Excel namespace handy.
Excel interop methods. If you look at various Excel interop objects in IntelliSense, you will see hundreds of undocumented functions. You will only need to use a few—unless you have complex requirements.
Class that stores Application: C# /// <summary> /// This class contains the Excel Interop code we need. /// It can be shared in many places to avoid duplication. /// </summary> class ExcelReaderInterop { /// <summary> /// Store the Application object we can use in the member functions. /// </summary> Application _excelApp; /// <summary> /// Initialize a new Excel reader. Must be integrated /// with an Excel interface object. /// </summary> public ExcelReaderInterop() { _excelApp = new Application(); } }
Interop class. We need a place to store our Excel information, so we use our special class. Put an Application object called _excelApp as a member. In the constructor, make the _excelApp a new Application object.
Class
Open. To open Excel files, you must call Workbooks.Open. It is harder than it should be. Put a public function on the class, and it can use the _excelApp we already have. The following code uses the Workbooks.Open method.
Method that opens Excel workbooks: C# /// <summary> /// Open the file path received in Excel. Then, open the workbook /// within the file. Send the workbook to the next function, the internal scan /// function. Will throw an exception if a file cannot be found or opened. /// </summary> public void ExcelOpenSpreadsheets(string thisFileName) { try { // // This mess of code opens an Excel workbook. I don't know what all // those arguments do, but they can be changed to influence behavior. // Workbook workBook = _excelApp.Workbooks.Open(thisFileName, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing); // // Pass the workbook to a separate function. This new function // will iterate through the worksheets in the workbook. // ExcelScanIntenal(workBook); // // Clean up. // workBook.Close(false, thisFileName, null); Marshal.ReleaseComObject(workBook); } catch { // // Deal with exceptions. // } }
I apologize for the mess that is Workbooks.Open. This is probably the result of legacy code at Microsoft. There are many Type.Missing arguments. And don’t panic over those Type.Missing arguments. You don’t need to know what they do.
Next, open a workbook with Workbooks.Open. Send the workbook we open to another function called ExcelScanInternal. Close the workbook and release all the memory. You will need to deal with exceptions in the catch block.
Note: I don’t have the information about all the detailed exceptions that can be thrown. Just catch them in one statement.
Catch
Loop. To get data from workbooks, you will need to loop over the sheets. An Excel workbook has one or more sheets. In Excel, you can switch between sheets by clicking on the tabs on the bottom.
Tip: In Excel Interop, sheets are indexed starting at 1. This is similar to Visual Basic but not the C# language.
Method that gets sheets: C# /// <summary> /// Scan the selected Excel workbook and store the information in the cells /// for this workbook in an object[,] array. Then, call another method /// to process the data. /// </summary> private void ExcelScanIntenal(Workbook workBookIn) { // // Get sheet Count and store the number of sheets. // int numSheets = workBookIn.Sheets.Count; // // Iterate through the sheets. They are indexed starting at 1. // for (int sheetNum = 1; sheetNum < numSheets + 1; sheetNum++) { Worksheet sheet = (Worksheet)workBookIn.Sheets[sheetNum]; // // Take the used range of the sheet. Finally, get an object array of all // of the cells in the sheet (their values). You can do things with those // values. See notes about compatibility. // Range excelRange = sheet.UsedRange; object[,] valueArray = (object[,])excelRange.get_Value( XlRangeValueDataType.xlRangeValueDefault); // // Do something with the data in the array with a custom method. // ProcessObjects(valueArray); } }
In this example, the object array is directly usable in the C# language. Once you get the objects, you don’t need to do any more Interop. This provides a huge performance boost. There are benefits to reducing calls to Excel Interop.
Next: I show some benchmarks I took comparing the various techniques of Excel interop.
Performance. If you do your Excel processing wrong, you could be waiting a long time to process even 10 spreadsheets at once. Using the Cells[] indexer on a range, or getting the range with the Range property or get_Range() is slow.
Note: This document is based on .NET 3.5. The approach shown here is the fastest one.
Excel interop benchmark Cells[]: 30.0 seconds get_Range(), Cells[]: 15.0 seconds UsedRange, get_Value(): 1.5 seconds [fastest]
Compatibility. There is a compatibility issue with the Excel program on the Apple Mac OS X platform. Excel stores OA dates in different formats on Macs and PCs. The code above shows one solution, which is to use get_Value() instead of Value2.
FromOADate and Excel Dates
Note: The function get_Value() returns objects that do not vary based on the original platform.
Charts. It is also possible to create charts directly in your Excel workbooks. The ChartWizard method is useful for this. Also, you can use new features from the .NET Framework 4.0 to simplify how you use Excel.
This example uses several hard-coded constant values. The range of the data we want to chart is encoded in the topLeft and bottomRight constants. You can see we use the ChartObjects property and then Add a Chart.
Next: We set the range of the chart with SetSourceData. We use the XlChartType.xlLine enumerated constant and call ChartWizard().
Tip: Include the Microsoft.Office.Interop.Excel namespace by right-clicking on References and selecting Add Reference.
C# program that creates Excel chart from data using Microsoft.Office.Interop.Excel; class Program { const string fileName = "C:\Book1.xlsx"; const string topLeft = "A1"; const string bottomRight = "A4"; const string graphTitle = "Graph Title"; const string xAxis = "Time"; const string yAxis = "Value"; static void Main() { // Open Excel and get first worksheet. var application = new Application(); var workbook = application.Workbooks.Open(fileName); var worksheet = workbook.Worksheets[1] as Microsoft.Office.Interop.Excel.Worksheet; // Add chart. var charts = worksheet.ChartObjects() as Microsoft.Office.Interop.Excel.ChartObjects; var chartObject = charts.Add(60, 10, 300, 300) as Microsoft.Office.Interop.Excel.ChartObject; var chart = chartObject.Chart; // Set chart range. var range = worksheet.get_Range(topLeft, bottomRight); chart.SetSourceData(range); // Set chart properties. chart.ChartType = Microsoft.Office.Interop.Excel.XlChartType.xlLine; chart.ChartWizard(Source: range, Title: graphTitle, CategoryTitle: xAxis, ValueTitle: yAxis); // Save. workbook.Save(); } }
In this example, we use the named parameters and default parameters functionality in the .NET Framework 4.0. In older versions of the .NET Framework, you will need to specify the Missing value yourself.
Note: Some code contributed by Randall Kelsey was adapted for the charting example.
The program requires an Excel document named Book1.xlsx located at C:Book1.xlsx. In this file, you need to add four values in the first column. The program, upon execution, will create a chart based on those four values.
Tip: Many options can be changed to create different charts based on different data ranges.
Also: We showed how named parameters and default parameters from the .NET Framework 4.0 can clean up Microsoft.Office.Interop.Excel code.
Summary. We saw how to get started with Excel interop and avoid severe performance problems with the assembly. It is confusing to call the complicated Open method with Type.Missing in more places than necessary.
Related Links
Adjectives
Ado
Ai
Android
Angular
Antonyms
Apache
Articles
Asp
Autocad
Automata
Aws
Azure
Basic
Binary
Bitcoin
Blockchain
C
Cassandra
Change
Coa
Computer
Control
Cpp
Create
Creating
C-Sharp
Cyber
Daa
Data
Dbms
Deletion
Devops
Difference
Discrete
Es6
Ethical
Examples
Features
Firebase
Flutter
Fs
Git
Go
Hbase
History
Hive
Hiveql
How
Html
Idioms
Insertion
Installing
Ios
Java
Joomla
Js
Kafka
Kali
Laravel
Logical
Machine
Matlab
Matrix
Mongodb
Mysql
One
Opencv
Oracle
Ordering
Os
Pandas
Php
Pig
Pl
Postgresql
Powershell
Prepositions
Program
Python
React
Ruby
Scala
Selecting
Selenium
Sentence
Seo
Sharepoint
Software
Spellings
Spotting
Spring
Sql
Sqlite
Sqoop
Svn
Swift
Synonyms
Talend
Testng
Types
Uml
Unity
Vbnet
Verbal
Webdriver
What
Wpf
Table of Contents
- Background
- Prerequisites
- Source Code
Background
A few days ago we got a requirement to read Excel files and store those values in the SQL server database. So in this example, we’re going to show how to get the basic four import data such as Excel Work Book
Name, Worksheet Count in that Workbook, Name of the First Worksheet, and finally the value of the first cell in that worksheet.
Prerequisites
Kindly ensure you add the following DLL as shown in the below screenshot,
Namespace
using Excel = Microsoft.Office.Interop.Excel;
Source Code
C# Code
- protected void BtnGetExcelFileDetails_Click(object sender, EventArgs e)
- {
- try
- {
- Excel.Application oExcel = new Excel.Application();
- string filepath = @ «D:TPMSUploaded_BoqRaveena_boq_From_Db.xlsx»;
- Excel.Workbook WB = oExcel.Workbooks.Open(filepath);
- string ExcelWorkbookname = WB.Name;
- int worksheetcount = WB.Worksheets.Count;
- Excel.Worksheet wks = (Excel.Worksheet) WB.Worksheets[1];
- string firstworksheetname = wks.Name;
- var firstcellvalue = ((Excel.Range) wks.Cells[1, 1]).Value;
- } catch (Exception ex)
- {
- string error = ex.Message;
- }
- }
Hope the above information was useful.