如何使用VSTO 2010 for Excel按名称显示工作表

时间:2022-06-08 07:23:49

I have never used VSTO and I am finding it difficult to find a good learning aid for 2010.

我从未使用过VSTO,我发现很难找到2010年的学习辅助工具。

My need is simple, I have a business workbook with 42 worksheets (I orignally guessed 20 but after counting found a surprising number). I want to add a ribbon (That part is easy) using VSTO to allow employees to navigate the large number of pages easily. I cannot seem to find the c# code to display a specific worksheet (Preferably by name) that I can simply add to the click event of the buttons.

我的需求很简单,我有一个包含42个工作表的业务工作簿(我原先猜测了20个,但在计算后发现了一个令人惊讶的数字)。我想使用VSTO添加功能区(这部分很简单),以便员工轻松浏览大量页面。我似乎无法找到c#代码来显示我可以简单地添加到按钮的click事件的特定工作表(最好是通过名称)。

Thanks

谢谢

1 个解决方案

#1


10  

Call the Activate method on the worksheet object (of type Microsoft.Office.Tools.Excel.Worksheet).

在工作表对象(类型为Microsoft.Office.Tools.Excel.Worksheet)上调用Activate方法。

You can do this by name from within your ThisWorkbook class or via Globals.ThisWorkbook as follows:

您可以通过ThisWorkbook类中的名称或通过Globals.ThisWorkbook执行以下操作:

private Excel.Worksheet GetWorksheetByName(string name)
{
  foreach (Excel.Worksheet worksheet in this.Worksheets)
  {
    if (worksheet.Name == name)
    {
      return worksheet;
    }
  }
  throw new ArgumentException();
}

private void ActivateWorksheetByName(string name)
{
  GetWorksheetByName(name).Activate();
}

Call the ActivateWorksheetByName and pass the name of the worksheet to show.

调用ActivateWorksheetByName并传递要显示的工作表的名称。

#1


10  

Call the Activate method on the worksheet object (of type Microsoft.Office.Tools.Excel.Worksheet).

在工作表对象(类型为Microsoft.Office.Tools.Excel.Worksheet)上调用Activate方法。

You can do this by name from within your ThisWorkbook class or via Globals.ThisWorkbook as follows:

您可以通过ThisWorkbook类中的名称或通过Globals.ThisWorkbook执行以下操作:

private Excel.Worksheet GetWorksheetByName(string name)
{
  foreach (Excel.Worksheet worksheet in this.Worksheets)
  {
    if (worksheet.Name == name)
    {
      return worksheet;
    }
  }
  throw new ArgumentException();
}

private void ActivateWorksheetByName(string name)
{
  GetWorksheetByName(name).Activate();
}

Call the ActivateWorksheetByName and pass the name of the worksheet to show.

调用ActivateWorksheetByName并传递要显示的工作表的名称。