c# - 无法嵌入。改用适用的接口(interface)

标签 c#

<分区>

我正在尝试将照片添加到 Excel 电子表格,但一直出现以下错误?

Error 1 Interop type 'Microsoft.Office.Interop.Excel.ApplicationClass' cannot be embedded. Use the applicable interface instead.

应用类();在下面的代码行中用红色下划线标出:

xlApp = new Excel.ApplicationClass();

有人可以告诉我如何解决这个问题吗?

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 引用。右键单击它并选择属性,并将“嵌入互操作类型”更改为 false

关于c# - 无法嵌入。改用适用的接口(interface),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11688761/

相关文章:

c# - 具有强类型索引的多维数组的最佳混合方法

c# - 如何解决 Entity Framework 提供程序异常

c# - 使用 Roslyn 生成代码时,如何在一行中获取自动属性?

c# - 是否有显示哪些方法没有 XML 文档的 Visual Studio 扩展?

c# - NUnit - 如何测试实现特定接口(interface)的所有类

c# - 实现人工神经网络时,是否应忽略输入层?

c# - 服务器端的 Blazor : using MongoDB for Identity management

c# - BitmapImage:如何在返回图像之前等待 BitmapImage 初始化? (C#.NET)

c# - 分页无法正常工作

c# - StreamGeometry 与 WPF C# 中的 DrawingContext.DrawLine