Strange error on Excel Workbook input

2019-05-07 04:06发布

For the current code:

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

I recieve the error:

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

3条回答
Animai°情兽
2楼-- · 2019-05-07 04:33

I don't know about your error but I have done something similar like this:

 _app = new Excel.Application();
Excel.Workbook wb = _app.Workbooks.Open(currentPath);
查看更多
来,给爷笑一个
3楼-- · 2019-05-07 04:35

Right click to your Solution and change Platform to x86 Rebuild your solution... Good luck to you!

查看更多
手持菜刀,她持情操
4楼-- · 2019-05-07 04:57

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.

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);
查看更多
登录 后发表回答