Transform Your Excel Experience: Effortlessly Rename Sheets with C#, Absolutely Free!
Renaming a worksheet in Excel is a common task that can help users better organize data and make the worksheet easier to understand. By renaming a worksheet, users can provide it with a more descriptive name that reflects the content of the worksheet. This can help users quickly find the information they need and avoid confusion. This tutorial will use the Free Spire.XLS for. NET control to rename worksheets and set tab colors.
How to install Free Spire.XLS for. NET?
Method 1: Download Free Spire.XLS for .NET locally, unzip it, and install it. After the installation is complete, find Spire.XLS.dll in the BIN folder under the installation path. Then open the "Solution Explorer" in Visual Studio, right-click "References", "Add Reference", and add a reference to the dll file in the BIN folder of the local path to the program.
Method 2: Install via NuGet. It can be installed by the following 2 methods:
(1) You can open the "Solution Explorer" in Visual Studio, right-click "References", "Manage NuGet Packages", then search for "Free Spire.XLS", and click "Install". Wait for the program installation to complete.
(2) Copy the following content to the PM console installation.
Install-Package FreeSpire.XLS -Version 12.7.0
Rename Excel Sheets and Set Tab Colors
Create a Workbook object.
Load a sample Excel file using Workbook.LoadFromFile() method.
Get a specified worksheet using Workbook.Worksheets[int] property.
Rename the specified worksheet using Worksheet.Name property.
Set tab color for the specified worksheet using Worksheet.TabColor property.
Save the document to another file using Workbook.SaveToFile() method.
Full Code:
C#
using Spire.Xls;
using System.Drawing;
namespace RenameWorksheet
{
class Program
{
static void Main(string[] args)
{
//Create a Workbook object
Workbook workbook = new Workbook();
//Load a sample Excel file
workbook.LoadFromFile(@"C:\Users\Administrator\Desktop\input.xlsx");
//Get the specified worksheet
Worksheet worksheet = workbook.Worksheets[0];
Worksheet worksheet1 = workbook.Worksheets[1];
Worksheet worksheet2 = workbook.Worksheets[2];
//Rename Excel worksheet
worksheet.Name = "Data";
worksheet1.Name = "Chart";
worksheet2.Name = "Summary";
//Set tab color
worksheet.TabColor = Color.DarkGreen;
worksheet1.TabColor = Color.Gold;
worksheet2.TabColor = Color.Blue;
//Save to file
workbook.SaveToFile("Rename.xlsx", ExcelVersion.Version2010);
}
}
}
VB.NET
Imports Spire.Xls
Namespace RenameWorksheet
Class Program
Private Shared Sub Main(args As String())
' Create a Workbook object
Dim workbook As New Workbook()
'Load a sample Excel file
workbook.LoadFromFile("C:\Users\Administrator\Desktop\input.xlsx")
'Get the specified worksheet
Dim worksheet As Worksheet = workbook.Worksheets(0)
Dim worksheet1 As Worksheet = workbook.Worksheets(1)
Dim worksheet2 As Worksheet = workbook.Worksheets(2)
'Rename Excel worksheet
worksheet.Name = "Rename sheet1"
worksheet1.Name = "Rename sheet2"
worksheet2.Name = "Rename sheet3"
'Set tab color
worksheet.TabColor = Color.DarkGreen
worksheet1.TabColor = Color.Red
worksheet2.TabColor = Color.Gold
'Save to file
workbook.SaveToFile("Rename.xlsx", ExcelVersion.Version2010)
End Sub
End Class
End Namespace
Conclusion:
In this post, you have learned how to rename Excel Sheets and set Tab Colors in C#/VB.NET. Not only that, we also have other functions, such as, Hide or Show Gridlines in Excel , Add Worksheets to Excel , Hide or Show Gridlines in Excel and so on. Apart from that, if you'd like to learn more, you can visit this link to explore more about for Spire.XLS for .NET.
Subscribe to my newsletter
Read articles from Carina Thorne directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by