【问题标题】:Error when calling AddLabel inside C# Excel Addin: System.Runtime.InteropServices.COMException (0x800A03EC)在 C# Excel 插件中调用 AddLabel 时出错:System.Runtime.InteropServices.COMException (0x800A03EC)
【发布时间】:2018-01-10 07:44:16
【问题描述】:

我有一个插件,当我尝试将标签添加到插件时,请遵循以下指南: https://msdn.microsoft.com/en-us/library/cc442817.aspx

我收到了这个错误:

System.Runtime.InteropServices.COMException (0x800A03EC): Cannot insert object.
   at System.RuntimeType.ForwardCallToInvokeMember(String memberName, BindingFlags flags, Object target, Int32[] aWrapperTypes, MessageData& msgData)
   at Microsoft.Office.Interop.Excel.Shapes.AddOLEObject(Object ClassType, Object Filename, Object Link, Object DisplayAsIcon, Object IconFileName, Object IconIndex, Object IconLabel, Object Left, Object Top, Object Width, Object Height)
   at Microsoft.Office.Tools.Excel.ControlCollectionImpl.CreateWrapperAndGetCookie(String name, Double left, Double top, Double width, Double height, Boolean anchor, _OLEObject& outObject)
   at Microsoft.Office.Tools.Excel.ControlCollectionImpl.InternalAddOtherControl(Control control, Double left, Double top, Double width, Double height, String name, Boolean anchor)
   at Microsoft.Office.Tools.Excel.ControlCollectionImpl.AddControl(Control control, Double left, Double top, Double width, Double height, String name)
   at Microsoft.Office.Tools.Excel.ControlExtensions.AddLabel(

我添加标签的代码:

excelLabel = Microsoft.Office.Tools.Excel.ControlExtensions.AddLabel(
        vstoDocument.Controls,
        left,
        top,
        height,
        width,
        myLabelName);

我在 Stack Overflow 上发现了一些相同的错误代码但不同的消息答案。我试过了,但还是不行。

此错误仅发生在生产模式(已安装)上。它在开发模式下正常工作。 (我用的是office 365和windows 8)

有人知道这个问题吗?

【问题讨论】:

    标签: c# vsto office-interop ole excel-addins


    【解决方案1】:

    尝试使用 EPPlus 框架,它允许您以纯粹的方式将数据发送到 Excel 文件,无需互操作。

    例如:

    private void ExportToExcelFile(DataTable table, string filename)
    {
        string fileNameOut = filename; // Environment.CurrentDirectory + "\\Exportation.xlsx";
    
        if (File.Exists(@fileNameOut)) File.Delete(@fileNameOut);       // If the file exist, then detele the file
        FileInfo newFile = new FileInfo(@fileNameOut);
    
        ExcelPackage package = new ExcelPackage(newFile);
        var wsDt = package.Workbook.Worksheets.Add(string.Format("Data Exported"));
        // Load the datatable and set the number formats...
        wsDt.Cells["A1"].LoadFromDataTable(table, true, TableStyles.Light9);
    
    
        var headerCells = wsDt.Cells[1, 1, 1, wsDt.Tables[0].Columns.Count];
        var headerFont = headerCells.Style.Font;
        headerFont.Bold = true;  //headerFont.Italic = true;
        //headerFont.SetFromFont(new Font("Calibri", 12, FontStyle.Bold));   //headerFont.Color.SetColor(Color.DarkBlue);
    
        // Disable Autofilter
        wsDt.Tables[0].ShowFilter = false;
    
        // Formating numeric type or date type
        // wsDt.Column(1).Style.Numberformat.Format = "dd/mm/yyyy";     // Column: TimeStamp "yyyy-mm-dd"
    
        // AutoFitColumns ALL columns
        for (int i = 1; i <= wsDt.Tables[0].Columns.Count; i++)
            wsDt.Column(i).AutoFit();
        /*
        wsDt.Column(1).AutoFit();   // TimeStamp
        wsDt.Column(2).AutoFit();   // FIRSTNAME
        wsDt.Column(3).AutoFit();   // LASTNAME
        wsDt.Column(4).AutoFit();   // TRANSPORT
        wsDt.Column(5).AutoFit();   // MODE
        wsDt.Column(6).AutoFit();   // MODECODE
        wsDt.Column(7).AutoFit();   // OBSERVATIONS
        */
    
        // set some document properties
        package.Workbook.Properties.Title = "Data Export";
        package.Workbook.Properties.Author = "My Company LLC";
        package.Workbook.Properties.Comments = "Data Exported to Excel (OpenXML Format)";
    
        // set some extended property values
        package.Workbook.Properties.Company = "My Company LLC";
    
        // save the file
        package.Save();
        //Console.WriteLine("Exported data {0}...");
        //log.Info(string.Format("Exported data: {0}", airlinename));
    }
    
    
    
    private void btnExportToExcel_Click(object sender, EventArgs e)
    {
        bool bandExport_ddMMyyyy = false;
        //show a file save dialog and ensure the user selects
        //correct file to allow the export
        saveFileDialog1.Title = "Export to Excel file";
    
        saveFileDialog1.Filter = "Microsoft Excel Book 2010-2013(*.xlsx)|*.xlsx";
        if (saveFileDialog1.ShowDialog() == DialogResult.OK)
        {
            if (MessageBox.Show("¿You wish export in format 'dd/MM/yyyy'? (By default, the TimeStamp values will not be format).", Toolbox.AssemblyTitle, MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
                bandExport_ddMMyyyy = true;
    
            if (!saveFileDialog1.FileName.Equals(String.Empty))
            {
                FileInfo file = new FileInfo(saveFileDialog1.FileName);
                if (file.Extension.Equals(".xlsx"))
                {
                    // Determine the columns to be exported.
                    DataTable dataResultsToExport = new DataTable();
                    foreach (DataGridViewColumn col in dataGridView1.Columns)
                        dataResultsToExport.Columns.Add(col.HeaderText);
    
                    // Copy the data.
                    foreach (DataGridViewRow row in dataGridView1.Rows)
                    {
                        DataRow dRow = dataResultsToExport.NewRow();
                        foreach (DataGridViewCell cell in row.Cells)
                        {
                            // If the column is TimeStamp, we can format it as we want, dd/MM/yyyy.
                            if (cell.ColumnIndex == 0 && bandExport_ddMMyyyy)
                                dRow[cell.ColumnIndex] = String.Format("{0:dd/MM/yyyy}", cell.Value);
                            else
                                dRow[cell.ColumnIndex] = cell.Value;
                        }
                        dataResultsToExport.Rows.Add(dRow);
                    }
    
                    this.ExportToExcelFile(dataResultsToExport, saveFileDialog1.FileName);
    
                    MessageBox.Show("The exportation works fine!", Toolbox.AssemblyTitle, MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
                else
                    MessageBox.Show("Invalid type file, please select the correct type file.", Toolbox.AssemblyTitle, MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
            }
            else
                MessageBox.Show("Spicify the file directory!", Toolbox.AssemblyTitle, MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
        }
    }
    

    【讨论】:

    • 感谢您的回答,但我正在开发 Excel 插件(不向 excel 发送数据)。
    【解决方案2】:

    我不认为你会在这里得到答案。但你可以自己找到。在你的情况下,我会做这些事情:

    1. 创建一个最小的工作示例,重现问题
    2. 检查哪个 Excel 版本存在此问题。
    3. 检查它是否依赖于构建模式(调试/发布)
    4. 检查它是否依赖于附加的调试器(尝试从 Visual Studio 启动它而不进行调试)
    5. 检查是否因为安装(但从命令行启动Excel)
    6. 开始测试会话时检查是否没有僵尸 Excel 进程
    7. 检查是否在 Excel 中仅启用了一个版本的插件。

    如果这些都没有帮助,回到这里发现新的事实。 Excel 互操作是一个领域,有时没有现成的答案,因为您可能是第一个遇到这个问题并试图解决它的人。幸运的是,几乎总是可以自己找到解决方案。

    【讨论】:

      【解决方案3】:

      检查Activex Addins 是否在设置中被禁用。
      对于 Excel 2016,请转到:File-&gt;Trust Centre-&gt;Trust Centre Settings

      如果有用户选择,

      在不通知的情况下禁用所有控件。

      那么我们在工作表中添加控件的时候excel就会报这个错误,

      无法插入对象。

      【讨论】:

        猜你喜欢
        • 2013-03-07
        • 2011-05-23
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-03-21
        • 1970-01-01
        相关资源
        最近更新 更多