0

I try to read my Exel file from code and received System.InvalidCastException:

Additional information: Unable to cast COM object of type 'System.__ComObject' to interface type 'Microsoft.Office.Tools.Excel.Worksheet'. This operation failed because the QueryInterface call on the COM component for the interface with IID '{297DC8D9-EABD-45A1-BDEF-68AB67E5C3C3}' failed due to the following error: No such interface supported (Exception from HRESULT: 0x80004002 (E_NOINTERFACE)).

This error occurs in objsheet = appExcel.ActiveWorkbook.ActiveSheet; so I try to cast it into objsheet = (Worksheet)appExcel.ActiveWorkbook.ActiveSheet; but this error still exist

using Microsoft.Office.Interop.Excel;

private static Microsoft.Office.Interop.Excel._Application appExcel;
private static Microsoft.Office.Interop.Excel.Workbook newWorkbook = null;
private static Microsoft.Office.Interop.Excel.Worksheet objsheet = null;
private static string file = @"D:\file.xlsx";

      //Method to initialize opening Excel
    static void excel_init(String path)
    {
        //appExcel = new Microsoft.Office.Interop.Excel.Application();

        if (System.IO.File.Exists(path))
        {
            // then go and load this into excel
            newWorkbook = appExcel.Workbooks.Open(file, true, true);

            int count = newWorkbook.Worksheets.Count;
            if (count > 0)
            {
                objsheet = (Microsoft.Office.Interop.Excel.Worksheet)newWorkbook.Worksheets[1];
            }
        }
        else
        {
            MessageBox.Show("Unable to open file!");
            System.Runtime.InteropServices.Marshal.ReleaseComObject(appExcel);
            appExcel = null;
            System.Windows.Forms.Application.Exit();
        }
    }

newWorkbook is null.

6
  • 1
    You are checking if 'path' exists and then opening a workbook from 'file'. Is that correct? Commented Feb 28, 2014 at 17:41
  • 1
    Are you sure a sheet is active? The documentation says that this property can return Nothing if no sheet is active. Commented Feb 28, 2014 at 17:43
  • Does the machine you're running this on actually have the correct version of Excel installed? Commented Feb 28, 2014 at 17:44
  • What do you mean the correct version ? the file exist and all i want to do is add the option to add my excel file from code Commented Feb 28, 2014 at 17:51
  • 1
    I don't understand why you have a path parameter but then you attempt to open the file found at the file field. Is this intentional? Commented Feb 28, 2014 at 18:46

3 Answers 3

1

Workbook.ActiveSheet Property might not be the best choice for programmatically opened Excel files as it can actually return a non-Worksheet object

You might want to consider checking sheets count and using indexes:

int count = newWorkbook.Worksheets.Count;
if (count > 0)
{
    objsheet = (Worksheet) newWorkbook.Worksheets[1];
}

And try not to break the 2-dot rule - you'll need to release all you COM's to properly close your app and Excel.

Edited:

You could be mixing Microsoft.Office.Interop.Excel with Microsoft.Office.Tools.Excel namespaces.

Try to declare and assign as follows:

private static Microsoft.Office.Interop.Excel.Worksheet objsheet = null;
...
objsheet = (Microsoft.Office.Interop.Excel.Worksheet) newWorkbook.Worksheets[1];
Sign up to request clarification or add additional context in comments.

4 Comments

It seems that there are no worksheets in your book. Can you open the file manually and see if there is anything inside? And count is actually greater than 0?
What do you mean there is no worksheets ? in my file i have 4 seets
Edited my answer - please check.
See my update, Object reference not set to an instance of an object because newWorkbook is null
1

Seems like you are using

    Microsoft.Office.Tools.Excel.Worksheet 

instead of

    Microsoft.Office.Interop.Excel.Worksheet

which is why you are getting an invalid cast exception.

Comments

0

Try the below code and see if it works, if not let us know what problems you have:

using Excel = Microsoft.Office.Interop.Excel;

namespace Excel_Sample
{
    public partial class YourClass
    {
        Excel.Application appExcel;
        Excel.Workbook newWorkbook;
        Excel.Worksheet objsheet;
        string file = @"D:\file.xlsx";

        static void excel_init()
        {
            if (System.IO.File.Exists(file))
            {
                //Start Excel and get Application object.
                appExcel = new Excel.Application();
                //Get a workbook.;
                newWorkbook = (Excel.Workbook)(appExcel.Workbooks.Open(file));

                int count = newWorkbook.Worksheets.Count;
                if (count > 0)
                {
                    //Get Your Worksheet
                    objsheet = (Excel.Worksheet)newWorkbook.ActiveSheet;
                }
            }
            else
            {
                MessageBox.Show("Unable to open file!");
                System.Runtime.InteropServices.Marshal.ReleaseComObject(appExcel);
                appExcel = null;
                System.Windows.Forms.Application.Exit();
            }
        }
    }
}

1 Comment

Cannot open the excel file, returns The server threw an exception. (Exception from HRESULT: 0x80010105 (RPC_E_SERVERFAULT))

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.