Strange error entering Excel workbook

For 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 get an error:

Error System.Runtime.InteropServices.COME was unhandled Message = Getting factory COM class 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 for adding sheets to

+4
source share
3 answers

I believe that in your code the full name of the book is Microsoft.Office.Interop.Excel.Workbook, and that excel is an instance of Microsoft.Office.Interop.Excel.Application.

If so, your code cannot work because the Workbook is an interface and the interfaces have no constructors. You should ask the excel application to create books for you, and in your case you just need to write:

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

Similarly, if you want to create a new empty book, you must write:

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

I do not know about your error, but I did something like this:

  _app = new Excel.Application(); Excel.Workbook wb = _app.Workbooks.Open(currentPath); 
+2
source

Right-click on your solution and change the platform to x86 Recover your solution ... Good luck!

+1
source

Source: https://habr.com/ru/post/1416136/


All Articles