Question posted 2010 · +7 upvotes
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
Accepted answer +17 upvotes
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…
Excel VBA objects referenced (5)
Application— Using events with the Application objectApplication— Working with Other ApplicationsExcel.Workbook— Workbooks and WorksheetsExcel.Workbook— Add a Table of Contents to a WorkbookExcel.Worksheet— Refer to All the Cells on the Worksheet
Top excel Q&A (6)
- Shortcut to Apply a Formula to an Entire Column in Excel +335 (2011)
- How should I escape commas and speech marks in CSV files so they work in Excel? +136 (2012)
- Convert xlsx to csv in linux command line +96 (2012)
- How to create a link inside a cell using EPPlus +50 (2011)
- IF statement: how to leave cell blank if condition is false ("" does not work) +44 (2013)
- T-SQL: Export to new Excel file +44 (2012)
excel solutions on this site
— top 21%.