【问题标题】:COMException C# Microsoft.Office.Interop.ExcelCOMException C# Microsoft.Office.Interop.Excel
【发布时间】:2014-09-22 23:46:49
【问题描述】:

我正在尝试解决我遇到的 COMException 问题。这是我的代码:

错误发生在 Workbook Original = new Workbook(result[0]);

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using MahApps.Metro.Controls;
using MahApps.Metro;
using Microsoft.Win32;
using System.Windows.Forms;

using System.Data;
using Microsoft.Office.Interop.Excel;




namespace KPI_Generator_v3
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : MetroWindow
{

    string [] result;
    Microsoft.Win32.OpenFileDialog dlg = new Microsoft.Win32.OpenFileDialog();


    public MainWindow()
    {
        InitializeComponent();
    }

    private void exit_Click(object sender, RoutedEventArgs e)
    {
        this.Close();

    }

    private void browse_Click(object sender, RoutedEventArgs e)
    {

        // Create OpenFileDialog 
        instructionslbl.Visibility = Visibility.Hidden;
        dlg.Multiselect = true;
        dlg.ShowDialog();
        result = dlg.FileNames;
        dlg.DefaultExt = ".xls";
        dlg.Filter = "XLS Files (*.xls)|*.xls";

        foreach (string fileName in result)
        {
            displayFilesBox.Text += fileName + System.Environment.NewLine;

        }

        SelectedFileslbl.Visibility = Visibility.Visible;
        displayFilesBox.Visibility = Visibility.Visible;
        generateBtn.Visibility = Visibility.Visible;

    }

    private void generate_Click(object sender, RoutedEventArgs e)
    {
        Workbook Original = new Workbook(result[0]);
        for (int i = 1; i <= result.Length; i++)
        {

            Workbook next = new Workbook(result[i]);
            Worksheet newOGsheet = Original.Worksheets.Add();
            Worksheet nextSheet = next.Worksheets[0];
            nextSheet.Copy(newOGsheet);
        }



    }


    }
 }

现在.. 虽然上面的代码可能无法正常工作,但我收到一个错误提示

“System.Runtime.InteropServices.COMException”发生在 mscorlib.dll 中

附加信息说明

HRESULT: 0x80040154 (REGDB_E_CLASSNOTREG))

在谷歌上搜索了一段时间后,我下载了dependency walker,并得到以下输出:

我不知道我在做什么,我们将不胜感激!谢谢

编辑:HKEY CLASSES_ROOT 的 REGEDIT 图片(如果需要)

EDIT2:错误图片

【问题讨论】:

  • 你在哪一行得到错误?从代码中我可以在这里看到一个错误 for (int i = 1; i &lt;= result.Length; i++) 。我认为需要将for (int i = 1; i &lt; result.Length; i++) 通知&lt;= 更改为&lt;。您超过了结果数组 1。
  • 错误在第 74 行 @ Workbook Original = new Workbook(result[0]);我相信我的系统没有正确注册的东西。当我尝试 regsvr32 Microsoft.Office.Interop.Excel.dll 时出现错误。另外,我在 HKEY CLASSES_ROOT 值处发布了一张 regedit 的图片
  • 顺便说一下,当您使用 Office 互操作时,这些类型的异常似乎很常见。您可能想要切换到 Open Office XML SDKEPPlus,因为它们完全在 .NET 框架中,并且不依赖 COM 与外部库进行通信。
  • 您不能自己创建 WorkBook 对象,您必须使用工厂函数。请改用 Application.WorkBooks.Add()。顺便说一句,在 Office 对象模型中非常常见。有大量示例代码可帮助避免此类简单错误。

标签: c# excel comexception


【解决方案1】:

不幸的是,这个错误是一个 COM 互操作缺陷。否则,你会得到一个编译错误,因为Workbook 是一个接口并且没有构造函数。此接口不打算实例化,这就是您收到此异常的原因。 (不知道你为什么还要在这里传递文件名......)

要解决此问题,请使用Microsoft.Office.Interop.Excel.Application:

using Microsoft.Office.Interop.Excel;

Application excelApp = new Application();
excelApp.Workbooks.Open(result[0]);

其他说明:

  • 您应该最后拨打dlg.ShowDialog() - 这样您的过滤器才会生效。
  • 对于您的过滤器,我建议为 .xlsx 文件添加一个选项,以支持较新的 Excel 文件
  • 在使用之前添加result.Length &gt; 0,只是为了确保用户确实打开了一些东西。
  • 将 for 循环条件更改为 i &lt; result.Length;否则,你会得到一个IndexOutOfRangeException

另外,只是为了添加更多信息:您可能会问:“但是等等,ryrich,Application 也是一个接口!为什么我可以实例化 it 而不是 Workbook? "

我也想知道这个。显然这是有效的,因为它装饰有CoClassAttribute(和一些其他属性)。有关这方面的更多详细信息,请参阅 this 堆栈溢出帖子。

【讨论】:

  • @MichaelPetch 好的,我会添加进去的。
  • 看起来不错。在我们两个之间,我们在您的回答中涵盖了所有内容。我已经删除了我的(即使它有赞成票)。我们可以专注于您的回答。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2018-07-02
  • 1970-01-01
  • 1970-01-01
  • 2012-12-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多