tags:

views:

170

answers:

1

Hi,

I'm currently obtaining a handle to a Excel worksheet by using the below C# code:

*Excel.Worksheet worksheet = (Excel.Worksheet)sheets.get_Item(15);//Get the worksheet "SubSignOff" number*

Is there any way that I can obtain the same by using the worksheet name -- "SubSignOff" ?

Many thanks for your help.

--Chapax

+1  A: 

Instead of the using Excel.Workbook.Sheets collection, it's easier to access the Excel.Workbook.Worksheets collection, that way you can utilize early binding.

In your case, it could look something like the following:

Excel.Application excelApp = new Excel.Application();
excelApp.Visible = true;

Excel.Workbook workbook = excelApp.Workbooks.Open("C:\MyWorkbook.xls",
    Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, 
    Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, 
    Type.Missing, Type.Missing, Type.Missing, Type.Missing);

// The key line:
Excel.Worksheet worksheet = (Excel.Worksheet)workbook.Worksheets["SubSignOff"];

Hope this helps...

Mike Rosenblum
Many thanks Mike ... I'll try this out.
Chapax
Hi Chapax, did it work?
Mike Rosenblum