简体   繁体   English

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

[英]using c# to select a worksheet in excel

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

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.我已经使用了这段代码,它对我来说很好用。

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 xlApp property to Application object you're working with and then use the code above.如果不是,您应该至少将xlApp属性初始化为您正在使用的Application对象,然后使用上面的代码。 You can initialize first two objects by using the code below.您可以使用以下代码初始化前两个对象。

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

Here is what I did and it works!这是我所做的,并且有效!

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

要么

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

You can do it both ways:您可以通过两种方式执行此操作:

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

  2. xlWorksheet = xlApp.Worksheets[1];

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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