C# 无法嵌入。改用适用的接口
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11688761/
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
Cannot be embedded. Use the applicable interface instead
提问by Pomster
I am trying to add a photo to a Excel Spread sheet but keep getting the following error?
我正在尝试将照片添加到 Excel 电子表格,但一直收到以下错误?
Error 1 Interop type 'Microsoft.Office.Interop.Excel.ApplicationClass' cannot be embedded. Use the applicable interface instead.
错误 1 无法嵌入互操作类型“Microsoft.Office.Interop.Excel.ApplicationClass”。请改用适用的接口。
ApplicationClass(); is underlined in red in the line of code below:
应用类();在下面的代码行中用红色下划线表示:
xlApp = new Excel.ApplicationClass();
xlApp = new Excel.ApplicationClass();
Could Some on please tel me how i could fix this?
可以请告诉我如何解决这个问题吗?
using Excel = Microsoft.Office.Interop.Excel;
using Microsoft.Office.Core;
private void btnWriteSpreedSheet_Click(object sender, EventArgs e)
{
Excel.Application xlApp;
Excel.Workbook xlWorkBook;
Excel.Worksheet xlWorkSheet;
object misValue = System.Reflection.Missing.Value;
xlApp = new Excel.ApplicationClass(); //This is where the problem is??????
xlWorkBook = xlApp.Workbooks.Add(misValue);
xlWorkSheet = (Excel.Worksheet)xlWorkBook.Worksheets.get_Item(1);
//add some text
xlWorkSheet.Cells[1, 1] = "http://csharp.net-informations.com";
xlWorkSheet.Cells[2, 1] = "Adding picture in Excel File";
xlWorkSheet.Shapes.AddPicture("C:\csharp-xl-picture.JPG", Microsoft.Office.Core.MsoTriState.msoFalse, Microsoft.Office.Core.MsoTriState.msoCTrue, 50, 50, 300, 45);
xlWorkBook.SaveAs("csharp.net-informations.xls", Excel.XlFileFormat.xlWorkbookNormal, misValue, misValue, misValue, misValue, Excel.XlSaveAsAccessMode.xlExclusive, misValue, misValue, misValue, misValue, misValue);
xlWorkBook.Close(true, misValue, misValue);
xlApp.Quit();
releaseObject(xlApp);
releaseObject(xlWorkBook);
releaseObject(xlWorkSheet);
MessageBox.Show ("File created !");
}
private void releaseObject(object obj)
{
try
{
System.Runtime.InteropServices.Marshal.ReleaseComObject(obj);
obj = null;
}
catch (Exception ex)
{
obj = null;
MessageBox.Show("Unable to release the Object " + ex.ToString());
}
finally
{
GC.Collect();
}
}
}
采纳答案by vcsjones
In your Project, expand the "References", find the Microsoft Office Interop reference. Right click it and select properties, and change "Embed Interop Types" to false.
在您的项目中,展开“参考”,找到 Microsoft Office Interop 参考。右键单击它并选择属性,然后将“嵌入互操作类型”更改为false.
回答by yms
As explained in a MSDN blog post, instead of disabling "Embed Interop Types" you can also change
正如MSDN 博客文章中所解释的,您还可以更改而不是禁用“嵌入互操作类型”
xlApp = new Excel.ApplicationClass();
into
进入
xlApp = new Excel.Application();
Although Excel.Applicationis an interface, we can instantiate it because it is decorated with a CoClass attribute, as explained in this other SO answer: https://stackoverflow.com/a/11039870/501196
虽然Excel.Application是一个接口,但我们可以实例化它,因为它用 CoClass 属性装饰,如另一个 SO 答案中所述:https: //stackoverflow.com/a/11039870/501196
Using this approach (Embed Interop Types = true) has the advantage that you will need to deploy less files with your project, and the embedded types will only contain the methods and types that your application is actually using. When you use external interop assemblies, you are importing there all the types and methods exposed by the referenced library.
使用这种方法 (Embed Interop Types = true) 的优点是您需要在项目中部署更少的文件,并且嵌入的类型将只包含您的应用程序实际使用的方法和类型。当您使用外部互操作程序集时,您将在那里导入引用库公开的所有类型和方法。
回答by RDVN
Find Microsoft.Office.Interop.Excel Reference and right click and change "Embed Interop Types" state falce if its true.
找到 Microsoft.Office.Interop.Excel Reference 并右键单击并更改“Embed Interop Types”状态为真。

