Excel工作簿输入出现奇怪的错误 [英] Strange error on Excel Workbook input

查看:180
本文介绍了Excel工作簿输入出现奇怪的错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

对于当前代码:

 String currentPath = Directory.GetCurrentDirectory();

        OpenFileDialog op = new OpenFileDialog();
        op.InitialDirectory = currentPath;
        if (op.ShowDialog() == DialogResult.OK)
            currentPath = op.FileName;
        else
        {
            toolStripStatusLabel1.Text = "Failed to Load Workbook";
            toolStripStatusLabel1.Visible = true;
        }

        Workbook wb = new Workbook(excel.Workbooks.Open(currentPath));

我收到错误:

System.Runtime.InteropServices.COMException未处理 消息=由于以下错误,未能为具有CLSID {00020819-0000-0000-C000-000000000046}的组件检索COM类工厂:80040154未注册类(HRESULT的异常:0x80040154(REGDB_E_CLASSNOTREG)). 来源= mscorlib ErrorCode = -2147221164

System.Runtime.InteropServices.COMException was unhandled Message=Retrieving the COM class factory for component with CLSID {00020819-0000-0000-C000-000000000046} failed due to the following error: 80040154 Class not registered (Exception from HRESULT: 0x80040154 (REGDB_E_CLASSNOTREG)). Source=mscorlib ErrorCode=-2147221164

我想要的是一个预定义的工作簿,用于向其中添加工作表

All I want is a predefined workbook to add worksheets to

推荐答案

我想在您的代码中,Workbook的全名是Microsoft.Office.Interop.Excel.Workbook,而excel是Microsoft.Office.Interop的一个实例. .Excel.Application.

I suppose that in your code the fullname of Workbook is Microsoft.Office.Interop.Excel.Workbook, and that excel is an instance of Microsoft.Office.Interop.Excel.Application.

如果是这种情况,则因为Workbook是一个接口,并且接口没有构造函数,您的代码将无法工作.您必须要求excel应用程序为您创建工作簿,在这种情况下,您只需编写以下内容:

If this is the case your code can't work because Workbook is an interface, and interfaces do not have constructors. You have to ask the excel application to create the workbooks for you, and in your case you have to simply write:

Workbook wb = excel.Workbooks.Open(currentPath); 

以类似的方式,如果要创建一个新的空工作簿,则应编写:

In a similar way, if you want to create a new empty workbook, you should write:

Workbook wb = excel.Workbooks.Add(System.Reflection.Missing.Value);

这篇关于Excel工作簿输入出现奇怪的错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆