0

Hi friends, I am beginner for using Excel. I need to figure out how to find the selected sheet name from the workbook in Excel.

Thanks a lot.

Devaraj

Frazell Thomas
  • 6,031
  • 1
  • 20
  • 21

4 Answers4

0

Here is a more up to date answer:

Excel.Worksheet activeWorksheet = ((Excel.Worksheet)Application.ActiveSheet);
string activeWorksheetName = activeWorksheet.Name;

hth

Joe Johnston
  • 2,794
  • 2
  • 31
  • 54
0
Application xlsxApp;
string sheetname = (xlsxApp.ActiveSheet).Name;

0

This is a little old. In 2004, but I used it and it helped me. What I understand is you want to call a certain excel sheet to your c# app? Anyway, check this site out, it will help if thats what your doing.

C# - Retrieve Excel Workbook Sheet Names.

javasocute
  • 648
  • 2
  • 11
  • 28
0

You can use this solution ....

Taken from here ....using excel oledb to get sheet names in sheet order

  private Dictionary<int,string> GetExcelSheetNames(string fileName)
    {
     Excel.Application _excel = null;
     Excel.Workbook _workBook = null;
     Dictionary<int,string> excelSheets = new Dictionary<int,string>();
     try
     {
      object missing = Type.Missing;
      object readOnly = true;

  Excel.XlFileFormat.xlWorkbookNormal
  _excel = new Excel.ApplicationClass();
  _excel.Visible = false;
  _workBook = _excel.Workbooks.Open(fileName, 0, readOnly, 5, missing,
     missing, true, Excel.XlPlatform.xlWindows, "\\t", false, false, 0, true, true, missing);
  if (_workBook != null)
  {

                   int index = 0; 

   foreach (Excel.Worksheet sheet in _workBook.Sheets)
   {
    // Can get sheet names in order they are in workbook
    excelSheets.Add(++index, sheet.Name);
   }
  }
 }
 catch (Exception e)
 {
  return null;
 }
 finally
 {
  if (_excel != null)
  {

   if (_workBook != null)
   {
    _workBook.Close(false, Type.Missing, Type.Missing);
   }
   _excel.Application.Quit();
  }
  _excel = null;
  _workBook = null;
 }
 return excelSheets;
}
Community
  • 1
  • 1
rockyashkumar
  • 1,302
  • 4
  • 13
  • 24