How to Rename and Set Tab color for Excel Worksheet in WPF

2016-04-21 07:29:11 Written by  support iceblue
Rate this item
(0 votes)

Renaming excel worksheet can makes readers clearly understand what we want to present at their first sight, at the same time, we can also set tab color for a specific worksheet, in order to distinguish it from others and find it in a very short time.

This section will demonstrate how to rename and set tab color for excel worksheet in WPF using Spire.XLS for WPF.

Please follow the detail steps and code snippets below:

Use namespace:

using System.Drawing;
using System.Windows;
using Spire.Xls;

Step 1: Initialize a new instance of Workbook class and load the sample excel document from file.

Workbook workbook = new Workbook();
workbook.LoadFromFile("Sample.xlsx");

Step 2: Get the worksheets that need to be renamed and set tab color for. Here we choose the first and the second worksheets.

Worksheet sheet = workbook.Worksheets[0];
Worksheet sheet1 = workbook.Worksheets[1];

Step 3: Rename and set tab color.

Rename the first and the second worksheets.

sheet.Name = "Sales Report";
sheet1.Name = "Vendors Info";

Set tab color of the second worksheet as Lawn Green.

sheet1.TabColor = Color.LawnGreen;

Step 4: Save the changes and launch the file.

workbook.SaveToFile("Rename.xlsx");
System.Diagnostics.Process.Start("Rename.xlsx");

Result:

How to Rename and Set Tab color for Excel Worksheet in WPF

Full codes:

using Spire.Xls;
using System.Drawing;
using System.Windows;

namespace WpfApplication1
{
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }
        private void button1_Click(object sender, RoutedEventArgs e)
        {
            //Load the excel file
            Workbook workbook = new Workbook();
            workbook.LoadFromFile("Sample.xlsx");

            Worksheet sheet = workbook.Worksheets[0];
            Worksheet sheet1 = workbook.Worksheets[1];

            //rename worksheets
            sheet.Name = "Sales Report";
            sheet1.Name = "Vendors Info";

            //set tab color
            sheet1.TabColor = Color.LawnGreen;

            //save and launch the file
            workbook.SaveToFile("Rename.xlsx");
            System.Diagnostics.Process.Start("Rename.xlsx");
        }
    }
}

Additional Info

  • tutorial_title: Rename and Set Tab color for Excel Worksheet in WPF
Last modified on Friday, 24 September 2021 09:47