Cannot be embbedded. Use the applicable interface instead
本问题已经有最佳答案,请猛点这里访问。
我正在尝试将照片添加到Excel Spread表但仍然收到以下错误?
Error 1 Interop type 'Microsoft.Office.Interop.Excel.ApplicationClass'
cannot be embedded. Use the applicable interface instead.
ApplicationClass(); 在下面的代码行中以红色加下划线:
xlApp = new Excel.ApplicationClass();
有人可以告诉我如何解决这个问题?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 | 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(); } } } |
在您的项目中,展开"参考",找到Microsoft Office Interop参考。右键单击它并选择属性,然后将"嵌入互操作类型"更改为
正如MSDN博客文章中所解释的那样,您也可以更改"禁用互操作类型",而不是禁用"嵌入互操作类型"
1 |
成
1 |
虽然
使用此方法(Embed Interop Types = true)的优势在于,您需要在项目中部署较少的文件,嵌入式类型将仅包含应用程序实际使用的方法和类型。使用外部互操作程序集时,将导入引用库公开的所有类型和方法。
找到Microsoft.Office.Interop.Excel参考并右键单击并更改"嵌入互操作类型"状态,如果为真,则为falce。