C# Activate a Worksheet

Activate a worksheet means clicking the worksheet's tab on MS Excel. Usually when we open the Excel workbook, it goes to the worksheet when we closed last time. With Spire.XLS, we can set a worksheet as active sheet in the workbook by using the sheet.Activate() method. Then when we open the excel workbook next time, it goes to the activate worksheet we set.

C# Code Snippet of how to activate a worksheet:

Step 1: Create an instance of Excel workbook and load the document from file.

Workbook workbook = new Workbook();
workbook.LoadFromFile("Sample.xlsx");

Step 2: Get the second worksheet from the workbook.

Worksheet sheet = workbook.Worksheets[1];

Step 3: Activate the sheet.

sheet.Activate();

Step 4: Save the document to file.

workbook.SaveToFile("result.xlsx",FileFormat.Version2013);   

Full codes:

using Spire.Xls;
namespace ActivateWorksheet
{
    class Program
    {

        static void Main(string[] args)
        {
            Workbook workbook = new Workbook();
            workbook.LoadFromFile("Sample.xlsx");

            Worksheet sheet = workbook.Worksheets[1];

            sheet.Activate();

            workbook.SaveToFile("result.xlsx", FileFormat.Version2013);

        }
    }
}