Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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);
like image 523
Alex Gordon Avatar asked Aug 24 '12 20:08

Alex Gordon


People also ask

What does %C do in C?

Show activity on this post. %d is used to print decimal(integer) number ,while %c is used to print character . If you try to print a character with %d format the computer will print the ASCII code of the character.

What is %d in C programming?

In C programming language, %d and %i are format specifiers as where %d specifies the type of variable as decimal and %i specifies the type as integer. In usage terms, there is no difference in printf() function output while printing a number using %d or %i but using scanf the difference occurs.

Is C useful now?

The C programming language has been alive and kicking since 1972, and it still reigns as one of the fundamental building blocks of our software-studded world.


1 Answers

Here is what I did and it works!

Excel.Worksheet xlWorkSheetFocus = (Excel.Worksheet)xlWorkBook.Worksheets.get_Item(2);
xlWorkSheetFocus.Activate();
like image 102
Alex Gordon Avatar answered Sep 19 '22 12:09

Alex Gordon