简体   繁体   中英

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:

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#?


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. 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];

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