繁体   English   中英

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

[英]using Excel = Microsoft.Office.Interop.Excel Compilation Error

在我的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相同版本的办公室或更好地使用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

如果我导出互操作类型,就会发生这种情况。 如果错误是关于Excel.Application那么在公共类上查找返回Application引用的公共方法或属性,或者将Application引用作为参数(任何使公共事件类型可见的公共类)。

通过使它成为privateinternal隐藏该类,属性或方法,或者不返回该类型,或者将interop类型包装在您自己的包装类中并返回它(相反,这是更好的实践。)

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM