使用c#在excel中选择工作表

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12116098/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-09 22:07:33  来源:igfitidea点击:

using c# to select a worksheet in excel

c#.netexcelexcel-2007office-interop

提问by l--''''''---------''''''''''''

Using C# in .NET 3.5 with Visual Studio 2008, I am trying to set focus (or activate) a specific worksheet in an open workbook:

在 .NET 3.5 和 Visual Studio 2008 中使用 C#,我试图在打开的工作簿中设置焦点(或激活)特定工作表:

Here are some properties:

以下是一些属性:

public Excel.Application xlApp {get;set;}
public Excel.Workbook xlWorkBook { get; set; }
public Excel.Worksheet xlWorkSheet { get; set; }
public Excel.Range range { get; set; }        

And here is how I am trying to select a specific worksheet:

这是我尝试选择特定工作表的方式:

(xlWorkSheet)Application.ActiveWorkbook.Sheets[FormControls.WorksheetFocus]).Select(Type.Missing);

And I have also tried this way:

我也试过这种方式:

((Excel.Worksheet)this.Application.ActiveWorkbook.Sheets[1]).Select();

What am I doing wrong? How do I select a specific worksheet in a workbook using C#?

我究竟做错了什么?如何使用 C# 在工作簿中选择特定工作表?



explanation of where the definitions are:

定义在哪里的解释:

namespace EmailSalesVolumeSolution
{
    class WorkBook
    {
        public string MasterFileName { get; set; }
        public string[] DistinctEmails { get; set; }
        public Excel.Application xlApp {get;set;}
        public Excel.Workbook xlWorkBook { get; set; }
        public Excel.Worksheet xlWorkSheet { get; set; }
        public Excel.Range range { get; set; }    

and everything is in the same class and namespace

一切都在同一个类和命名空间中

here is how it is initiliazed:

这是它的初始化方式:

private void OpenWorkBook()
{
    string str;
    int rCnt = 0;
    int cCnt = 0;


    xlApp = new Excel.ApplicationClass();
    xlWorkBook = xlApp.Workbooks.Open(MasterFileName, 0, true, 5, "", "", true,
        Microsoft.Office.Interop.Excel.XlPlatform.xlWindows,
        "\t", false, false, 0, true, 1, 0);
    xlWorkSheet = (Excel.Worksheet)xlWorkBook.Worksheets.get_Item(FormControls.WorksheetEmails);

采纳答案by l--''''''---------''''''''''''

Here is what I did and it works!

这是我所做的,并且有效!

Excel.Worksheet xlWorkSheetFocus = (Excel.Worksheet)xlWorkBook.Worksheets.get_Item(2);
xlWorkSheetFocus.Activate();

回答by Nikola Male?evi?

Are your properties initialized?

你的属性初始化了吗?

If they are, you should probably be able to achieve what you are trying to by either of those:

如果是,您应该能够通过以下任一方式实现您的目标:

xlApp.ActiveWorkbook.Sheets[1].Activate();
xlWorkbook.Sheets[1].Activate();
xlSheet.Activate();

If they are not, you should initialize at least xlAppproperty to Applicationobject you're working with and then use the code above. You can initialize first two objects by using the code below.

如果不是,您应该至少将xlApp属性初始化为Application您正在使用的对象,然后使用上面的代码。您可以使用以下代码初始化前两个对象。

xlApp = new Microsoft.Office.Interop.Excel.Application();
Workbooks xlWorkbooks = xlApp.Workbooks;
xlWorkbook = xlWorkbooks.Open(@"C:\filename.xlsx");

回答by M_Mogharrabi

You can use the following code :

您可以使用以下代码:

Worksheet sheet = (Worksheet)xlApp.Worksheets[1];
sheet.Select(Type.Missing);

or

或者

sheet.Activate();

I have used this code and it works fine for me.

我已经使用了这段代码,它对我来说很好用。

回答by André Bravo Ferreira

You can do it both ways:

您可以通过两种方式执行此操作:

Excel.Application xlApp;
Excel.Worksheet xlWorksheet;
  1. xlWorksheet = xlApp.Worksheets.get_Item(1);

  2. xlWorksheet = xlApp.Worksheets[1];

  1. xlWorksheet = xlApp.Worksheets.get_Item(1);

  2. xlWorksheet = xlApp.Worksheets[1];

回答by Himanshu Rajoriya

xlWorkSheet = (Worksheet)xlWorkBook.Worksheets.get_Item(2);

OR

或者

xlWorkSheet =(Worksheet)xlWorkBook.Sheets["SheetName"];