none
Opening Excel file using C# RRS feed

  • Question


  • My Excel class contains:

    using Microsoft.Office.Interop.Excel;
    using _Excel = Microsoft.Office.Interop.Excel;

    namespace ExcelTest
    {
        class Excel
        {

            //global reference
            string path = "";
            _Application excel = new _Excel.Application();
            Workbook wb;
            Worksheet ws;

            //constructors


            public Excel(string path, int sheet)
            {
                this.path = path;
                wb = excel.Workbooks.Open(path);
                ws = wb.Worksheets[sheet];
            }

    }

    On executing "wb = excel.Workbooks.Open(path);"  I get the following error;

    System.InvalidCastException: 'Unable to cast COM object of type 'Microsoft.Office.Interop.Excel.ApplicationClass' to interface type 'Microsoft.Office.Interop.Excel._Application'. This operation failed because the QueryInterface call on the COM component for the interface with IID '{000208D5-0000-0000-C000-000000000046}' failed due to the following error: Interface not registered (Exception from HRESULT: 0x80040155).'

    How do I register or rectify it?  Do I have to re-install Excel?

    Many thanks

    Sue


    Thursday, April 25, 2019 7:42 PM

All replies

  • Hi,

      I am not sure what is going on with what you are doing, but this has worked for me to open Excel

    static Microsoft.Office.Interop.Excel.Application app = new Microsoft.Office.Interop.Excel.Application(); static Microsoft.Office.Interop.Excel.Application _App { get { if (app == null) { app = new Microsoft.Office.Interop.Excel.Application(); } return app; } }

    I open files like so:

    _App.Workbooks.Open(file.FullName, true, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value);

    I should say that by far my biggest issue with Excel and interop is closing Excel when I am done. I have gone through many iterations of closing and exiting Excel and none of them work every time.
    My advice would be to open every single Excel related within a try finally block and release it as soon as possible:

    public static void ReleaseComObject(object releasableComObject) { if (releasableComObject != null) { int count = Marshal.ReleaseComObject(releasableComObject); while (count > 0) { count = Marshal.ReleaseComObject(releasableComObject); } releasableComObject = null; } }

    Good luck!

    Ethan


    Ethan Strauss

    • Marked as answer by Soozi-Wong Thursday, April 25, 2019 9:39 PM
    • Unmarked as answer by Soozi-Wong Saturday, April 27, 2019 9:11 AM
    Thursday, April 25, 2019 8:01 PM
  • Many thanks, Ethan.

    I will give that all a go, but having tried it several times on 2 other machines where it worked it may mean a re-install of Excel

    Thanks,

    Sue


    • Edited by Soozi-Wong Thursday, April 25, 2019 9:02 PM
    Thursday, April 25, 2019 9:02 PM
  • Hello,

    Excel automation can be touch and go meaning if your version of Excel is different from the machine having issues thus can cause an issue and is common. 

    This is why using Open Office XML for Excel (if .xlsx format) is better as there are no version issues like with automation, downside, it's very difficult to create a solution for a task which brings us to use a third party (free library) and there are plenty free ones out there.


    Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.

    NuGet BaseConnectionLibrary for database connections.

    StackOverFlow
    profile for Karen Payne on Stack Exchange

    • Marked as answer by Soozi-Wong Thursday, April 25, 2019 9:40 PM
    • Unmarked as answer by Soozi-Wong Saturday, April 27, 2019 9:10 AM
    Thursday, April 25, 2019 9:14 PM
  • Thanks, Karen, xml may be the answer.  I will have a look that that too.
    Thursday, April 25, 2019 9:41 PM
  • My saga continues...

    I am still having this exception when I try to open the .xlsx file.

    As I said, it is working properly on my 2 other machines, just not this one.

    Name

    Value

    Type

    $exception

    {"Unable to cast COM object of type 'Microsoft.Office.Interop.Excel.ApplicationClass' to interface type 'Microsoft.Office.Interop.Excel._Application'. This operation failed because the QueryInterface call on the COM component for the interface with IID '{000208D5-0000-0000-C000-000000000046}' failed due to the following error: Interface not registered (Exception from HRESULT: 0x80040155)."}

    System.InvalidCastException

    I have uninstalled office, restarted the machine and re-installed.  I  even recreated a a new test app on this machine where I added a new reference to Excel, and still the problem persists. 

    I have only recently started using Office 365 and installed it on all 3 machines about the same time. The version numbers are the same - 16.011425.20228.  All 3 machines are running Windows 10 Pro.

    What program is it looking for?  Are there some Office Tools I should be installing?

    Help! I need to fix this.

    Thanks,

    Sue


    • Edited by Soozi-Wong Saturday, April 27, 2019 9:35 AM
    Saturday, April 27, 2019 9:17 AM
  • Hi Soozi-Wong,

    Since your question is more related to Excel, you could post a new thread in Excel for Developers forum.

    https://social.msdn.microsoft.com/Forums/en-US/home?forum=exceldev&filter=alltypes&sort=lastpostdesc

    Best Regards,

    Wendy


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Wednesday, May 1, 2019 7:34 AM
  • Thanks Wendy,

    Will do.

    Thursday, May 2, 2019 5:28 AM
  • Another option is SpreadSheetLight (totally free, unrestrictive)

    http://spreadsheetlight.com/

    SpreadSheetLight is only for .xlsx and is Open Office XML base, zero automation.


    Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.

    NuGet BaseConnectionLibrary for database connections.

    StackOverFlow
    profile for Karen Payne on Stack Exchange

    Thursday, May 2, 2019 3:29 PM
  • Hi Soozi,

    if you just want to open excel which is of type .xlsx or OpenOffice XML. You can also use ClosedXML which is a Nuget package.
    Nuget package link: https://www.nuget.org/packages/ClosedXML/ 
    They have Code examples to load and modify the excel as well :https://github.com/ClosedXML/ClosedXML/wiki/Loading-and-Modifying-Files

    Using Nuget packages would make the application independent of Excel installation on your system. I would recommend using Nuget pacakges to do any operations on excel.

    Hope this helps!! Cheers 

    Thursday, May 2, 2019 3:44 PM