111

我对使用 C# 来操作/自动化 Excel 文件很感兴趣。

浏览网页后,我找到了VSTO,但似乎您不能在 Visual Studio Express Edition 中使用它,所以我不能使用它。

就在几分钟前,我注意到这个站点中的一个问题在其代码中使用了这个命名空间:

Microsoft.Office.Interop.Excel

所以我想知道我是否只需要添加必要的引用,如果需要,添加哪个引用?

更新

我按照接受的答案建议安装了“主互操作程序集”,但由于某种原因,“.NET”下的“添加引用”对话框中仍然缺少它们,但它们存在于 GAC 中。

所以我只是使用“浏览”添加了引用并位于Microsoft.Office.Interop.Excel.dllGAC 文件夹中。

但是在这个网站上寻找关于添加来自 GAC 的参考的类似问题似乎不建议这样做。

4

15 回答 15

108

更新(感谢 user2347528)

这些程序集以 NuGet 包的形式提供,这比我原来的答案容易得多。

您可以通过右键单击项目中的引用并选择管理 NuGet 包...并搜索下面列出的包之一来安装,或者使用包管理器控制台进行安装:

PM> Install-Package Microsoft.Office.Interop.Excel

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).

于 2012-05-07T07:03:55.007 回答
50

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

于 2013-03-14T14:25:53.000 回答
24

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

于 2014-07-24T06:37:53.633 回答
8

Just add reference of Microsoft.Office.Interop.Excel.

It includes MicroSoft Excel related classes, no need to add more references.

于 2012-05-07T07:06:59.153 回答
4

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.

于 2012-05-07T08:24:33.580 回答
4

Add reference > Browse > C: > Windows > assembly > GAC > Microsoft.Office.Interop.Excel > 12.0.0.0_wasd.. > Microsoft.Office.Interop.Excel.dll

于 2013-05-05T11:38:20.093 回答
4

I found it located under Assemblies->Extensions in VS2013.

Add Reference

于 2015-01-05T14:46:38.820 回答
3

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

于 2012-05-07T07:05:59.283 回答
3

its in the com component, named: "Microsoft Office 14 Object Library"

于 2014-04-07T16:40:52.240 回答
3

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.

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!

--> 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.

于 2014-11-13T21:27:33.600 回答
3

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.

于 2016-02-05T08:19:45.590 回答
2

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

于 2012-10-30T21:39:56.627 回答
1

1.Download and install: Microsoft Office Developer Tools

2.Add references from:

C:\Program Files (x86)\Microsoft Visual Studio 11.0\Visual Studio Tools for Office\PIA\Office15

于 2015-07-24T15:25:19.313 回答
1

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.

于 2015-11-03T00:01:08.637 回答
0

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".

于 2019-11-01T10:28:20.337 回答