使用Excel = Microsoft.Office.Interop.Excel编译错误

Bry*_*yuk 4 .net c# asp.net-mvc excel office-interop

在我的MVC 5应用程序中,我想使用Microsoft.Office.Interop.Excel库.我使用Visual Studio 2013和Office 2013.我添加了一个引用,Microsoft.Excel 15.0 Object Library并在我的类中添加了using Excel = Microsoft.Office.Interop.Excel;当我单击build它时没有显示任何错误并告诉它Build Complete,但是当我运行我的应用程序时我收到此错误

 Compilation Error
Description: An error occurred during the compilation of a resource required to service this request. Please review the following specific error details and modify your source code appropriately.

Compiler Error Message: CS1748: Cannot find the interop type that matches the embedded interop type 'Microsoft.Office.Interop.Excel.Application'. Are you missing an assembly reference?

Source Error:

[No relevant source lines]

Source File:    Line: 0 
Run Code Online (Sandbox Code Playgroud)

可能是什么?

Jer*_*vak 9

您有MS 2010的程序集,但您已安装Office 2013.程序集不向前兼容.您必须安装与您的DLL相同版本的办公室或更好地使用OpenXml sdk之类的版本.

然后它看起来缺少一些必要的装配.您必须添加对程序集的引用.

找不到与嵌入式互操作类型匹配的互操作类型''.你错过了装配参考吗?此方法类似于先前的错误,因为如果一个程序集嵌入类型信息而另一个程序集不嵌入,则会发生此方法.在这种情况下,您有一个程序集assembly1,它引用具有Embed Interop Types的PIA程序集设置为true.然后Assembly1从PIA程序集中公开一个类型,例如作为方法或属性的返回类型.另一个程序集assembly2引用assembly1并使用嵌入类型.如果assembly2也没有引用PIA程序集,则会发生错误,因此无法找到嵌入的类型信息.要解决此问题,您需要将第二个程序集中的引用添加到PIA程序集,并将Embed Interop Types属性设置为true.因此,两个程序集都将引用PIA程序集,其中嵌入了类型信息,并且两者都可以使用嵌入类型.

http://blogs.msdn.com/b/vbteam/archive/2010/06/11/troubleshooting-errors-when-embedding-type-in​​formation-doug-rothaus.aspx