I have users that name their sheets all sorts of crazy things, but I want to be able to get the first sheet of the Excel document regardless of what it is named.
I
That's my solution
private static string GetExcelWorkSheet(string pathToExcelFile)
{
Microsoft.Office.Interop.Excel.Application ExcelObj = new Microsoft.Office.Interop.Excel.Application();
Microsoft.Office.Interop.Excel.Workbook theWorkbook = null;
theWorkbook = ExcelObj.Workbooks.Open(pathToExcelFile);
Microsoft.Office.Interop.Excel.Sheets sheets = theWorkbook.Worksheets;
// Get the reference of first worksheet. Index start at 1
Microsoft.Office.Interop.Excel.Worksheet worksheet = (Microsoft.Office.Interop.Excel.Worksheet)sheets.get_Item(1);
// Get the name of worksheet.
string strWorksheetName = worksheet.Name;
return strWorksheetName;
}