Excel worksheet get item

18,969

I executed a sample application with similar code on a machine with Excel 2013 and it fails at the same line of code you mentioned. By default Excel 2013 application opens up with a single worksheet ("Sheet1") so you would have to modify the code accordingly

Share:
18,969
altandogan
Author by

altandogan

.

Updated on June 14, 2022

Comments

  • altandogan
    altandogan almost 2 years

    I have a problem with Excel worksheet. I am trying to create an Excel file with c#.

    This code works and runs correctly on my computer but in other computers get an error at last line:

    Excel.Application xlApp;
    Excel.Workbook xlWorkBook;
    Excel.Worksheet xlWorkSheetInvoice;
    Excel.Worksheet xlWorkSheetInvoiceLine;
    
    object misValue = System.Reflection.Missing.Value;
    
    xlApp = new Microsoft.Office.Interop.Excel.Application();
    xlWorkBook = xlApp.Workbooks.Add(misValue);
    
    xlWorkSheetInvoice = (Excel.Worksheet)xlWorkBook.Worksheets.get_Item(1);
    
    xlWorkSheetInvoiceLine = (Excel.Worksheet)xlWorkBook.Worksheets.get_Item(2);
    

    System.Runtime.InteropServices.COMException (0x8002000B): Invalid index. (Exception from HRESULT: 0x8002000B (DISP_E_BADINDEX)) at Microsoft.Office.Interop.Excel.Sheets.get_Item(Object Index)