facebooktwittermenuarrow-up

GemBox Support Forum

How to set current worksheet as active?

I’m generating an Excel file with GemBox.Spreadsheet and I just need to activate a certain worksheet so that when someone opens the file in Excel he’ll get the sheet I specified.

So, let’s say I have 3 sheets in Book.xlsx file named Sheet1, Sheet2, and Sheet3.
I load the Excel file:

var workbook = ExcelFile.Load("Book.xlsx");

I import some data into its worksheets and then I save it:

workbook.Save("Book.xlsx");

The current worksheet remains the same first Sheet1.
How can I select a different active sheet, so that the current worksheet is Sheet2 or Sheet3?

Hi hertzogth,

The current worksheet is the one that’s specified in ExcelFile.Worksheets.ActiveWorksheet property.
So, try the following:

var workbook = ExcelFile.Load("Book.xlsx");
var worksheets = workbook.Worksheets;

worksheets.ActiveWorksheet = worksheets["Sheet2"];
workbook.Save("Book.xlsx");

Does this solve your issue?

Regards,
Mario