COMException C#Microsoft.Office.Interop.Excel

COMException C#Microsoft.Office.Interop.Excel,c#,excel,comexception,C#,Excel,Comexception,我试图解决一个问题,我有一个共同的例外。这是我的代码: 工作簿原始=新工作簿时出错(结果[0]) 使用系统; 使用System.Collections.Generic; 使用System.Linq; 使用系统文本; 使用System.Threading.Tasks; 使用System.Windows; 使用System.Windows.Controls; 使用System.Windows.Data; 使用System.Windows.Documents; 使用System.Windows.Inp

我试图解决一个问题,我有一个共同的例外。这是我的代码:

工作簿原始=新工作簿时出错(结果[0])

使用系统;
使用System.Collections.Generic;
使用System.Linq;
使用系统文本;
使用System.Threading.Tasks;
使用System.Windows;
使用System.Windows.Controls;
使用System.Windows.Data;
使用System.Windows.Documents;
使用System.Windows.Input;
使用System.Windows.Media;
使用System.Windows.Media.Imaging;
使用System.Windows.Navigation;
使用System.Windows.Shapes;
使用MahApps.Metro.Controls;
使用MahApps.Metro;
使用Microsoft.Win32;
使用System.Windows.Forms;
使用系统数据;
使用Microsoft.Office.Interop.Excel;
命名空间KPI_生成器_v3
{
/// 
///MainWindow.xaml的交互逻辑
/// 
公共部分类主窗口:MetroWindow
{
字符串[]结果;
Microsoft.Win32.OpenFileDialog dlg=新的Microsoft.Win32.OpenFileDialog();
公共主窗口()
{
初始化组件();
}
私有无效退出\单击(对象发送者,路由目标e)
{
这个。关闭();
}
私有无效浏览\单击(对象发送者,路由目标)
{
//创建OpenFileDialog
指令slbl.Visibility=可见性.Hidden;
dlg.Multiselect=true;
dlg.ShowDialog();
结果=dlg.filename;
dlg.DefaultExt=“.xls”;
dlg.Filter=“XLS文件(*.XLS)|*.XLS”;
foreach(结果中的字符串文件名)
{
displayFileBox.Text+=fileName+System.Environment.NewLine;
}
SelectedFileslbl.Visibility=可见性.Visibility;
displayFilesBox.Visibility=可见性.Visibility;
generateBtn.Visibility=可见性.Visibility;
}
私有void生成\单击(对象发送方,路由目标)
{
工作簿原件=新工作簿(结果[0]);

对于(int i=1;i这个错误很不幸是COM互操作缺陷。否则,您将得到一个编译错误,因为
Workbook
是一个接口,并且没有构造函数。这个接口不是要实例化的,这就是为什么您会得到这个异常。(不确定为什么您也要在这里传递文件名…)

要解决此问题,请使用

其他说明:

  • 您应该最后调用
    dlg.ShowDialog()
    这样,您的过滤器就会生效
  • 对于您的过滤器,我建议为.xlsx文件添加一个选项,以支持更新的Excel文件
  • 在使用它之前添加
    result.Length>0
    ,以确保用户确实打开了某些内容
  • 将for循环条件更改为
    i
    ;否则,您将得到一个
    索引自动失效异常
另外,为了补充更多信息:您可能会问:“等等,ryrich,
应用程序
也是一个接口!我怎么能实例化,而不是
工作簿
?”


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

您在哪一行发现了错误?从代码中我可以看到一个错误(int i=1;i错误在第74行@Workbook Original=new工作簿上(结果[0]);我相信我的系统没有正确注册某些内容。当我尝试注册Microsoft.Office.Interop.Excel.dll时,我遇到了一个错误。此外,我在HKEY CLASSES_根值上发布了一张regedit的图片。顺便说一句,这些类型的异常似乎在您使用Office Interop时经常发生。您可能想切换到某些内容或者,由于它们完全位于.NET framework中,不依赖COM与外部库通信。您不能自己创建工作簿对象,必须使用工厂函数。使用Application.WorkBooks.Add()相反,这在Office对象模型中非常常见。顺便说一句,有大量的示例代码可以帮助避免类似这样的简单错误。@MichaelPetch好的,我会把它添加进去。看起来不错。我们两人在你的答案中把它都覆盖了。我已经删除了我的代码(即使它有投票权)。我们可以关注你的答案。
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);
        }



    }


    }
 }
using Microsoft.Office.Interop.Excel;

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