Spread.Services Documentation
Work with Worksheets
Spread.Services Documentation > Developer's Guide > Customize User Interaction > Manage Worksheet > Work with Worksheets

While working with worksheets, you can perform the following operations to accomplish several important tasks in a workbook.

Access the default worksheet

Whenever a new workbook is created, an empty worksheet with the name Sheet1 is automatically added to the workbook. This worksheet is known as the default worksheet. For every workbook, only one default worksheet is added to it.

Refer to the following example code in order to access the default worksheet in your workbook.

C#
Copy Code
// Fetch the default WorkSheet
IWorksheet worksheet = workbook.Worksheets[0];

Add multiple worksheets

A workbook may contain any number of worksheets. You can add one or more worksheets before or after a specific sheet in your workbook.

Refer to the following example code to insert multiple worksheets in a workbook.

C#
Copy Code
//Initialize the WorkBook and add multiple WorkSheets
IWorksheet worksheet = workbook.Worksheets.Add();
IWorksheet worksheet2 = workbook.Worksheets.AddAfter(worksheet);
IWorksheet worksheet3 = workbook.Worksheets.AddBefore(worksheet2);

Activate a worksheet

While working with multiple worksheets in a workbook, you may require to make the current sheet to workbook's active sheet so as to execute certain operations on that particular worksheet. This can be done using the Activate method of the IWorksheet interface.

Refer to the following example code to activate a worksheet.

C#
Copy Code
IWorksheet worksheet3 = workbook.Worksheets.Add();
            
//Activate new created worksheet.
worksheet3.Activate();

Access a worksheet

All the worksheets within a workbook are stored in Worksheets collection. In order to access a specific worksheet within a workbook, you can choose either of the two ways : using the Index property or using the Name property of the IWorksheet interface.

Refer to the following example code to access a worksheet within the workbook.

C#
Copy Code
//Use sheet index to access the worksheet.
IWorksheet worksheet4 = workbook.Worksheets[0];

////Use sheet name to access the worksheet.
IWorksheet worksheet5 = workbook.Worksheets["SampleSheet5"];
//worksheet5.Name = "SampleSheet5";

Protect a worksheet

To ensure the data lying in the cells of your spreadsheet is safe and can't be modified by anyone, you can protect your worksheet by transforming it into a read-only sheet. Further, you can use the properties of the IProtectionSettings interface to explicitly setup your protected worksheet the way you want. Later, if you want to remove protection, you can unprotect your worksheet by setting the protection property to false.

Refer to the following example code to protect or unprotect a worksheet in Spread.Services.

C#
Copy Code
//protect worksheet, allow insert column.
worksheet3.Protection = true;
worksheet3.ProtectionSettings.AllowInsertingColumns = true;

//Unprotect worksheet.
worksheet3.Protection = false;

Delete Worksheet

You can remove one or more worksheets from a workbook. When you delete a worksheet, it automatically gets deleted from the Worksheets collection.

Refer to the following example code to delete a specific sheet from the workbook.

C#
Copy Code
IWorksheet worksheet7 = workbook.Worksheets.Add();

//workbook must contain one visible worksheet at least, if delete the one visible worksheet, it will throw exception.
worksheet7.Delete();
See Also