C#/VB.NET: Rename Excel Sheets and Set Tab Colors

2022-02-24 03:05:00 Written by  support iceblue
Rate this item
(0 votes)

An Excel file can contain dozens of sheets, and sometimes you may need to rename these sheets to make the whole workbook more organized. Meanwhile, setting different tab colors also seems to be a good way to highlight certain important sheets. This article will introduce how to programmatically rename Excel sheets and set tab colors using Spire.XLS for .NET.

Install Spire.XLS for.NET

To begin with, you need to add the DLL files included in the Spire.XLS for .NET package as references in your .NET project. The DLL files can be either downloaded from this link or installed via NuGet.

PM> Install-Package Spire.XLS 

Rename Excel Sheets and Set Tab Colors

Spire.XLS for .NET offers a simple solution for you to rename sheets and set tab colors in Excel. The detailed steps are as follows.

  • 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.
  • C#
  • VB.NET
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);
        }
    }
}

C#/VB.NET: Rename Excel Sheets and Set Tab Colors

Apply for a Temporary License

If you'd like to remove the evaluation message from the generated documents, or to get rid of the function limitations, please request a 30-day trial license for yourself.

Additional Info

  • tutorial_title: Rename Excel Sheet and Set Tab Color
Last modified on Thursday, 24 February 2022 07:50