如何使用c#.net在Excel中获取选定的工作表名称

发布于 2024-12-14 21:32:13 字数 92 浏览 1 评论 0原文

嗨朋友们, 我是使用 Excel 的初学者。我需要弄清楚如何从 Excel 工作簿中查找选定的工作表名称。

多谢。

德瓦拉吉

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

郁金香雨 2024-12-21 21:32:13

这有点旧了。 2004年,但我使用了它,它对我很有帮助。我的理解是你想将某个 Excel 工作表调用到你的 C# 应用程序中?不管怎样,请查看这个网站,如果这就是您所做的,它将有所帮助。

C# - 检索 Excel 工作簿工作表名称。

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.

旧城烟雨 2024-12-21 21:32:13

您可以使用此解决方案....

取自此处....使用excel oledb按工作表顺序获取工作表名称

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

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;
}
謸气贵蔟 2024-12-21 21:32:13
Application xlsxApp;
string sheetname = (xlsxApp.ActiveSheet).Name;

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

请叫√我孤独 2024-12-21 21:32:13

这是更新的答案:

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

hth

Here is a more up to date answer:

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

hth

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文