C# – How to Name and Color Sheet Tab

In order to make viewers be clearer about what the data in one sheet presents, we often name this sheet. If all the worksheets in one workbook are named, people may set different color to highlight the sheet tabs. In this post, I will introduce method to name and color sheet tabs by using C#.

In my example, I created an empty workbook firstly. Then, named and colored three sheet tabs respectively. Because I prepare to add employee information in sheets, so names of these three sheets are about different departments: sales, support and develop.

This method is based on Spire.XLS as well. Add its DLL file as reference, please. Also, we need to add System.Drawing as reference for color setting.

Details:

Step 1: Create a new workbook and initialize worksheets (three by default).

            //Initialize Worksheet

            Workbook workbook = new Workbook();

            Worksheet sheet = workbook.Worksheets[1];

Step 2: Name and color the first sheet. Initialize it firstly and assign name and color value for it.

            //Set Tab Name and Color for the First Sheet

            Worksheet sheet1 = workbook.Worksheets[0];

            sheet1.Name = "Sales Department";

            sheet1.TabColor = Color.LightCyan;

Step 3: Name and color the second, third sheets as step 2.

Step 4: Save and launch new file.

Complete Coding:

using System;

using System.Drawing;

using Spire.Xls;

namespace tabcolor

{

    class TabColor

    {

        static void Main(string[] args)

        {

            //Initialize Worksheet

            Workbook workbook = new Workbook();

            Worksheet sheet = workbook.Worksheets[1];

            //Set Tab Name and Color for the First Sheet

            Worksheet sheet1 = workbook.Worksheets[0];

            sheet1.Name = "Sales Department";

            sheet1.TabColor = Color.LightCyan;

            //Set Tab Name and Color for the Second Sheet

            Worksheet sheet2 = workbook.Worksheets[1];

            sheet2.Name = "Support Department";

            sheet2.TabColor = Color.LightGoldenrodYellow;

            //Set Tab Name Color for the Third Sheet

            Worksheet sheet3 = workbook.Worksheets[2];

            sheet3.Name = "Develop Department";

            sheet3.TabColor = Color.Orange;

            //Save and Lanuch File

            workbook.SaveToFile("TabName&Color.xlsx", ExcelVersion.Version2010);

            System.Diagnostics.Process.Start(workbook.FileName);

        }

    }

}

Result:

————————————————————————————————————

Click Here to learn more about Spire.XLS

Click Here to download Spire.XLS
 

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s