简体   繁体   中英

How to get the selected sheet name in Excel using c#.net

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

Here is a more up to date answer:

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

hth

Application xlsxApp;
string sheetname = (xlsxApp.ActiveSheet).Name;

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.

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;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM