C# – How to Rename Worksheet

By default, after creating an Excel workbook, the worksheets will be named as sheet1, sheet2 and sheet3. In order to show readers what the workbook includes, people may rename worksheet according to data they import. The other situation we may need to rename is that people have renamed one worksheet. But they change data several days later, so they want to rename worksheet to suit for recent data. For example, I import customers contact information in the first sheet. So, I change the default name as ContactInfo.

In this post, I will show how to rename a worksheet by using C#. I prepare an Excel file which I want to rename. This file just has one worksheet.

Default Name

The following steps show details to rename worksheet.

Note: I use Spire.XLS in this method. So I add its DLL as reference at the beginning.

  1. Load file from my computer.
  2. Select worksheet I want to rename and assign name string to worksheet.Name to rename.
  3. Save and Launch file

Coding:
 

using System;

using Spire.Xls;

namespace Rename_Worksheet

{

    class Rename

    {

        static void Main(string[] args)

        {

            //Load File

            Workbook workbook = new Workbook();

            workbook.LoadFromFile(@"D:\work\My Documents\CommissionReport.xlsx", ExcelVersion.Version2010);

 

            //Rename Worksheet

            Worksheet worksheet = workbook.Worksheets[0];

            worksheet.Name = "Commission Report";

 

            //Save and Launch File

            workbook.SaveToFile("Reanme.xlsx", ExcelVersion.Version2010);

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

        }

    }

}

 

After running, you can get the following result:

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

Click Here to learn more about Spire.XLS

Click Here to download

Advertisements

Author: janewdaisy

.NET Program Beginner. Share methods about how to use C#/VB.NET to export data, operate Word, Excel, PDF and other useful skills.

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