在C#Excel Addin中调用AddLabel时出错:System.Runtime.InteropServices.COMException(0x800A03EC)

时间:2018-01-10 07:44:17

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

我有一个Addin,当我尝试添加一个Label to addin时请遵循以下指南: 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)

有谁知道这个问题?

3 个答案:

答案 0 :(得分:0)

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

例如:

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);
    }
}

答案 1 :(得分:0)

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

  1. 创建一个最小的工作示例,重现问题
  2. 检查哪个Excel版本有此问题。
  3. 检查是否依赖于构建模式(调试/发布)
  4. 检查是否依赖于附加的调试器(尝试从Visual Studio启动它而不进行调试)
  5. 检查是否因为安装(但从命令行启动Excel)
  6. 检查开始测试会话时是否没有僵尸Excel进程
  7. 检查Excel中是否只启用了一个版本的插件。
  8. 如果没有任何帮助,请回到这里,发现新的事实。 Excel互操作是一个区域,有时候没有现成的答案,因为你可能是第一个遇到这个问题并尝试解决它的人。幸运的是,几乎总能找到自己的解决方案。

答案 2 :(得分:0)

检查是否在设置中禁用了 Activex Addins
对于 Excel 2016,请转到:File->Trust Centre->Trust Centre Settings

如果有用户选择了,

<块引用>

禁用所有控件而不通知。

然后当我们给工作表excel添加控件时会报这个错误,

<块引用>

无法插入对象。