invalid cast exception RRS feed

  • Question

  • I using excel in visual studio 2010. I am attempting to read excel data for elements of multidimensional array. My code is as follows H(cells(1,1),cells(1,2),cells(1,3)=cells(1,4). My program stops with the message "invalid cast expression was handled in my code, conversion from type Range to type integer is not valid" I do not know what this means or correct what is wrong.
    Sunday, March 26, 2017 3:29 AM

All replies

  • Hi bigalvino,

    Could you provide your code correctly?

    As for your thread, I'm afraid the number of parentheses are not the same.

    • Edited by Ashidacchi Sunday, March 26, 2017 11:30 AM
    Sunday, March 26, 2017 11:06 AM
  • Hi bigalvino,

    try to use code like below.

     Excel.Sheets sheets =  Application.Worksheets;
                Excel.Worksheet worksheet = (Excel.Worksheet)sheets.get_Item(1);
                System.Array myvalues;
                Excel.Range range = worksheet.get_Range("A1", "E1".ToString());
                myvalues = (System.Array)range.Cells.Value;


    Read Excel content into an array

    Reading from Excel (Range into multidimensional Array) C#



    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

    Monday, March 27, 2017 6:22 AM