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

在我的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 

它可能是什么?

你有MS 2010的组件,但你已经安装了Office 2013.组件不是向前兼容的。 您必须安装与您的DLL相同的版本的Office,或者更好地使用OpenXml sdk。

然后,看起来缺less一些必要的组件。 您将不得不添加对组件的引用。

无法find匹配embedded式互操作types“'的互操作types。 你是否缺lessassembly参考? 这种方法类似于以前的错误,因为如果一个程序集embeddedtypes信息而另一个程序集没有。 在这种情况下,您有一个程序集assembly1,它将Embed Interop Types的PIA程序集设置为true。 然后,Assembly1将从PIA程序集中公开一个types,例如从方法或属性返回types。 另一个程序集assembly2引用assembly1并使用embeddedtypes。 如果assembly2不引用PIA程序集,并因此无法findembedded的types信息,则会出现错误。 要解决此问题,您需要将第二个程序集的引用添加到PIA程序集,并将Embed Interop Types属性设置为true。 因此,两个组件都将参考PIA组件,embeddedtypes信息,并且都可以使用embedded式types。

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

如果我导出互操作types,这发生在我身上。 如果错误与Excel.Application有关,则在公共类上查找返回Application引用或具有Application引用作为参数的公共方法或属性(公开的任何使交互types可见的方法)。

或者隐藏这个类,属性或者方法,使其成为private或者internal ,或者不返回这个types,或者把这个互操作types封装在你自己的包装类中,然后返回它(这是更好的做法)。

将参考dll文件添加到Bin文件夹将有助于解决此问题。