C# 在 .NET 中使用 Microsoft.Office.Interop.Excel 需要什么参考?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10477977/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
What reference do I need to use Microsoft.Office.Interop.Excel in .NET?
提问by user850010
I am interested in using C# to manipulate/Automate Excel files.
我对使用 C# 操作/自动化 Excel 文件很感兴趣。
After browsing the web I have found VSTObut it seems you can not use that in Visual Studio Express Edition so I can not use that.
浏览网页后,我找到了VSTO,但您似乎无法在 Visual Studio Express Edition 中使用它,因此我无法使用它。
Just few minutes ago I noticed a question in this site that used this namespace in its code:
就在几分钟前,我注意到这个站点中的一个问题在其代码中使用了这个命名空间:
Microsoft.Office.Interop.Excel
微软.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.
我安装了“主要互操作程序集”作为已接受的答案,但由于某种原因,“.NET”下的“添加引用”对话框中仍然缺少它们,但它们存在于 GAC 中。
So I just added the reference using "Browse" and located Microsoft.Office.Interop.Excel.dllin GAC folder.
所以我只是使用“浏览”添加了引用并位于Microsoft.Office.Interop.Excel.dllGAC 文件夹中。
But looking for similar questions on this site regarding adding reference from GAC it seems this is not recommended.
但是在此站点上寻找有关从 GAC 添加参考的类似问题似乎不推荐这样做。
采纳答案by moribvndvs
Update (thanks user2347528)
更新(感谢 user2347528)
These assemblies are available as NuGet packages, which is much easier than my original answer.
这些程序集可用作 NuGet 包,这比我原来的答案要容易得多。
You can install by either right clicking on Referencesin your project and selecting Manage NuGet packages...and searching for one of the packages listed below, or install using the Package Manager Console:
您可以通过右键单击项目中的“引用”并选择“管理 NuGet 包...”并搜索下面列出的包之一来安装,或者使用包管理器控制台进行安装:
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
- 微软.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.
它们可以作为“主要互操作程序集”使用,可以与 Office 一起安装,也可以单独下载和安装。如何:安装 Office 主互操作程序集。
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).
安装后,您可以在 .NET 下的“添加引用”对话框中的项目中引用它们。如果您没有看到列出的那些 Microsoft.Office.Interop 程序集,则它们尚未安装。从您的设置中安装它们,或单独下载并安装它们(有关下载,请参阅上面的链接)。
回答by paulsm4
If you have Microsoft Office installed, then you should be able to add a reference to Interop.Excel.
如果您安装了 Microsoft Office,那么您应该能够添加对 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.
例如,我正在输入的 PC 具有 MSVS 2010 C# Express 和 Office 2010。我可以添加对 Microsoft.Office.Interop.Excel 11.0.0.0 的引用。
'Hope that helps
'希望有帮助
回答by RAKESH HOLKAR
Just add reference of Microsoft.Office.Interop.Excel.
只需添加Microsoft.Office.Interop.Excel.
It includes MicroSoft Excel related classes, no need to add more references.
包含MicroSoft Excel相关类,无需添加更多引用。
回答by Misam
I guess what you are trying to do is add Microsoft.Office.Interop.Excelwith usingstatement with out adding its reference in your application, in that case it wont be found. Before calling it with usingstatement you need to add a reference to ur application. Right click on Referencesand add the Excel Interopreference.
我猜你想要做的是添加Microsoft.Office.Interop.Excelwithusing语句而不在你的应用程序中添加它的引用,在这种情况下它不会被找到。在使用using语句调用它之前,您需要添加对您的应用程序的引用。右键单击References并添加Excel Interop引用。
回答by Mario Trento
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.
这是超级可靠的解决方案,您只需要在 Debug/Release 文件夹中有 Excell.dll 我是 77,824 字节,我将其下载为文件,这也解释了为什么有些人编译了 Debug 而 Release 没有,反之亦然。
Trento
特伦托
回答by Robert
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
Add reference > Browse > C: > Windows > assembly > GAC > Microsoft.Office.Interop.Excel > 12.0.0.0_etc > Microsoft.Office.Interop.Excel.dll
回答by Turanian
Add reference > Browse > C: > Windows > assembly > GAC > Microsoft.Office.Interop.Excel > 12.0.0.0_wasd.. > Microsoft.Office.Interop.Excel.dll
添加引用 > 浏览 > C: > Windows > 程序集 > GAC > Microsoft.Office.Interop.Excel > 12.0.0.0_wasd.. > Microsoft.Office.Interop.Excel.dll
回答by James Tan
its in the com component, named: "Microsoft Office 14 Object Library"
它在 com 组件中,名为:“Microsoft Office 14 Object Library”
回答by 0x8BADF00D
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:\Windows\assembly\GAC_MSIL\Microsoft.Office.Interop.Excel\14.0.0.0__71e9bce111e9429c\Microsoft.Office.Interop.Excel.dll
答案并没有帮助我解决我的问题,尽管我使用 Microsoft 的 msi 安装程序安装了它们,但我无法找到(和浏览)这些程序集。对我来说,excel程序集位于C:\Windows\assembly\GAC_MSIL\Microsoft.Office.Interop.Excel\14.0.0.0__71e9bce111e9429c\Microsoft.Office.Interop.Excel.dll
回答by thehelix
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:\Windows\assembly\GAC_MSIL\Microsoft.Office.Interop.Excel\14.0.0.0__71e9bce111e9429c\Microsoft.Office.Interop.Excel.dll), but this is not the one that was referenced in the project I was trying to get building.
我只是遇到了同样的问题,但这些答案都没有帮助我。我确实在我的电脑上找到了 dll,位于 Mostey 指出的位置:( C:\Windows\assembly\GAC_MSIL\Microsoft.Office.Interop.Excel\14.0.0.0__71e9bce111e9429c\Microsoft.Office.Interop.Excel.dll),但这不是我试图构建的项目中引用的那个。
The reference in our project in Visual Studio 2012 was pointing to C:\Program Files (x86)\Microsoft Visual Studio 11.0\Visual 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!
我们在 Visual Studio 2012 项目中的引用指向C:\Program Files (x86)\Microsoft Visual Studio 11.0\Visual Studio Tools for Office\. 这个位置对我来说是空的,但对其他人来说效果很好。尝试了很多次,但我终于找到了一个可以工作的安装程序。我希望这可以为其他人节省同样的麻烦!
--> Office Tools Bundle installer for VS2012<--
--> 适用于 VS2012 的 Office Tools Bundle 安装程序<--
This was located on the Office Documentation and Downloadpage. Scroll down to Tools Downloads. There's also currently one for VS2013.
这位于Office 文档和下载页面上。向下滚动到工具下载。目前还有一个适用于 VS2013。

