Unable to cast COM object of type 'microsoft.Office.Interop.Excel.ApplicationClass' to 'microsoft.Office.Interop.Excel.Application'
If this is not a code-related error, then please remove the registry key listed below. This issue is caused by the key being left over from a higher version of Office.
Steps:
- Go to Start
- Select Run
- Type regedit and hit Enter
- Navigate to HKEY_CLASSES_ROOT -> TypeLib -> {00020813-0000-0000-C000-000000000046} -> 1.9/1.8/1.7
- Delete the registry key
Unable to cast COM object of type 'Microsoft.Office.Interop.Excel.WorksheetClass'
Like Hans already said you need to create an Excel application first, then you add a Workbook to it, then you add a Worksheet (or try to access an already existing Sheets[1]
)
Give using Microsoft.Office.Interop.Excel
and alias
using Excel = Microsoft.Office.Interop.Excel
and then
Excel.Application xlApp = new Excel.Application();
xlApp.Visible = true;
Excel.Workbook xlWb = xlApp.Workbooks.Add() as Excel.Workbook;
Excel.Worksheet xlSheet = xlWb.Sheets[1] as Excel.Worksheet;
Excel.Range range = xlSheet.get_Range("A1");
range.Value = "hello world!";
and then have a look at How to properly clean up Excel interop objects
C#: some of machines are throwing following error: Unable to cast COM object of type 'Microsoft.Office.Interop.Excel.ApplicationClass'
It seems some of contents are missing from the windows registry key,
and this can be happening when MS Excel is downgraded.
To fix this issue, Please go to regedit and delete [HKEY_CLASSES_ROOT-->TypeLib-->{00020813-0000-0000-C000-000000000046} --> 1.9]
and see if this works.
Related Topics
How to Exit a Wpf Application Programmatically
Sqldataadapter VS SQLdatareader
Wpf Application That Only Has a Tray Icon
Email Address Validation Using ASP.NET MVC Data Type Attributes
How to Elegantly Check If a Number Is Within a Range
Difference Between Await and Continuewith
Retrying Httpclient Unsuccessful Requests
Image Sequence to Video Stream
To Return Iqueryable<T> or Not Return Iqueryable<T>
In C#, What Happens When You Call an Extension Method on a Null Object
Implement Validation for Wpf Textboxes
Replace Only Some Groups with Regex