Rename a Worksheet
Workbooks hold multiple worksheets to allow efficient organization and consolidation of data. Often, workbooks contain worksheets with related data. In such cases, naming the worksheets appropriately helps you find and retrieve information from the workbook.
Setting the Worksheet Name
When you add a new worksheet to a workbook collection, it is automatically assigned the first available name in the sequence Sheet1, Sheet2, Sheet3, Sheet4, and so on. Once the worksheet is added to the workbook, you can access it and change its name through the Name property.
The following constraints apply to the worksheet name:
|
Worksheet validation rules |
|---|
|
Each worksheet must have a unique name in the workbook. The comparison of the worksheet names is case-insensitive. That said, sheets with names "Sheet1" and "sheet1" cannot reside within the same workbook. If you attempt to set a name that already appears in the collection, an exception is raised. |
|
The name of the worksheet cannot contain the following symbols: \ / ? * [ ] : |
|
The name of the worksheet must not start or end with a single quote ('). However, the symbol may appear inside the worksheet name. For example, "Sam's Worksheet" is a correct name and "Sam'" is not valid. |
|
The name of the worksheet cannot be an empty string. |
|
The name of the worksheet cannot exceed 31 characters. |
Example 1 creates a new workbook, adds a single worksheet to it, and renames the newly added worksheet.
Example 1: Create and Rename a Worksheet
Workbook workbook = new Workbook();
Worksheet worksheet = workbook.Worksheets.Add();
worksheet.Name = "Sam's Worksheet";
Example 2 creates a new workbook and adds two worksheets to it. The snippet illustrates how to rename the worksheet with index 0 to "July's Worksheet". To ensure name uniqueness, the sample code checks if the workbook already contains a worksheet with the desired name.
Example 2: Rename a Worksheet
Workbook workbook = new Workbook();
workbook.Worksheets.Add();
workbook.Worksheets.Add();
string newWorksheetName = "July's Worksheet";
int indexOfWorksheetToRename = 0;
int index = workbook.Worksheets.IndexOf(newWorksheetName);
if (index == -1 || index == indexOfWorksheetToRename)
{
workbook.Worksheets[indexOfWorksheetToRename].Name = newWorksheetName;
}
else
{
//There already exists worksheet with this name
}